这段代码工作正常,但我的SQL2005兼容级别为80,所以我不能使用它。
我应该用OUTER APPLY
替换t.Parameters
列作为参数来运行?
DECLARE @task TABLE
(
ActionTaskId BIGINT,
[Parameters] VARCHAR(512)
)
INSERT INTO @task
SELECT 1, '{"A":"#FFFFFF","B":"#000000"}' UNION
SELECT 2, '{"A":"#EEEEEE","B":"#000000"}'
SELECT *, OA1.Val
FROM @task AS t
OUTER APPLY (SELECT * FROM [dbo].[parseJSON](t.[Parameters]) AS pj WHERE pj.Name='A') OA1
必需的输出:
ActionTaskId | Val
----------------
1 | #FFFFFF
2 | #EEEEEE
答案 0 :(得分:0)
SELECT *
, (
SELECT MAX (pj.Val)
FROM [dbo].[parseJSON](t.[Parameters]) AS pj
WHERE pj.Name='A'
)
FROM @task AS t
顺便说一句,你能展示一下parseJSON函数里面的内容吗?很有意思。
答案 1 :(得分:0)
如果您的数据量很少,则有解决方法(效率不高):
SELECT *
, 'A' = [dbo].[fsParseJSON](t.[Parameters], 'A')
, 'B' = [dbo].[fsParseJSON](t.[Parameters], 'B')
FROM @task AS t
标量函数的样子:
CREATE FUNCTION fsParseJSON
(
@Json varchar(4000), @Name VARCHAR(128)
)
RETURNS varchar(128)
AS
BEGIN
DECLARE @Result varchar(128)
SELECT @Result = [pj].[StringValue]
FROM [dbo].[parseJSON](@Json) AS pj
WHERE [pj].[Name] = @Name
RETURN @Result
END
GO