sql server - Create string with embedded quotes in SQL -


i run several queries use list of character values in clause, e.g.,

select  *    table1   col1 in ('a','b','c') 

the character list changes frequently, want store string in variable , reference variable in of queries instead of maintaining several copies of string. i've tried following query returns 0 rows.

declare @str varchar(50) select @str = '''a''' + ',' + '''b'''+ ',' + '''c''' select  *    table1   col1 in (@str) 

@str has value 'a','b','c' reason, sql server doesn't recognize it. how build string , store in variable works in keyword?

the in construct in sql set lookup, not string lookup. single string value of "'a','b','c'" it's looking when col1 in (@str)... fredou mentioned in comments.

instead want pass in set of values using table variable (or temp table):

declare @tabin table ( val varchar(10) ) insert @tabin     (val) values     ('a'), ('b'), ('c')  select *     table1             col1 in (select val @tabin) 

or, alternatively, straight join:

declare @tabin table ( val varchar(10) )     insert @tabin         (val) values         ('a'), ('b'), ('c')  select *     table1 t1     join @tabin t2 on         t1.col1 = t2.val 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -