【问题标题】:Sql OpenJson - Incorrect syntax near '$.recordtype'Sql OpenJson - '$.recordtype' 附近的语法不正确
【发布时间】:2020-08-12 19:52:49
【问题描述】:

我有一个要执行的存储过程,其功能是将 json 结果插入到我的表“ItemTransaction”中。然后我将通过声明 JsonString 的变量来检索它。问题是我无法执行我的存储过程。

“$.recordtype”附近的语法不正确。

我不知道到底是哪个错误。

exec AWSLINK.APILINK.dbo.spu_JsonSendData 'LoadItem',@pBranchCode,@pTranNo,'','','Test','POST'

        Declare @JsonString varchar(MAX) = (Select DataReceived from AWSLINK.APILINK.dbo.ItemTransaction where 
                                DocType ='LoadItem' and BranchCode = @pBranchCode and TranNo =  @pTranNo)

            SELECT * FROM  
            OPENJSON ( @JsonString,'$."records"')  
            WITH (   
                          RecordType   varchar(200) '$.recordtype' ,  
                          ItemID     varchar     '$.id',  
                          ItemDetails varchar(200) '$.itemid',  
                          Quantity int          '$.locationquantityonhand'  
             ) 

【问题讨论】:

  • 为什么不在过程中使用输出变量输出 JSON?
  • @SteveC 你是什么意思?
  • 你的存储过程的定义是什么?您发布的代码看起来“很好”。给我们一个minimal reproducible example
  • @Larnu,是的,它看起来不错,但是当我添加上面显示的代码时。当我尝试保存/执行时,它不能。上面显示的错误也是。
  • 添加什么代码?您说EXEC 语句失败,但是我们不知道该过程(spu_JsonSendData)中的 SQL 是什么,如果没有minimal reproducible example,那么我们不知道为什么我们看不到的 SQL 失败了.不要忘记,我们没有(也不应该)访问您的 SQL 实例,因此您需要提供所有所需的信息。如果程序失败,则意味着您需要提供所述程序的定义,以及可能的样本数据和样本参数,以便我们可以运行所述程序。

标签: sql json sql-server


【解决方案1】:

类似的东西

drop proc if exists dbo.TestExample;
go
create proc dbo.TestExample
  @var1             int,
  @json             nvarchar(max) output
as
set nocount on;
select @json=(select @var1 var1 for json path, without_array_wrapper);
go

declare
  @out_json          nvarchar(max);

exec dbo.TestExample 1, @json=@out_json output;
print (@out_json);

输出: {"var1":1}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多