我有一个像这样的字符串:
@Values VARCHAR(1000)
SET @Values = 'one, two, three'
我想在带有WHERE
运算符的IN
子句中使用此字符串。
为此,我正在使用以下表值函数。
ALTER FUNCTION [dbo].[FN_RPT_CommaSeperated]
(@StringInput VARCHAR(8000),
@Delimiter NVARCHAR(1))
RETURNS @OutputTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN
DECLARE @String VARCHAR(10)
WHILE LEN(@StringInput) > 0
BEGIN
SET @String = LEFT(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput) - 1, -1),
LEN(@StringInput)))
SET @StringInput = SUBSTRING(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput), 0),
LEN(@StringInput)) + 1, LEN(@StringInput))
INSERT INTO @OutputTable ([String])
VALUES ('''' + @String + '''')
END
RETURN
END
此函数以预期的方式返回值。但是我的存储过程仍然没有按预期提供数据。
在存储过程中,我这样使用:
WHERE
TD.CaseId IN (SELECT * FROM [FN_RPT_CommaSeperated] (@Values,','))
无论如何,当我在存储过程中对这样的值进行硬编码时,它都会返回预期的输出。
WHERE
TD.CaseId IN ('one', 'two', 'three')
有什么想法如何使用该函数获得输出?
答案 0 :(得分:2)
自SQL Server 2016起,SQL Server中已经内置了一个表值函数。String_Split
WARNING: The conda.compat module is deprecated and will be removed in a future release.
[I 14:11:23.334 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found
[I 14:11:23.343 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret
[I 14:11:24.175 NotebookApp] Serving notebooks from local directory: /home/username
[I 14:11:24.175 NotebookApp] The Jupyter Notebook is running at:
[I 14:11:24.175 NotebookApp] http://localhost:8889/?token=d111b8540568567c80796a3be5cf53229fe38360b411a4dd
[I 14:11:24.175 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 14:11:24.178 NotebookApp]
To access the notebook, open this file in a browser:
file:///run/user/1000/jupyter/nbserver-77790-open.html
Or copy and paste one of these URLs:
http://localhost:8889/?token=d111b8540568567c80796a3be5cf53229fe38360b411a4dd
您可以执行以下操作:
DECLARE @Values VARCHAR(1000)
SET @Values = 'one, two, three'
SELECT value FROM STRING_SPLIT(@Values, ',');
答案 1 :(得分:1)
您已经很近了,但是由于您现在有了一张桌子,因此可以使用类似的内容:
WHERE exists
(select * from [FN_RPT_CommaSeperated] (@Values,',') where [string]=TD.CaseId)
答案 2 :(得分:0)
我在SP中稍微更改了一个函数和WHERE子句以获取预期的输出。很好。
df %>% separate(c1, into =c("c2", "c3"), sep = "(?=[A-Za-z])")
# c2 c3
# 1 5.5 K
# 2 2 M
# 3 3.1 <NA>
# 4 M
在存储过程中这样更改。
ALTER FUNCTION [dbo].[FN_RPT_CommaSeperated]
(@StringInput VARCHAR(8000),
@Delimiter NVARCHAR(1))
RETURNS @OutputTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN
DECLARE @String VARCHAR(10)
WHILE LEN(@StringInput) > 0
BEGIN
SET @String = LEFT(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput) - 1, -1),
LEN(@StringInput)))
SET @StringInput = SUBSTRING(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput), 0),
LEN(@StringInput)) + 1, LEN(@StringInput))
**INSERT INTO @OutputTable ([String])
VALUES (@String)**
END
RETURN
END
答案 3 :(得分:0)
您需要在功能上做些小改动
ALTER FUNCTION [dbo].[FN_RPT_CommaSeperated]
(@StringInput VARCHAR(8000),
@Delimiter NVARCHAR(1))
RETURNS @OutputTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN
DECLARE @String VARCHAR(10)
WHILE LEN(@StringInput) > 0
BEGIN
SET @String = LEFT(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput) - 1, -1),
LEN(@StringInput)))
SET @StringInput = SUBSTRING(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput), 0),
LEN(@StringInput)) + 1, LEN(@StringInput))
INSERT INTO @OutputTable ([String])
VALUES ('' + @String + '')
END
RETURN
END
使用VALUES ('' + @String + '')
代替VALUES ('''' + @String + '''')