Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

subhaoviya
Posting Yak Master

India
135 Posts

Posted - 02/26/2014 :  06:12:09  Show Profile  Reply with Quote
Hi,
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).

Thanks
Subha

James K
Flowing Fount of Yak Knowledge

3873 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:
DECLARE @Table2 TABLE(VALUE VARCHAR(32));
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  
 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.13 seconds. Powered By: Snitz Forums 2000