Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

passing multiple int parameter to stored procedure

Posted on 2004-03-30
5
Medium Priority
?
1,414 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
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 …
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
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

916 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