Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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_Sql Server_Stored Procedures - Fatal编程技术网

Sql json数据的存储过程选择

Sql json数据的存储过程选择,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我一直在努力格式化存储过程以创建json字符串数据 我有一个示例查询,其中我需要像这样格式化json字符串 {“摘要”:“员工变更用户名”,“日期修改”:“2017/09/06”,“变更”:[{“属性”:“测试”,“旧”:“10”,“新”:“1”},{“属性”:“测试”,“旧”:“10”,“新”:“2”},{“属性”:“测试”,“旧”:“10”,“新”:“3”},{“属性”:“测试”,“新”:“4”},{“属性”:“测试”,“旧”:“10”,“新”:“5”} 目前我的输出仍然是这样 {“委员会成

我一直在努力格式化存储过程以创建json字符串数据

我有一个示例查询,其中我需要像这样格式化json字符串

{“摘要”:“员工变更用户名”,“日期修改”:“2017/09/06”,“变更”:[{“属性”:“测试”,“旧”:“10”,“新”:“1”},{“属性”:“测试”,“旧”:“10”,“新”:“2”},{“属性”:“测试”,“旧”:“10”,“新”:“3”},{“属性”:“测试”,“新”:“4”},{“属性”:“测试”,“旧”:“10”,“新”:“5”}

目前我的输出仍然是这样

{“委员会成员”:[{“属性”:“测试”,“旧”:“10”,“新”:“1”},{“属性”:“测试”,“旧”:“10”,“新”:“2”},{“属性”:“测试”,“旧”:“10”,“新”:“3”},{“属性”:“测试”,“旧”:“10”,“新”:“4”},{“属性”:“测试”,“旧”:“10”,“新”:“5”}


这是数字小提琴

您可能需要在此处使用光标

 declare @new_Cursor as CURSOR 
 declare @Property varchar(10)   
 declare @oldValue varchar(10)   
 declare @newValue varchar(10)   
 declare @jsonResult nvarchar(max) ='{"CommitteeMembers":['
set @new_Cursor = CURSOR FOR SELECT * FROM @jsonTable FOR JSON PATH, ROOT 
('CommitteeMembers')
    OPEN @new_Cursor
    FETCH NEXT FROM @new_Cursor INTO @Property,@oldValue,@newValue
    WHILE @@FETCH_STATUS = 0
    Begin
      if(@jsonResult<>'{"CommitteeMembers":[') --To separate the objects
          begin
               set @jsonResult = @jsonResult + ','
          end 
      set @jsonResult = @jsonResult + 
'{"Property":"'+@Property+'","old":"'+@oldValue+'","new":"'+@newValue+'"}'
 FETCH NEXT FROM @new_Cursor INTO @Property,@oldValue,@newValue
    end
    CLOSE @new_Cursor
   DEALLOCATE @new_Cursor
   set @jsonResult = @jsonResult + ']}'

  select @jsonResult as Result
将@new\u游标声明为游标
声明@Property varchar(10)
声明@oldValue varchar(10)
声明@newValue varchar(10)
声明@jsonResult nvarchar(max)='{“委员会成员”:['
将@new_Cursor=光标设置为SELECT*从@jsonTable设置为JSON路径,根
(“委员会成员”)
打开@new_光标
从@new\u游标获取下一个到@Property、@oldValue、@newValue
而@@FETCH\u STATUS=0
开始
if(@jsonResult'{“CommitteeMembers”:[')--用于分隔对象
开始
设置@jsonResult=@jsonResult+','
结束
设置@jsonResult=@jsonResult+
“{”Property:“++@Property+”,“old:“++@oldValue+”,“new:“++@newValue+”}”
从@new\u游标获取下一个到@Property、@oldValue、@newValue
结束
关闭@new_光标
释放@new_游标
设置@jsonResult=@jsonResult+']}
选择@jsonResult作为结果
更新

问题在于select语句


您可能需要在此处使用光标

 declare @new_Cursor as CURSOR 
 declare @Property varchar(10)   
 declare @oldValue varchar(10)   
 declare @newValue varchar(10)   
 declare @jsonResult nvarchar(max) ='{"CommitteeMembers":['
set @new_Cursor = CURSOR FOR SELECT * FROM @jsonTable FOR JSON PATH, ROOT 
('CommitteeMembers')
    OPEN @new_Cursor
    FETCH NEXT FROM @new_Cursor INTO @Property,@oldValue,@newValue
    WHILE @@FETCH_STATUS = 0
    Begin
      if(@jsonResult<>'{"CommitteeMembers":[') --To separate the objects
          begin
               set @jsonResult = @jsonResult + ','
          end 
      set @jsonResult = @jsonResult + 
'{"Property":"'+@Property+'","old":"'+@oldValue+'","new":"'+@newValue+'"}'
 FETCH NEXT FROM @new_Cursor INTO @Property,@oldValue,@newValue
    end
    CLOSE @new_Cursor
   DEALLOCATE @new_Cursor
   set @jsonResult = @jsonResult + ']}'

  select @jsonResult as Result
将@new\u游标声明为游标
声明@Property varchar(10)
声明@oldValue varchar(10)
声明@newValue varchar(10)
声明@jsonResult nvarchar(max)='{“委员会成员”:['
将@new_Cursor=光标设置为SELECT*从@jsonTable设置为JSON路径,根
(“委员会成员”)
打开@new_光标
从@new\u游标获取下一个到@Property、@oldValue、@newValue
而@@FETCH\u STATUS=0
开始
if(@jsonResult'{“CommitteeMembers”:[')--用于分隔对象
开始
设置@jsonResult=@jsonResult+','
结束
设置@jsonResult=@jsonResult+
“{”Property:“++@Property+”,“old:“++@oldValue+”,“new:“++@newValue+”}”
从@new\u游标获取下一个到@Property、@oldValue、@newValue
结束
关闭@new_光标
释放@new_游标
设置@jsonResult=@jsonResult+']}
选择@jsonResult作为结果
更新

问题在于select语句


FOR JSON语句中不允许使用光标-这就是错误请参见此处的dbdiffle链接@SecretCoder我正确获得结果请检查图像否我想添加“摘要”:员工变更等等,“日期修改”:“2017/09/06“在json数据上。我已经实现了committeemembers json,但我想在select语句中添加摘要和日期修饰符。如果您解决了这一问题,它将从@jsonTable FOR json PATH,ROOT('CommitteeMbers')中选择*…这是罪魁祸首,必须相应地修改光标,这将导致JSON语句中不允许使用workCURSOR-这就是错误请参见此处的dbdiffle链接@SecretCoder我得到了正确的结果请检查图像否我想添加“摘要”:员工变动等等,“日期修改”:“2017/09/06”“在json数据上。我已经实现了committeemembers json,但我想在select语句中添加Summary和date ModifierDay,无论问题是什么,如果您解决了这个问题,它将在@jsonTable FOR json路径、ROOT('CommitteeMbers')中运行select*。这是罪魁祸首,必须相应地修改光标,它就会运行。”