Solved

Passing comma list to Stored Procedure for use with IN()

Posted on 2006-10-19
3
279 Views
Last Modified: 2012-06-27
Hi,

I have code in ASP that passes a comma delimited string parameter to a stored procedure ie: '75,18,92,'. This list can be variable in length.

The stored procedure then needs to use this string with a IN statement ie:
      update datefield where id IN (@parameterList)

The problem occurs as the field 'id' is an integer. I cannot use EXEC @SQLCommand as i dont want to set permisions on the table. ie, the table does not have select,update etc.. permisions. All work is done via stored procedures which have the appropriate permisions.

Esentially, what im looking for is being able to pass the list to the stored procedure and then do the following: Update TableName SET datefield = GETDATE() where id IN ('75,18,92')

Thanks.
0
Comment
Question by:liaminnes
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17764078
please check out here:
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21627393.html


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    


and use it like this:
update datefield where id IN (select value from dbo.ParmsToList(@parameterlist) )

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17764080

EXEC( 'Update TableName SET datefield = GETDATE() where id IN ('+@param+')')
0
 

Author Comment

by:liaminnes
ID: 17764148
Thanks angelIII,

That was exactly what i needed and it works a treat!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now