CODE - TSQL convert Query to JSON
原文 ODE - TSQL convert Query to JSON
TSQL - Query to JSON
It is my philosophy that good development starts with the data. I have always stressed whenever possible allow your data processing to take place on your SQL server or database processing engine and rendering of the data to the application control engine. By the time your application server receives the data it should be in the truncated, filtered, limited by rows, converted to correct formats, free of whitespace ect. Your application should only receive what it will use on the screen and nothing more. This however requires a developer to actually develop code, Stored Procedures and Functions.
This follows the same logical philosophy and creates a simple Query to JSON procedure.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE[dbo].[SerializeJSON](@ParameterSQL AS VARCHAR(MAX))ASBEGIN DECLARE @SQL NVARCHAR(MAX)DECLARE @XMLString VARCHAR(MAX)DECLARE @XML XMLDECLARE @Paramlist NVARCHAR(1000)SET @Paramlist = N'@XML XML OUTPUT'SET @SQL = 'WITH PrepareTable (XMLString)'SET @SQL = @SQL + 'AS('SET @SQL = @SQL + @ParameterSQL+ 'FOR XML RAW,TYPE,ELEMENTS'SET @SQL = @SQL + ')'SET @SQL = @SQL + 'SELECT @XML=[XMLString]FROM[PrepareTable]'EXEC sp_executesql @SQL, @Paramlist, @XML=@XML OUTPUTSET @XMLString=CAST(@XML AS VARCHAR(MAX)) DECLARE @JSON VARCHAR(MAX)DECLARE @Row VARCHAR(MAX)DECLARE @RowStart INTDECLARE @RowEnd INTDECLARE @FieldStart INTDECLARE @FieldEnd INTDECLARE @KEY VARCHAR(MAX)DECLARE @Value VARCHAR(MAX) DECLARE @StartRoot VARCHAR(100);SET @StartRoot='<row>'DECLARE @EndRoot VARCHAR(100);SET @EndRoot='</row>'DECLARE @StartField VARCHAR(100);SET @StartField='<'DECLARE @EndField VARCHAR(100);SET @EndField='>' SET @RowStart=CharIndex(@StartRoot,@XMLString,0)SET @JSON=''WHILE @RowStart>0BEGIN SET @RowStart=@RowStart+Len(@StartRoot) SET @RowEnd=CharIndex(@EndRoot,@XMLString,@RowStart) SET @Row=SubString(@XMLString,@RowStart,@RowEnd-@RowStart) SET @JSON=@JSON+'{' -- for each row SET @FieldStart=CharIndex(@StartField,@Row,0) WHILE @FieldStart>0 BEGIN -- parse node key SET @FieldStart=@FieldStart+Len(@StartField) SET @FieldEnd=CharIndex(@EndField,@Row,@FieldStart) SET @KEY=SubString(@Row,@FieldStart,@FieldEnd-@FieldStart) SET @JSON=@JSON+'"'+@KEY+'":' -- parse node value SET @FieldStart=@FieldEnd+1 SET @FieldEnd=CharIndex('</',@Row,@FieldStart) SET @Value=SubString(@Row,@FieldStart,@FieldEnd-@FieldStart) SET @JSON=@JSON+'"'+@Value+'",' SET @FieldStart=@FieldStart+Len(@StartField) SET @FieldEnd=CharIndex(@EndField,@Row,@FieldStart) SET @FieldStart=CharIndex(@StartField,@Row,@FieldEnd) END IF LEN(@JSON)>0SET @JSON=SubString(@JSON,0,LEN(@JSON)) SET @JSON=@JSON+'},' --/ for each row SET @RowStart=CharIndex(@StartRoot,@XMLString,@RowEnd)ENDIF LEN(@JSON)>0SET @JSON=SubString(@JSON,0,LEN(@JSON))SET @JSON='['+@JSON+']'SELECT @JSON ENDGO |
Call thestored procedure
|
1
|
EXEC[SerializeJSON]'SELECT*FROM[Employee_TBL]' |
CODE - TSQL convert Query to JSON的更多相关文章
- URLSearchParams & GET Query String & JSON
URLSearchParams & GET Query String & JSON https://developer.mozilla.org/zh-CN/docs/Web/API/U ...
- 使用TSQL查询和更新 JSON 数据
JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...
- PHP 数组转JSON数据(convert array to JSON object);
<?php header('Content-type: appliction/json; charset=shift-JIS'); $data =array(); class Test { pu ...
- Query a JSON array in SQL
sql 中存的json 为数组: [{"Level":1,"Memo":"新用户"},{"Level":2," ...
- Vs code 下设置python tasks.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.jso ...
- Convert ResultSet to JSON and XML
public static JSONArray convertToJSON(ResultSet resultSet) throws Exception { JSONArray jsonArray = ...
- vs code 用户代码片段 html.json
{ // Place your snippets for html here. Each snippet is defined under a snippet name and has a p ...
- [Backbone] Parse not formatted JSON code
The good Dr. recently had another team implement the server and they slightly messed up the format o ...
- 【Json】Jackson将json转换成泛型List
Jackson将json转换成泛型List 获取泛型类型 /** * 获取泛型类型 * * @return */ protected Class<T> getGenericsType() ...
随机推荐
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_1
以下是一些设计略显繁琐,有必要清除思维. 下一个主要的成就,当我们点击Gobutton后,得到一个随机数骰子,是走了几步,它是基于以下步骤行走路径的数目,然后移动位置的基于角色的路径. 流程如图普遍认 ...
- Windows 8 应用开发 - 磁贴
原文:Windows 8 应用开发 - 磁贴 我们开发的应用在Win8 界面中会以磁贴形式展现,默认情况下磁贴会显示应用图标,即项目工程中的Logo.png图片文件.开发人员可按应用的需要使 ...
- 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快. 1.定义变量和常量 var 定义变量,let定义常量. 比如 ...
- [文学阅读] METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments
METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments Satanje ...
- Swift新手教程3-字符串String
原创blog,转载请注明出处 String 在swfit中,String兼容Unicode的方式.用法和C语言类似. 注意 在Cocoa和Cocoa touch中,Swift的String,和Fo ...
- POJ 2538 WERTYU水的问题
[题目简述]:题意非常easy,没有trick. [分析]:事实上这题还是挺有趣的,在 算法竞赛入门经典中也有这一题. 详见代码: // 120K 0Ms /* 边学边做 -- */ // 字符串:W ...
- effective c++ 条款5 c++ 默默实现的函数
当写一个空类c++ 会为我们自动提供四个函数 1 默认构造函数 2 默认析构函数 3 拷贝构造函数 4 默认赋值运算符
- hibernate的通配符比拼接sql到底好在哪?
Hibernate对于刚接触的人来说,通配符只是提供了另一种组合sql的方式.接触的久了,熟悉之后,才能够真正理解通配符在Hibernate中起到的作用 主要作用有两点: 1,避免sql注入 hibe ...
- 【rman,1】经典案例增量备份
一.备份策略: 1.星期天晚上 -level 0 backup performed(全备份) 2.星期一晚上 -level 2 backup performed 3.星期二晚上 ...
- HTML5 CSS3 精美案例 : 达到VCD盒个性幻灯片
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/31015121 哈,首先感谢下w3cfuns教师,行~ 行.这一次分享发夹CSS3 ...