Solved

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

Posted on 2006-10-19
3
280 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I Need a T-SQL Query (Union or Intersect, or...?) 6 39
SQL Replication question 9 41
SSRS 2013 - Creating a summarized report 19 33
Get the latest status 8 30
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 …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

776 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