Solved

passing multiple int parameter to stored procedure

Posted on 2004-03-30
5
1,393 Views
Last Modified: 2007-12-19
Is there an easy way to pass multiple ints in as a parameter?  

For example, the where clause will be:  where id = 12345 or 23456 or 45678

I want it to read where id = @p1

Thanks

0
Comment
Question by:LATurk
  • 2
  • 2
5 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 10717290
pass the ints as a comma delimeted parameter

e.g.

exec  yourproc '123,456,789'

then inside

either use dynamic sql (not preferred)

declare @sqlstr varchar(8000)
set @sqlstr = 'Select ....  where id in (' + @parm1 + ')'
exec (@SQLSTR)


or decode the string into a temp table/table variable
and join to it..

e.g.

declare @i int
declare @next int
set @i = 1

select convert(int,0) as [id] into #temp where 0=1

while charindex(',',@parm1,@i) > 0
begin
      Set @next = charindex(',',@parm1,@i)
      Insert into #temp select convert(int,substring(@parm1,@i,@next-1))
      set @i=@next+1
end

insert into #temp select convert(int,substring(@parm1,@i,datalength(@parm1) +1 - @i))

select a.*
from xyz as a
 inner join #temp as t
 on a.[id] = t.[id]

 
0
 
LVL 3

Expert Comment

by:debi_mela
ID: 10717431
Inside the store procdure...

create procedure pname(declare @x as varchar(100))
declare @q as nvarchar(500)
--set @x = '(0,1,2,3,4)'  /* this is an eg: assign your input values like these, @x is the input parameter */
set @q = 'select * from tblname where field in' + @x
EXECUTE sp_executesql @q
0
 

Author Comment

by:LATurk
ID: 10726285
I'm having problems with this.  If I run the stored procedure below in Query Analyzer passing two numbers like this:

myProcedure '159563,159562'
it works.

When I add one more number to the string as in this:

myProcedure '159563,159562,159564'

I get the following error:

Server: Msg 245, Level 16, State 1, Procedure Loribtreport, Line 13
Syntax error converting the varchar value '159562,159564' to a column of data type int.


CREATE PROCEDURE myProcedure (@p1 varchar(500)) as

declare @i int
declare @next int
set @i = 1

Select convert(int,0) as [id] into #temp where 0=1

while charindex(',',@p1,@i) > 0

begin
      Set @next = charindex(',',@p1,@i)
      Insert into #temp select convert(int,substring(@p1,@i,@next - 1))
      Set @i=@next + 1
end

insert into #temp select convert(int,substring(@p1, @i, datalength(@p1) + 1 - @i))

select * from #temp
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 10726422
sorry

try this

CREATE PROCEDURE myProcedure @p1 varchar(500) as

set nocount on
declare @i int

declare @next int
set @i = 1

Select convert(int,0) as [id] into #temp where 0=1

set @next = 1
while @next>0

begin
     Set @next = charindex(',',@p1,@i)
/*
print @p1
print @i
print @next
*/
     if @next > 0
     begin
     Insert into #temp select convert(int,substring(@p1,@i,@next - @i))
     Set @i=@next + 1
     end
end

insert into #temp select convert(int,substring(@p1, @i, datalength(@p1) - @i + 1))

select * from #temp


return
go
exec myProcedure '159563,159562,159564'
drop procedure myprocedure
0
 

Author Comment

by:LATurk
ID: 10726665
Thank you so much for your help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
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

830 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