Solved

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

Posted on 2006-10-19
3
283 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
[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 143

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

737 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