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() ...
随机推荐
- POJ训练计划2418_Hardwood Species(Trie树)
解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include < ...
- CentOS下tmux安装与使用
tmux介绍: tmux它是BSDScreen替代品,相对于Screen,它更加先进:支持屏幕切分,并且具备丰富的命令行參数,使其能够灵活.动态的进行各种布局和操作.它能够做到一条命令就启动起来(强大 ...
- CentOS6.5 Nginx优化编译配置[续]
继续上文CentOS6.5 Nginx优化编译配置本文记录有关Nginx系统环境的一些细节设置,有关Nginx性能调整除了配置文件吻合服务器硬件之前就是关闭不必要的服务.磁盘操作.文件描述符.内核调整 ...
- java.nio分析软件包(三)---Charset理解力
前面的分析后,2一个基本的封装类型.现在我们就来揭开Java.nio魔法知识的最后一块,CharsetEncoding类,他的主要功能是实现字节Unicode之间的转换转码. 让我们来看看他同样的封装 ...
- repo总结
repo刚google使用Python脚本写通话git脚本.主要用于下载.管理Android工程仓库. 1. 下载 repo 的地址: http://android.git.kernel.org/re ...
- jvisualvm远程监控Tomcat
网上已经有很多这方面的资料,但有些很杂乱,这里做了整理总结. 一.Java VisualVM 概述 对于使用命令行远程监控jvm 太麻烦 . 在jdk1.6 中 Oracle 提供了一个新的可视化的. ...
- [ACM] POJ 3254 Corn Fields(状态压缩)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8062 Accepted: 4295 Descr ...
- android传感器;摇抽奖功能
package com.kane.sensortest; import java.util.Random; import android.hardware.Sensor; import android ...
- 【程序员联盟】官网上线啦!coderunity.com
内容简介 欢天喜地,[程序员联盟]官网上线咯(此处应该有鸡蛋丢过来...) [程序员联盟]官网 大家也许会问:“这几天小编都没出文章,跑哪里happy去啦?是不是偷懒去了?” 小编:“臣妾冤枉啊.” ...
- client多线程
1.多线程对象 对象可以是多线程访问,线程可以在这里分为两类: 为完成内部业务逻辑的创建Thread对象,线程需要访问对象. 使用对象的线程外部对象. 进一步假设更精细的划分.业主外螺纹成线等线,. ...