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 ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE[dbo].[SerializeJSON](

@ParameterSQL AS VARCHAR(MAX)

)

AS

BEGIN

 

DECLARE @SQL NVARCHAR(MAX)

DECLARE @XMLString VARCHAR(MAX)

DECLARE @XML XML

DECLARE @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, @[email protected] OUTPUT

SET @XMLString=CAST(@XML AS VARCHAR(MAX))

 

DECLARE @JSON VARCHAR(MAX)

DECLARE @Row VARCHAR(MAX)

DECLARE @RowStart INT

DECLARE @RowEnd INT

DECLARE @FieldStart INT

DECLARE @FieldEnd INT

DECLARE @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>0

BEGIN

    SET @[email protected]+Len(@StartRoot)

    SET @RowEnd=CharIndex(@EndRoot,@XMLString,@RowStart)

    SET @Row=SubString(@XMLString,@RowStart,@[email protected])

    SET @[email protected]+‘{‘

 

    -- for each row

    SET @FieldStart=CharIndex(@StartField,@Row,0)

    WHILE @FieldStart>0

    BEGIN

        -- parse node key

        SET @[email protected]+Len(@StartField)

        SET @FieldEnd=CharIndex(@EndField,@Row,@FieldStart)

        SET @KEY=SubString(@Row,@FieldStart,@[email protected])

        SET @[email protected]+‘"‘[email protected]+‘":‘

        -- parse node value

        SET @[email protected]+1

        SET @FieldEnd=CharIndex(‘</‘,@Row,@FieldStart)

        SET @Value=SubString(@Row,@FieldStart,@[email protected])

        SET @[email protected]+‘"‘[email protected]+‘",‘

 

        SET @[email protected]+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 @[email protected]+‘},‘

    --/ for each row

 

    SET @RowStart=CharIndex(@StartRoot,@XMLString,@RowEnd)

END

IF LEN(@JSON)>0SET @JSON=SubString(@JSON,0,LEN(@JSON))

SET @JSON=‘[‘[email protected]+‘]‘

SELECT @JSON

 

END

GO

Call thestored procedure

?


1

EXEC[SerializeJSON]‘SELECT*FROM[Employee_TBL]‘

时间: 2024-10-31 16:04:44

CODE - TSQL convert Query to JSON的相关文章

T-SQL Convert日期问题

print char(13)+char(10)+'该信息于'+Convert(nvarchar,GETDATE(),111)+':完成自处理派工' 如果第三个参数没写则时间是:08 10 2014  9:49AM Convert第三个参数说明 101 mm/dd/yyyy 102 yyyy.mm.dd 103 dd/mm/yyyy 104 dd.mm.yyyy 105 dd-mm-yyyy 具体看帮助文档 http://msdn.microsoft.com/query/dev10.query?a

PHP 数组转JSON数据(convert array to JSON object);

<?php header('Content-type: appliction/json; charset=shift-JIS'); $data =array(); class Test { public $userid ; public $cmt ; } for ($x=1; $x<=50; $x++) { $test =new Test(); $test->userid = urlencode("user".strval($x)); $test->cmt =

Vs code 下设置python tasks.json

{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "python", "type": "shell", "c

Convert ResultSet to JSON and XML

public static JSONArray convertToJSON(ResultSet resultSet) throws Exception { JSONArray jsonArray = new JSONArray(); while (resultSet.next()) { int total_rows = resultSet.getMetaData().getColumnCount(); JSONObject obj = new JSONObject(); for (int i =

Jackson序列化和反序列化Json数据完整示例

Jackson序列化和反序列化Json数据 Web技术发展的今天,Json和XML已经成为了web数据的事实标准,然而这种格式化的数据手工解析又非常麻烦,软件工程界永远不缺少工具,每当有需求的时候就会出现各种类库,框架以及工具来解决这些基础的问题,Jackson就是这些工具中的一个,使用这个工具开发者完全可以从手工结束Json数据的重复劳动中解放出来.使用Jackson首先需要下载相应的类库,如下的Maven dependency列出了完整的POM dependency. 1 <dependen

javascript xml字符串转为JSON对象

/* ### jQuery XML to JSON Plugin v1.2 - 2013-02-18 ### * http://www.fyneworks.com/ - [email protected] * Licensed under http://en.wikipedia.org/wiki/MIT_License ### Website: http://www.fyneworks.com/jquery/xml-to-json/ *//* # INSPIRED BY: http://www.

Query runs slow via .NET

Slow in the Application, Fast in SSMS?Understanding Performance Mysteries An SQL text by Erland Sommarskog, SQL Server MVP. Last revision: 2013-08-30.This article is also available in Russian, translated by Dima Piliugin. Introduction When I read var

CQRS学习——Storage实现(EF+Code First+DynamicReponsitory)[其四]

[这里是的实现,指的是针对各个数据访问框架的一个基础实现] 目标 定义仓储/QueryEntry的基本功能 实现仓储的基本功能,以利于复用 实现一些常用的功能 提供一些便利的功能 目标框架 博主使用的ORM框架是EF6.x,使用MAP来配置模型和数据库之间的映射(因为模型是定义在领域层[CQRS]的),所以不打算使用声明式的Attribute.使用code first来生成数据库. 仓储基本功能 使用一个泛型接口定义了一个仓储需要实现的功能: public interface IBasicRep

SpringMVC——对Ajax的处理(包含 JSON 类型)

一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON 数据和实体之间的对应. (2)如何组织这些发送的数据. 2.从服务器端来看,需要搞明白: (1)SpringMVC 如何返回 JSON 数据. (2)SpringMVC 如何处理请求的复杂数据. 3.$.ajax 的几个参数: (1)contentType: contentType: 'appli