Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query quesiton Expand / Collapse
Author
Message
Posted Tuesday, March 06, 2012 12:47 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 5:26 AM
Points: 2,797, Visits: 1,745
how can i achieve this?
Declare @p1 = 'xyz,tln,tprq'

select * from table1 where col1 in('xyz','tln','tprq')
Post #1261985
Posted Tuesday, March 06, 2012 1:51 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 3:43 AM
Points: 4,804, Visits: 8,091
Use a split function: http://www.sqlservercentral.com/articles/Tally+Table/72993/


Get your two-cent-answer quickly
The Spaghetti DBA
Post #1261997
Posted Tuesday, March 06, 2012 2:19 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, June 14, 2013 8:33 AM
Points: 1,402, Visits: 6,950
As Gianluca has shown, one way to do this is to split @P1, because it is no use in it's current format as a parameter.
Col1 would have to have a value of 'xyz,tln,tprq' in one row.

Another option is to use Dynamic SQL, but this would require a change in the format of @P1 - this may be an option for you.
Pay attention to the number of single quotes required in the value of @P1:

CREATE TABLE #Test1(
Test_Text VarChar(20)
);

INSERT INTO #Test1(Test_Text)
VALUES('abc'),
('cde'),
('fgh'),
('xyz'),
('123'),
('tln'),
('456'),
('tprq');

DECLARE @P1 VarChar(30);

SET @P1 = '''xyz'''+','+'''tln'''+','+'''tprq''';

SELECT @P1;

EXEC(N'SELECT * FROM #Test1 WHERE Test_Text IN (' + @P1 + ')');



BrainDonor
Linkedin
Post #1262002
Posted Friday, March 09, 2012 2:31 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 5:26 AM
Points: 2,797, Visits: 1,745
create table #t1
(id varchar(5))

insert into #t1 values('xyz'), ('kkl'), ('tln'), ('cxz'), ('dfa'), ('tprq')

select * from #t1

Declare @p1 varchar(50) = 'xyz,tln,tprq'
Declare @p2 varchar(50) = ''''+replace(@p1, ',', ''',''')+''''
select @p1
select @p2

select * from #t1 where id in(@p2)

Why teh last select does not return any value inspite of @p2 having the correct presentation of condition ?
Post #1264190
Posted Friday, March 09, 2012 2:42 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Tuesday, June 04, 2013 7:03 AM
Points: 4,443, Visits: 7,249
Because, as Gianluca and BrainDonor have already mentioned, you need to use a split function or dynamic SQL. Make sure you understand the implications of the latter.

John
Post #1264192
Posted Friday, March 09, 2012 2:46 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, June 14, 2013 8:33 AM
Points: 1,402, Visits: 6,950
Because you need to run it as Dynamic SQL, using the EXEC command:

EXEC(N'SELECT * FROM #Test1 WHERE Test_Text IN (' + @P1 + ')');

What you have is SQL probably attempting to find a match to the entire varchar ''xyz','tln','tprq'', not three seperate values.



BrainDonor
Linkedin
Post #1264194
Posted Friday, March 09, 2012 3:04 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 5:26 AM
Points: 2,797, Visits: 1,745
I agree but why does not it works like this ? I mean is there something more precise or some doc where I can get deep understanding on this ?
Post #1264202
Posted Friday, March 09, 2012 3:39 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:08 PM
Points: 38,099, Visits: 30,392
Because select * from #t1 where id in(@p2) is completely and totally equivalent to select * from #t1 where id = @p2. Each item in an IN (variable, parameter or constant) is considered to be a single value

To specify variables in an IN, it would be like this: select * from #t1 where id in(@p1, @p2, @p3, @p4)



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1264217
Posted Friday, March 09, 2012 5:23 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 5:26 AM
Points: 2,797, Visits: 1,745
Thanks Gail, I got it from your point. Thanks to others as well for describing.
Post #1264271
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse