tsql - C# T-SQL create IN with array of strings -


maybe i'm not doing correctly, need create list of strings filter sql select query , can find answers escape single parameter value only.

given array :

string[] domains = new string[] { "sec", "krn", "op", "ip" };

i need create query

select * user_domains name in ("sec", "krn", "op", "ip")

note : not real query, illustrate point of question.

a naïve solution create query as

select * user_domains name in (@p1, @p2, @p3, @p4)

and executing query params formatted as

dictionary<string,string> interms = domains      .select((t, i) => new { key = "p" + i, term = t })      .todictionary(item => item.key, item => item.term,                    stringcomparer.ordinalignorecase); 

is there approach?

update

would easy and faster perform simple string comparison (aka index of) comma, or otherwise, separated string of terms?

ex:

select * user_domains charindex(name, @terms) > 0

i using function

create function dbo.fnlisttocol (     @sinputlist varchar(8000) -- list of delimited items   , @sdelimiter varchar(2) = '|' -- delimiter separates items ) returns @list table (item varchar(8000))  begin declare @sitem varchar(8000) while charindex(@sdelimiter,@sinputlist,0) <> 0  begin  select   @sitem=rtrim(ltrim(substring(@sinputlist,1,charindex(@sdelimiter,@sinputlist,0)-1))),   @sinputlist=rtrim(ltrim(substring(@sinputlist,charindex(@sdelimiter,@sinputlist,0)+len(@sdelimiter),len(@sinputlist))))   if len(@sitem) > 0   insert @list select @sitem  end  if len(@sinputlist) > 0  insert @list select @sinputlist -- put last item in return end go 

then build delimited parameter ( e.g "sec|krn" in code , pass procedure so:

where ( ( vssas.location in ( select *  dbo.fnlisttocol(@locationfilter, '|') ) ) or ( @locationfilter null ) ) 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -