Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Stored procedure declaration

Author  Topic 

daman
Yak Posting Veteran

72 Posts

Posted - 2007-11-19 : 18:49:16
I'd like to get input from helpful members here about the stored procedures I'm writing.


1) Stored procedure that takes either ID or Name as input so
I can call EXEC usp_get_values '1234' or EXEC usp_get_values 'John Smith'

CREATE PROCEDURE [dbo].[usp_get_values]
@Id INT = null,
@Name VARCHAR(100) = null

As
Select value from Table where Id=@Id or Name=@Name


The way it is written now, I have to enter both

2) Stored procedure that takes an array as input

My input will be an array of id. How do I declare my SP ?

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-11-19 : 22:53:19
Exec [dbo].[usp_get_values] @ID = 1
Exec [dbo].[usp_get_values] @Name = 'john'


In sql there is really no way to pass an array. A work around is to add a table to sql that has all your id and name values in it, then code the store procedure to referance the table for the array list.

Another alternative is you can always pass a string containing the sql, but that is not very safe.

i.e.

declare @strSql varchar(200),@Name varchar(200),@ID varchar(200)

set @strSql = 'Select value from Table'
set @name = '''John'',''Joe'',''tom'''
set @ID = '1,2,3,4,5'

exec (@StrSql + ' Where ID in (' + @ID +') and Name in (' + @Name + ')')



Go to Top of Page

daman
Yak Posting Veteran

72 Posts

Posted - 2007-11-19 : 23:10:34
hi Vinnie,
thanks for the input.
i read somewhere that you can use xml input in sql 2005 to read in a whole list at once.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-20 : 01:35:56
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -