Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Send Parameter to SQL Server as delimeted argument for IN

Posted on 2007-03-27
3
Medium Priority
?
185 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 100 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 400 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

704 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