SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


In clause is giving problem in dynamic sql.


In clause is giving problem in dynamic sql.

Author
Message
abhas
abhas
SSC-Addicted
SSC-Addicted (478 reputation)SSC-Addicted (478 reputation)SSC-Addicted (478 reputation)SSC-Addicted (478 reputation)SSC-Addicted (478 reputation)SSC-Addicted (478 reputation)SSC-Addicted (478 reputation)SSC-Addicted (478 reputation)

Group: General Forum Members
Points: 478 Visits: 664
Hi All,

I am trying to run below simple query. I am trying to pass comma separated string into my query but its not working. if i pass single value then it works fine. Pklease guide.

Declare @DayName varchar(100)
--set @DayName = 'Monday'
Set @DayName = '''Monday''' +','+ '''Sunday''' +','+' ''Friday'''
select * from tbldays where weekday in(@DayName)
select @DayName


thanks
Abhas.
Orlando Colamatteo
Orlando Colamatteo
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15049 Visits: 14396
abhas (5/3/2013)
Hi All,

I am trying to run below simple query. I am trying to pass comma separated string into my query but its not working. if i pass single value then it works fine. Pklease guide.

Declare @DayName varchar(100)
--set @DayName = 'Monday'
Set @DayName = '''Monday''' +','+ '''Sunday''' +','+' ''Friday'''
select * from tbldays where weekday in(@DayName)
select @DayName


thanks
Abhas.

SQL is a declarative language, not an interpreted language so when your code runs here is what is actually run:

select * from tbldays where weekday in('''Monday'',''Sunday'',''Friday''')



Which obviously will return no results.

Is this for code that will run within a stored procedure? If so, then look into passing an XML Document to the stored procedure instead of a delimited string. Then you can shred the XML document with normal T-SQL commands and join to it from your existing query.

If you still want to use a delimited string because XML is not your strength or because of client limitations there are string splitting functions available that will turn your delimited string into a table that you can join to in your query. Here is a good start with a T-SQL and SQLCLR option: http://www.sqlservercentral.com/articles/Tally+Table/72993/

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)

Group: General Forum Members
Points: 843 Visits: 1721
Something like this is what you need. The splitter function allows you to do a set-based join to get your query results.



--sample date table

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable

CREATE TABLE #TempTable (
[ID] INT IDENTITY(1,1) NOT NULL,
[WeekDay] NVARCHAR(50) NULL,
PRIMARY KEY (ID))

INSERT INTO #TempTable
SELECT 'Monday' UNION ALL
SELECT 'Tuesday' UNION ALL
SELECT 'Wednesday' UNION ALL
SELECT 'Thursday' UNION ALL
SELECT 'Friday'






DECLARE @DayName VARCHAR(100)
SET @DayName = 'Monday,Sunday,Friday'

SELECT
[WeekDay]
FROM
#TempTable AS tt
INNER JOIN
dbo.DelimitedSplit8K(@DayName,',') AS dsk
ON tt.WeekDay = dsk.Item




Output:



WeekDay
Monday
Friday




 
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search