Solved

Send Parameter to SQL Server as delimeted argument for IN

Posted on 2007-03-27
3
181 Views
Last Modified: 2010-04-23
Is it possible to send a string to sql server as a parameter for an IN sql statement.

Basically I am building the string as

'product1','product2','product3','product4'

The stored proc will be something like:

CREATE PROCEDURE dbo.GetProducts
(
@Products AS nvarchar(200)
)
AS
SELECT *
FROM Products
WHERE ProductID IN(@Products)
GO

Is this valid? if not what should the string format be if it is possible?

Wing
0
Comment
Question by:WingYip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 25 total points
ID: 18798556
You cannot do this without using dynamic sql or a more complicated method involving xml as a string.

The easiest way is this:

CREATE PROCEDURE dbo.GetProducts
(
@Products AS nvarchar(200)
)
AS
Declare @Sql nvarchar(4000)
Set @Sql = 'SELECT * FROM Products WHERE ProductID IN(' + @Products +')'
Exec (@sql)
GO
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 18798557
create the following function:


CREATE FUNCTION dbo.ParmsToList (@Parameters varchar(500))
returns @result TABLE (Value varchar(30))
AS  
begin
     DECLARE @TempList table
          (
          Value varchar(30)
          )

     DECLARE @Value varchar(30), @Pos int

     SET @Parameters = LTRIM(RTRIM(@Parameters))+ ','
     SET @Pos = CHARINDEX(',', @Parameters, 1)

     IF REPLACE(@Parameters, ',', '') <> ''
     BEGIN
          WHILE @Pos > 0
          BEGIN
               SET @Value = LTRIM(RTRIM(LEFT(@Parameters, @Pos - 1)))
               IF @Value <> ''
               BEGIN
                    INSERT INTO @TempList (Value) VALUES (@Value) --Use Appropriate conversion
               END
               SET @Parameters = RIGHT(@Parameters, LEN(@Parameters) - @Pos)
               SET @Pos = CHARINDEX(',', @Parameters, 1)

          END
     END    
     INSERT @result
     SELECT value
        FROM @TempList
     RETURN
END    


then, change your procedure:

CREATE PROCEDURE dbo.GetProducts
(
@Products AS nvarchar(200)
)
AS
SELECT *
FROM Products
WHERE ProductID IN( select value from dbo.ParmstoList(@Products))
GO

and finally, pass the value in your code without quotes in the middle, like this
exec dbo.GetProducts 'product1,product2,product3,product4'

0
 
LVL 1

Author Comment

by:WingYip
ID: 18804944
Thanks alot.  Ended up using angelII suggestion but both answers appreciated.  Much obliged to both of you.

Thanks

Wing
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question