• sqlserver 之 将查询结果变为json字符串


    GO
    /****** Object:  StoredProcedure [dbo].[SerializeJSON]    Script Date: 6/4/2019 3:58:23 PM
        将查询结果变为json字符串
     ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[SerializeJSON] (
       @ParameterSQL NVARCHAR(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, @XML=@XML OUTPUT
    SET @XMLString=CAST(@XML AS VARCHAR(MAX))
    
      DECLARE @json_xml NVARCHAR(MAX);
      BEGIN TRY--//开始捕捉异常
        DECLARE @JSON NVARCHAR(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 @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);
            DECLARE @end INT = CHARINDEX('/>', @Row, @FieldStart);
            IF @end<1
              OR @end>@FieldEnd
            BEGIN
              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 + '",';
            END;
            SET @FieldStart = @FieldStart + LEN(@StartField);
            SET @FieldEnd = CHARINDEX(@EndField, @Row, @FieldStart);
            SET @FieldStart = CHARINDEX(@StartField, @Row, @FieldEnd);
          END;
          IF LEN(@JSON)>0
            SET @JSON = SUBSTRING(@JSON, 0, LEN(@JSON));
          SET @JSON = @JSON + '},';
          --// for each row
          SET @RowStart = CHARINDEX(@StartRoot, @XMLString, @RowEnd);
        END;
        IF LEN(@JSON)>0
          SET @JSON = SUBSTRING(@JSON, 0, LEN(@JSON));
          SET @JSON = '[' + @JSON + ']';
        SET @json_xml = @JSON;
      END TRY--//结束捕捉异常
      BEGIN CATCH--//有异常被捕获
        SET @json_xml = @XMLString;
      END CATCH;--//结束异常处理
      select @json_xml as result_json;
    END;

    USE [EMRWS]GO/****** Object:  StoredProcedure [dbo].[SerializeJSON]    Script Date: 6/4/2019 3:58:23 PM将查询结果变为json字符串 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[SerializeJSON] (   @ParameterSQL NVARCHAR(MAX)  )ASBEGINDECLARE @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_xml NVARCHAR(MAX);  BEGIN TRY--//开始捕捉异常    DECLARE @JSON NVARCHAR(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 @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);        DECLARE @end INT = CHARINDEX('/>', @Row, @FieldStart);        IF @end<1          OR @end>@FieldEnd        BEGIN          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 + '",';        END;        SET @FieldStart = @FieldStart + LEN(@StartField);        SET @FieldEnd = CHARINDEX(@EndField, @Row, @FieldStart);        SET @FieldStart = CHARINDEX(@StartField, @Row, @FieldEnd);      END;      IF LEN(@JSON)>0        SET @JSON = SUBSTRING(@JSON, 0, LEN(@JSON));      SET @JSON = @JSON + '},';      --// for each row      SET @RowStart = CHARINDEX(@StartRoot, @XMLString, @RowEnd);    END;    IF LEN(@JSON)>0      SET @JSON = SUBSTRING(@JSON, 0, LEN(@JSON));  SET @JSON = '[' + @JSON + ']';    SET @json_xml = @JSON;  END TRY--//结束捕捉异常  BEGIN CATCH--//有异常被捕获    SET @json_xml = @XMLString;  END CATCH;--//结束异常处理  select @json_xml as result_json;END;

  • 相关阅读:
    NGINX_深度优化实践
    NFS服务端___NFS客户端
    NFS 批量管理 分发
    MYSQL---数据备份与还原
    MYSQL---建立字符集数据库
    MYSQL---基于mysql多实例数据库创建主从复制
    MYSQL---关于MYSQL优化
    bug记录-left jion连接后不是一对一情况时,记得去重
    bug记录-不等于某个值,查询后注意不包括为空的情况(由于NULL不能直接用算术运算符进行比较值。要想把为NULL 的那行也查询出来的话,只能使用IS NULL)
    bug记录-sqljion连接 like
  • 原文地址:https://www.cnblogs.com/xiaoping1993/p/10974074.html
Copyright © 2020-2023  润新知