下面的代码适用于“主要流”或“高级”作为参数,但是我正在尝试使其适用于它们,如下所示,但它不会返回任何结果。如何使下面的代码工作?
declare @myParameter varchar(50)
SET @myParameter = 'Main Stream , Premium'
select * FROM sales
where myCategory IN (@myParameter)
答案 0 :(得分:2)
您可以尝试类似
的内容--Split
DECLARE @textXML XML
DECLARE @data NVARCHAR(MAX),
@delimiter NVARCHAR(5)
SELECT @data = 'Main Stream , Premium',
@delimiter = ','
SELECT @textXML = CAST('<d>' + REPLACE(@data, @delimiter, '</d><d>') + '</d>' AS XML)
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM @textXML.nodes('/d') T(split)
您可以将其存储在临时表中,也可以在IN cluase中使用它。
@Hoy评论
您可以查看nodes() Method (xml Data Type)
当您想要粉碎xml数据类型时,nodes()方法很有用 实例到关系数据。它允许您识别那些节点 将被映射到一个新行。
另外,请查看xml Data Type Methods
然后你可以用它作为
select *
FROM sales
where myCategory IN (
SELECT T.split.value('.', 'nvarchar(max)')
FROM @textXML.nodes('/d') T(split)
)
答案 1 :(得分:2)
尝试:
select id from dbo.split("24,25,26",',');
所以你需要:
select * FROM sales
where myCategory IN ( select id from dbo.split(@myParameter,",");
并运行以下命令在数据库中添加split函数:
Create FUNCTION [Split](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (id varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(id) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
Go
答案 2 :(得分:1)
我为another thread on SO上的类似问题找到了一个很好的解决方案
基本上,它使用一个函数来分割字符串(在您的情况下为@MyParameter
),使用分隔符(在您的情况下为“,
”。)
以下是您调用该函数的方法:
declare @myParameter varchar(50)
SET @myParameter = 'Main Stream,Premium'
--this table will hold delimited values
DECLARE @ParsedTable TABLE
(
[id] [int] NOT NULL,
[content] [nvarchar](50) NULL
)
--Parsing @myParameter
INSERT INTO @ParsedTable
SELECT * from [dbo].[SplitString](@myParameter, ',')
--SELECT * FROM @ParsedTable --This will show you the values...
--run the query
select * FROM sales
where myCategory IN (SELECT content from @ParsedTable)
以下是创建拆分函数的代码:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SplitString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[SplitString]
go
/****** Object: UserDefinedFunction [dbo].[SplitString] Script Date: 11/04/2013 19:04:05 ******/
SET ANSI_NULLS ON
go
SET QUOTED_IDENTIFIER ON
go
CREATE FUNCTION [dbo].[SplitString]
(
-- Add the parameters for the function here
@StringToDelimit nvarchar(500),
@deliminator nvarchar(2)
)
RETURNS
@ReturnTable TABLE
(
-- Add the column definitions for the TABLE variable here
[id] [int] IDENTITY(1,1) NOT NULL,
[content] [nvarchar](50) NULL
)
AS
BEGIN
Declare @delimitorIdx int
Declare @content varchar(50)
--initialize spaces
Select @delimitorIdx = charindex(@deliminator,@StringToDelimit,0)
While @delimitorIdx > 0
Begin
Select @content = substring(@StringToDelimit,0,charindex(@deliminator,@StringToDelimit,0))
Insert Into @ReturnTable(content)
Select @content
Select @StringToDelimit = substring(@StringToDelimit,charindex(@deliminator,@StringToDelimit,0)+ 1,len(@StringToDelimit) - charindex(' ',@StringToDelimit,0))
Select @delimitorIdx = charindex(@deliminator,@StringToDelimit,0)
end
If len(@StringToDelimit) > 0
Insert Into @ReturnTable
Select @StringToDelimit
RETURN
END
go
答案 3 :(得分:0)
declare @myParameter varchar(50)
SET @myParameter = 'Main Stream , Premium'
select * FROM sales
where myCategory IN (select * from string_split(@myParameter,','))