你能帮我吗,我想创建一个程序,允许我发送一个参数把它放在一个IN子句中,如下所示:
CREATE PROCEDURE [dbo].[NamesQry] @Names char(150) AS SELECT * From Mydatabase Where Names in (@Names);
并执行
EXEC [dbo].[IGDMediaSkills] 'Carl,Johnson'
问题是我不知道如何将多参数发送到程序.
解决方法
根据您的技能设置执行此操作的两种方法.
[Microsoft.sqlServer.Server.sqlFunction(Name="fnToList",FillRowMethodName="FillRow",TableDefinition="ID NVARCHAR(1000)")] public static IEnumerable sqlArray(sqlString inputString,sqlString delimiter) { if (string.IsNullOrEmpty(delimiter.Value)) return new string[1] { inputString.Value }; return inputString.Value.Split(delimiter.Value.tocharArray(),StringSplitOptions.RemoveEmptyEntries); } public static void FillRow(object row,out sqlString str) { str = new sqlString((string)row); }
CREATE FUNCTION [dbo].[fnArray] ( @Str varchar(8000),@Delim varchar(1) = ' ' ) returns @tmpTable table ( arrValue varchar(25)) as begin declare @pos integer declare @lastpos integer declare @arrdata varchar(8000) declare @data varchar(25) set @arrdata = replace(replace(replace(replace(upper(@Str),@Delim,'|'),'-',''),'/','\','|') set @arrdata = @arrdata + '|' set @lastpos = 1 set @pos = 0 set @pos = charindex('|',@arrdata) while @pos <= len(@arrdata) and @pos <> 0 begin set @data = substring(@arrdata,@lastpos,(@pos - @lastpos)) if rtrim(ltrim(@data)) > '' begin if not exists( select top 1 arrValue from @tmpTable where arrValue = @data ) begin insert into @tmpTable ( arrValue ) values ( @data ) end end set @lastpos = @pos + 1 set @pos = charindex('|',@arrdata,@lastpos) end return end
然后使用:
SELECT * From Mydatabase Where Names in (select arrValue from dbo.fnArray(@Names,','))