【发布时间】:2017-11-27 08:57:25
【问题描述】:
我有两种插入 json 字符串的场景。 我使用下面的代码检查 json 字符串是否已经存在,然后我插入到表中
**Scenario - 1**
declare @jsonStr nvarchar(max) = {"Manufacurer": "test", "Model": "A", "Color":"New Color","Thickness":"1 mm"}
declare @SqlStr nvarchar(max), @counts int
Select @dynamicFilter = Coalesce(@dynamicFilter,'') +' and Json_Value(ItemOfProduct,''$."'+ [key] +'"'') = ''' +[value]+'''' from openjson(jsonStr)
set @SqlStr = 'select @cnt = count(*) from tblTest where '+@dynamicFilter
Exec sp_executesql @SqlStr, N'@cnt int output', @cnt = @counts output
if(@counts < 1)
Begin
//insert the data into the table
End
**Scenario - 2**
set @jsonStr = {"Manufacurer": "test", "Model": "A", "Color":"New Color"}
//Do the same above process of inserting
在第二个场景中,我的数据没有被插入到表中,因为计数是 1。但是我希望场景 1 和 2 中的字符串都被视为不同,并且两个数据都被插入到数据库中。
【问题讨论】:
-
tblTest的结构是什么?桌子上有什么限制吗?您实际使用什么代码尝试将记录插入表中?您的代码似乎缺少帮助您调试它所需的部分。
标签: sql json sql-server sql-server-2016