我可以在 sql 游标中使用 sql 递归吗?
Can I use sql recursive in sql cursor?
我正在编写一个程序,希望通过使用 mssql.
在游标中递归执行函数
下面代码中的函数ParseJson
参考
PS。参考例子是递归版本,但我的问题是一步一步解析。
这是我函数的第一个结果 ParseJson
topKey Key isTerminal Value
Book IssueDate 1 02-15-2019
Book Detail 0 { "Type":"Any Type", "Author":{ "Name":"Annie" , "Sex":"Female"}
Book Chapter 0 [{ "Section":"1.1", "Title":"Hello world." }, { "Section":"1.2", "Title":"Be happy." }]
Book Sponsor 0 ["A","B","C"]
每一列的值isTerminal
就是条件,
当 isTerminal=0
然后执行函数 ParseJson
;
当 isTerminal=1
然后打印一些东西。
我正在创建一个过程以在 sql 游标中递归执行函数。
函数创建成功,执行失败
create procedure CursorJson
@json nvarchar(max)
, @Type nvarchar(max)
, @isArray bit = 0
as
begin
set nocount on
declare
@TopKey nvarchar(4000)
, @Key nvarchar(4000)
, @IsType bit
, @IsList bit
, @isTerminal bit
, @Value nvarchar(4000)
--defind
declare myCursor cursor for
--dataset
select * from ParseJson(@json, @Type, @isArray)
--open
open myCursor
--run
fetch next from myCursor into
@TopKey nvarchar(4000)
, @Key nvarchar(4000)
, @IsType bit
, @IsList bit
, @isTerminal bit
, @Value nvarchar(4000)
while(@@fetch_status = 0)
begin
if @isTerminal = 0
begin
set @json = '{"' + @Key + '":' + @Value + '}'
exec CursorJson @json, @Key, @isList
end
else
begin
print 'insert...'
end
fetch next from myCursor into
@TopKey nvarchar(4000)
, @Key nvarchar(4000)
, @IsType bit
, @IsList bit
, @isTerminal bit
, @Value nvarchar(4000)
end
--close and deallocate
close myCursor
deallocate myCursor
return
end
declare @Type nvarchar(max)=N'Book'
declare
@json nvarchar(max)=N'{
"Book":{
"IssueDate":"02-15-2019"
, "Detail":{
"Type":"Any Type"
, "Author":{
"Name":"Annie"
, "Sex":"Female"
}
}
, "Chapter":[
{
"Section":"1.1"
, "Title":"Hello world."
}
,
{
"Section":"1.2"
, "Title":"Be happy."
}
]
, "Sponsor":["A","B","C"]
}
}'
--exec
exec CursorJson @json, @Type, 0
Program CursorJson, [Batch Start Line 0] The cursor with the name 'myCursor' already exists.
"quick fix" 将指定 cursor is local:
declare myCursor cursor local for
但我会认真地重新检查您是否可以通过使用 recursive CTE 来实现您想要做的事情。
我正在编写一个程序,希望通过使用 mssql.
在游标中递归执行函数下面代码中的函数ParseJson
参考
PS。参考例子是递归版本,但我的问题是一步一步解析。
这是我函数的第一个结果 ParseJson
topKey Key isTerminal Value
Book IssueDate 1 02-15-2019
Book Detail 0 { "Type":"Any Type", "Author":{ "Name":"Annie" , "Sex":"Female"}
Book Chapter 0 [{ "Section":"1.1", "Title":"Hello world." }, { "Section":"1.2", "Title":"Be happy." }]
Book Sponsor 0 ["A","B","C"]
每一列的值isTerminal
就是条件,
当 isTerminal=0
然后执行函数 ParseJson
;
当 isTerminal=1
然后打印一些东西。
我正在创建一个过程以在 sql 游标中递归执行函数。 函数创建成功,执行失败
create procedure CursorJson
@json nvarchar(max)
, @Type nvarchar(max)
, @isArray bit = 0
as
begin
set nocount on
declare
@TopKey nvarchar(4000)
, @Key nvarchar(4000)
, @IsType bit
, @IsList bit
, @isTerminal bit
, @Value nvarchar(4000)
--defind
declare myCursor cursor for
--dataset
select * from ParseJson(@json, @Type, @isArray)
--open
open myCursor
--run
fetch next from myCursor into
@TopKey nvarchar(4000)
, @Key nvarchar(4000)
, @IsType bit
, @IsList bit
, @isTerminal bit
, @Value nvarchar(4000)
while(@@fetch_status = 0)
begin
if @isTerminal = 0
begin
set @json = '{"' + @Key + '":' + @Value + '}'
exec CursorJson @json, @Key, @isList
end
else
begin
print 'insert...'
end
fetch next from myCursor into
@TopKey nvarchar(4000)
, @Key nvarchar(4000)
, @IsType bit
, @IsList bit
, @isTerminal bit
, @Value nvarchar(4000)
end
--close and deallocate
close myCursor
deallocate myCursor
return
end
declare @Type nvarchar(max)=N'Book'
declare
@json nvarchar(max)=N'{
"Book":{
"IssueDate":"02-15-2019"
, "Detail":{
"Type":"Any Type"
, "Author":{
"Name":"Annie"
, "Sex":"Female"
}
}
, "Chapter":[
{
"Section":"1.1"
, "Title":"Hello world."
}
,
{
"Section":"1.2"
, "Title":"Be happy."
}
]
, "Sponsor":["A","B","C"]
}
}'
--exec
exec CursorJson @json, @Type, 0
Program CursorJson, [Batch Start Line 0] The cursor with the name 'myCursor' already exists.
"quick fix" 将指定 cursor is local:
declare myCursor cursor local for
但我会认真地重新检查您是否可以通过使用 recursive CTE 来实现您想要做的事情。