SQL Server存储过程到JSON错误

时间:2017-04-26 18:51:27

标签: sql-server json stored-procedures sql-server-2014

我正在尝试编写以下存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spInsertAudit]
    (@val1 VARCHAR(100),
     @val2 VARCHAR(200))
AS
BEGIN TRY
    DECLARE @return_value varchar(max)
    /************************************/
    /*            The Query             */
    /************************************/
    EXEC [dbo].[SerializeJSON] 
    'INSERT INTO tLogin (EMP, LoginInTime) 
     VALUES (@val1, @val2)'
END TRY
BEGIN CATCH
    /************************************/
    /*      Get Error results back      */
    /************************************/
    EXEC [dbo].[SerializeJSON] 
    'SELECT ERROR_NUMBER()  AS ErrorNumber,
        ERROR_SEVERITY()    AS ErrorSeverity,
        ERROR_STATE()       AS ErrorState,
        ERROR_PROCEDURE()   AS ErrorProcedure,
        ERROR_LINE()        AS ErrorLine,
        ERROR_MESSAGE()     AS ErrorMessage'
END CATCH

SerializeJSON存储过程(找到here)是这样的:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER 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
        , @XML = @XML 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 @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) > 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 + ']'

    SELECT @JSON
END

当我执行spInsertAudit存储过程时,出现此错误:

  

Msg 102,Level 15,State 1,Line 8
  'XML'附近的语法不正确。

所以它确实写了那个表,但由于某种原因,它没有返回JSON并且上面有这个错误....

0 个答案:

没有答案