如何使用mssql根据条件table结果执行两个函数?
How to execute two function according to a table result with a condition by using mssql?
ParseValuesJson
和 insertToTable
是用户定义的函数。
下面是执行结果(ParseValuesJson
):
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"]
}
}'
declare
@tempTable table (
topKey nvarchar(4000)
, [key] nvarchar(4000)
, IsType bit
, IsList bit
, [value] nvarchar(4000))
--execute
insert @tempTable
select * from GetValuesJson(@json,default)
topKey Key isType isList Value
--
Book Type 0 0 Any Type
Book Author 1 0 {"Name":"Annie", "Sex":"Female"}
Book IssueDate 0 0 02-15-2019
Book Chapter 1 1 [{"Section":"1.1", "Title":"Hello world."}, {"Section":"1.2", "Title":"Be happy."}]
Book Sponsor 1 1 ["A","B","C"]
如题,如果忽略函数的作用,如何达到下面的目的?
如果 IsType
=1 ,我想调用函数 ParseValuesJson
;
如果 IsType
=0 ,我想调用函数 insertToTable
.
但是我发现sqlcase
不能那样用
这个sql查询可以递归执行并相应地在同一级别调用不同的函数。
这意味着我不能先解析所有字符串(ParseValuesJson
)然后将结果(insertToTable
)插入到table。
有没有其他方法可以实现?
select
case IsType when 1 then
ParseValuesJson('{"' + [key] + '":' + [value] + '}',IsList)
else
insertToTable(topKey,[key])
end
from ParseValuesJson(@json,default)
嗯,最简单的做法是将它分成两个单独的 SELECT。
select ParseValuesJson('{"' + [key] + '":' + [value] + '}',IsList)
from ParseValuesJson(@json,default)
where IsType = 1
select insertToTable(topKey,[key])
from ParseValuesJson(@json,default)
where IsType = 0
但我想这种方法对您没有帮助,因为在用户定义的函数中您不能使用 INSERT、UPDATE、DELETE 语句 -> 即修改 table 数据
所以我猜想要解析 JSON 您需要使用递归 CTE 来首先解析所有值,然后将它们立即插入到临时 table 中。
像这样:
;WITH ParsedJSON(topKey, [key], IsType, IsList, [value])
AS
(
SELECT topKey, [key], IsType, IsList, [value]
FROM ParseValuesJson('{"' + [key] + '":' + [value] + '}',IsList)
UNION ALL
SELECT topKey, [key], IsType, IsList, [value]
FROM ParsedJSON
WHERE IsType = 1
)
insert @tempTable
select * from ParsedJSON
ParseValuesJson
和 insertToTable
是用户定义的函数。
下面是执行结果(ParseValuesJson
):
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"]
}
}'
declare
@tempTable table (
topKey nvarchar(4000)
, [key] nvarchar(4000)
, IsType bit
, IsList bit
, [value] nvarchar(4000))
--execute
insert @tempTable
select * from GetValuesJson(@json,default)
topKey Key isType isList Value -- Book Type 0 0 Any Type Book Author 1 0 {"Name":"Annie", "Sex":"Female"} Book IssueDate 0 0 02-15-2019 Book Chapter 1 1 [{"Section":"1.1", "Title":"Hello world."}, {"Section":"1.2", "Title":"Be happy."}] Book Sponsor 1 1 ["A","B","C"]
如题,如果忽略函数的作用,如何达到下面的目的?
如果 IsType
=1 ,我想调用函数 ParseValuesJson
;
如果 IsType
=0 ,我想调用函数 insertToTable
.
但是我发现sqlcase
不能那样用
这个sql查询可以递归执行并相应地在同一级别调用不同的函数。
这意味着我不能先解析所有字符串(ParseValuesJson
)然后将结果(insertToTable
)插入到table。
有没有其他方法可以实现?
select
case IsType when 1 then
ParseValuesJson('{"' + [key] + '":' + [value] + '}',IsList)
else
insertToTable(topKey,[key])
end
from ParseValuesJson(@json,default)
嗯,最简单的做法是将它分成两个单独的 SELECT。
select ParseValuesJson('{"' + [key] + '":' + [value] + '}',IsList)
from ParseValuesJson(@json,default)
where IsType = 1
select insertToTable(topKey,[key])
from ParseValuesJson(@json,default)
where IsType = 0
但我想这种方法对您没有帮助,因为在用户定义的函数中您不能使用 INSERT、UPDATE、DELETE 语句 -> 即修改 table 数据
所以我猜想要解析 JSON 您需要使用递归 CTE 来首先解析所有值,然后将它们立即插入到临时 table 中。
像这样:
;WITH ParsedJSON(topKey, [key], IsType, IsList, [value])
AS
(
SELECT topKey, [key], IsType, IsList, [value]
FROM ParseValuesJson('{"' + [key] + '":' + [value] + '}',IsList)
UNION ALL
SELECT topKey, [key], IsType, IsList, [value]
FROM ParsedJSON
WHERE IsType = 1
)
insert @tempTable
select * from ParsedJSON