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() ...
随机推荐
- Follow your hear(跟着心走)
端午三天的哈尔滨之旅已经over,非常开心真的非常开心.听了刘四风老师的"为爱开讲.我爱这世界"的论坛,尽管.这三天老师讲的不多.可是句句是精华.Follow your heart ...
- 开展:随笔记录 OSGI的jar增加了一些小问题和注意事项
在引用jar当包,假设引用的项目包.在需要MANIFEST.MF 它定义 一.外用jar: 实例:外部参考需要包装的Import package里面 定义一下.如:google-gson-2.2.2. ...
- 九度OJ 1177 查找 (模拟)
题目1177:查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5659 解决:1667 题目描写叙述: 读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共同拥有 ...
- [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 55273 ...
- 十一:Java之GUI图形Awt和Swing
一. AWT和 Swing AWT 和 Swing 是 Java 设计 GUI用户界面的基础.与 AWT 的重量级组件不同,Swing 中大部分是轻量级组件.正是这个原因,Swing 差点儿无所不能, ...
- JNI 可变印刷
1.包log.h #ifndef __MULTI_TRACE_H__ #define __MULTI_TRACE_H__ #ifdef ANDROID_NDK_BUILD #define LOG_TA ...
- 开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式
原文:[原创]开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式 开源Math.NET基础数学类库使用系列文章总目录: 1.开源.NET基础数学计算组件Math. ...
- Swing动画之游戏角色
一.动画效果:实现了飞机飞行的动画效果,也实现了飞机的移动. 二.实现原理: 1.飞机飞行 的效果:事实上也还是重写paintComponent,依照一定的时间间隔更换图片就有了飞行的效果,动画就是更 ...
- OnWndMsg处理功能
于MFC于, OnWndMsg功能,如以下处理步骤: 1)首先.被推断消息有一个消息响应函数.例如OnLButtonDown()处理"左新闻"新闻. 2)数.这里以MFC 的pro ...
- 高清电视产业的关键词,4K过渡时期8K未来
有些不尽人意,归根结底在于,绝大多数厂商并没有把电视的性能作为突破口,相反,仅仅是在外观.设计上做起了文章.在部分厂商看来,要真正研发性能一流的智能电视须要更高的投入,但改变一下外形似乎也能获 ...