SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 sring formation with delimiter from table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

135 Posts

Posted - 02/26/2014 :  06:12:09  Show Profile  Reply with Quote
I am having table t1 having column c1 with values v1, v2, v3

I need to form a dynamic query which needs to consider the t1 table's c1 values in where condition.

dynamic sample query:

select name from Table
Where value in (v1,v2,v3).


James K
Flowing Fount of Yak Knowledge

3865 Posts

Posted - 02/26/2014 :  08:15:13  Show Profile  Reply with Quote
Don't quite know what you meant by "dynamic". If you mean that they will come in as parameters, there are a few different ways you could handle it.

One would be to put those values into a table and join on that. For example:
INSERT INTO @Table2 VALUES ('v1'),('v2'),('v3');

SELECT t1.name FROM Table1 t1
INNER JOIN @Table2 t2 ON t1.VALUE = t2.VALUE
If you are getting the values as a string delimited parameter, you can first use a string splitter to split the string into the 3 tokens and use the above approach. Or, you could do something like this:
DECLARE @parameterString VARCHAR(32) = 'v1,v2,v3';
SELECT name FROM Table1
WHERE ','+@parameterString+',' LIKE '%,'+VALUE+',%';
This assumes VALUE is a string column.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000