Read + Write + Report
Home | Start a blog | About Orble | FAQ | Sites | Writers | Advertise | My Orble | Login

Running a Dynamic Query against SQL Server without using Dynamic SQL

August 27th 2008 04:37
Running a Dynamic Query against SQL Server without using Dynamic SQL


Problem
I am trying to pass a comma delimited list of values into a stored procedure to limit the result set. Whenever I use the variable in the IN clause I get an error message. Is there a way to do this without using Dynamic SQL?

Solution
There is a way to do this without using Dynamic SQL, but first lets explore the problem. I will be using the AdventureWorks Database in the following examples.

This will work great as long as you only have a single value.

Declare @ManagerIDs Varchar(100)

Set @ManagerIDs = '3'

Select * from HumanResources.Employee
Where ManagerID IN (@ManagerIDs)

But as soon as you add the comma, the results will look something like this.

Declare @ManagerIDs Varchar(100)
Set @ManagerIDs = '3,6'

Select * from HumanResources.Employee
Where ManagerID IN (@ManagerIDs)

Msg 245, Level 16, State 1, Line 4
Conversion failed when converting the varchar value '3,6' to data type int.

This is because SQL Server knows that the ManagerID column is an integer and is trying to implicitly convert the @ManagerIDs variable.

In order to resolve the issue you can execute the statement using Dynamic SQL. This will allow you to build the entire query “dynamically” before executing it.

Declare @ManagerIDs Varchar(100)
Set @ManagerIDs = '3,6'
Declare @SQL Varchar(1000)
Set @SQL =
'Select * from HumanResources.Employee
Where ManagerID IN (' @ManagerIDs ')'

EXEC (@SQL)

This will allow you to execute the query, but Dynamic SQL is a security risk and may not even be allowed in certain organizations.


So how do you execute the query without using Dynamic SQL? This can be accomplished using XML.

The first thing you need to do is create an xml string from the comma delimited string.

Declare @ManagerIDs Varchar(100)
Set @ManagerIDs = '3,6'

DECLARE @XmlStr XML
SET @XmlStr =
--Start Tag
'<ManagerID>'
--Replace all commas with an ending tag and start a new tag
REPLACE( @ManagerIDs, ',', '</ManagerID><ManagerID>')
--End Tag
'</ManagerID>'

Selecting the xml value will display the following.

Select @XmlStr

1. < managerid >3</ managerid > < managerid >6</ managerid >

Now that you have an xml string we can query it and display the results as rows.

SELECT x.ManagerID.value('.', 'INT') AS A
FROM @XmlStr.nodes('//ManagerID') x(ManagerID)

A
1. 3
2. 6
Now you can use the previous query to limit the results.

SELECT *
FROM HumanResources.Employee
WHERE ManagerID IN(
SELECT x.ManagerID.value('.', 'INT') AS A
FROM @XmlStr.nodes('//ManagerID') x(ManagerID)
)

Or you can limit the results by using an Inner Join.

SELECT *
FROM HumanResources.Employee AS A
INNER JOIN
(SELECT x.ManagerID.value('.', 'INT') AS ManagerID
FROM @XmlStr.nodes('//ManagerID') x(ManagerID)) B
ON A.ManagerID = B.ManagerID


63
Vote
Add To: del.icio.us Digg Furl Spurl.net StumbleUpon Yahoo


   
Subscribe to this blog 


Just this blog This blog and DailyOrble (recommended)

   

   

   


Comments
1 Comments. [ Add A Comment ]

Comment by TimmyH

September 5th 2008 19:45
But why would you ever need to do this?

Add A Comment

To create a fully formatted comment please click here.


CLICK HERE TO LOGIN | CLICK HERE TO REGISTER

Name or Orble Tag
Home Page (optional)
Comments
Bold Italic Underline Strikethrough Separator Left Center Right Separator Quote Insert Link Insert Email
Notify me of replies
Notify extra people about this comment
Is this a private comment?
List the Email Addresses or Orble Tags of the people you would like to be notified about this comment


One per line max of 30

List the Email Addresses or Orble Tags of the people you would like to be notified about this private comment thread. Only the people in this list will be able to see or reply to your comment.


One per line max of 30

Your Name
(for the email going out to the above list, it can be different to your Orble Tag)
Your Email Address
(optional)
(required for reply notification)
Submit
More Posts
6 Posts
11 Posts
1 Posts
79 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:
0

Siddharth sood's Blogs

35 Vote(s)
0 Comment(s)
1 Post(s)
75 Vote(s)
0 Comment(s)
2 Post(s)
35 Vote(s)
0 Comment(s)
1 Post(s)
48 Vote(s)
0 Comment(s)
1 Post(s)
27 Vote(s)
0 Comment(s)
1 Post(s)
67 Vote(s)
0 Comment(s)
2 Post(s)
146 Vote(s)
0 Comment(s)
4 Post(s)
193 Vote(s)
2 Comment(s)
5 Post(s)
24 Vote(s)
0 Comment(s)
1 Post(s)
23 Vote(s)
0 Comment(s)
1 Post(s)
Moderated by Siddharth sood
Copyright © 2006 2007 2008 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]