Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 插入参数值和JSON字符串值_Sql_Json_Sql Server_Tsql - Fatal编程技术网

Sql 插入参数值和JSON字符串值

Sql 插入参数值和JSON字符串值,sql,json,sql-server,tsql,Sql,Json,Sql Server,Tsql,我试图创建一个存储过程,通过JSON和参数插入数据 Declare @PaymentJson Nvarchar(1000) = N'{"type":4},{"type":1`},{"type":2},{"type":3}' ,@ProductID bigint = 5 ,@UserCode bigint = 2 Insert into PaymentType ([ProductID], [PaymentT

我试图创建一个存储过程,通过
JSON
参数插入数据

Declare @PaymentJson        Nvarchar(1000)  = N'{"type":4},{"type":1`},{"type":2},{"type":3}'  
        ,@ProductID         bigint  = 5
        ,@UserCode          bigint  = 2

Insert into PaymentType ([ProductID], [PaymentType],[UserCode])
            SELECT @ProductID,
                    PaymentType = MAX(CASE WHEN LOWER([key]) = 'type' THEN [value] END),
                    @UserCode
            FROM OPENJSON(@PaymentJson)
结果不是真的,是第一行JSON(在本例中类型是4)

(1行受影响)

我需要,在这种情况下,插入行
PaymentType
中所需的
JSON
字符串数应为:

(4行受影响)


My DBMS is SQL Server 2019

此意外行为的原因是,输入的JSON无效(多个根元素),但
OPENJSON()
成功地从无效的JSON解析第一个对象(尽管
ISJSON()
返回
0
)。作为一种解决方法,您需要将输入JSON转换为有效的JSON数组,并使用
OPENJSON()
和带有适当列定义的显式模式对其进行解析:

DECLARE 
   @PaymentJson nvarchar(1000) = N'{"type":4},{"type":1},{"type":2},{"type":3}',
   @ProductID bigint = 5,
   @UserCode bigint = 2

INSERT INTO PaymentType ([ProductID], [PaymentType], [UserCode])
SELECT 
   @ProductID,
   [Type],
   @UserCode
FROM OPENJSON(CONCAT('[', @PaymentJson, ']')) WITH ([Type] int '$.type')

首先,JSON字符串无效,它必须是数组

N'[{"type":4},{"type":1},{"type":2},{"type":3}]'
其次,您不需要在select语句中使用聚合函数

因此,您的代码应该如下所示:

Declare @PaymentJson        Nvarchar(1000)  = N'[{"type":4},{"type":1},{"type":2},{"type":3}]'  
        ,@ProductID         bigint  = 5
        ,@UserCode          bigint  = 2

Insert into PaymentType ([ProductID], [PaymentType],[UserCode])
            SELECT @ProductID,
                    PaymentType = (CASE WHEN LOWER([key]) = 'type' THEN [value] END),
                    @UserCode
            FROM OPENJSON(@PaymentJson)

您好,我尝试了一下,我有一个错误:无法将值NULL插入“PaymentType”列、表“PaymentType”中;列不允许空值。插入失败。然后注释insert并使用select,它的tru,return NULL谢谢你的详细回答,它工作正常。
Declare @PaymentJson        Nvarchar(1000)  = N'[{"type":4},{"type":1},{"type":2},{"type":3}]'  
        ,@ProductID         bigint  = 5
        ,@UserCode          bigint  = 2

Insert into PaymentType ([ProductID], [PaymentType],[UserCode])
            SELECT @ProductID,
                    PaymentType = (CASE WHEN LOWER([key]) = 'type' THEN [value] END),
                    @UserCode
            FROM OPENJSON(@PaymentJson)