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)
 Passing a range of Values in stored procedure

Author  Topic 

omega1983
Starting Member

40 Posts

Posted - 2009-08-17 : 15:42:47
alter procedure school_lookup1
@schoolyear1 char (2) = ''
@schoolyear2 char (2) = ''
@schoolyear3 char (2) = ''
@degreeyear1 char (4) = ''
@degreeyear2 char (4) = ''
@degreeyear3 char (4) = ''
@schoolname1 char (65) = ''
@schoolname2 char (65) = ''
@schoolname3 char (65) = ''
as
select schoolid, schoolyear,schoolname,degreeyear
from school_lookup1
where schoolyear in (@schoolyear1,@schoolyear2,@schoolyear3)
and
degreeyear in (@degreeyear1,@degreeyear2,@degreeyear3)
and
schoolname in (@schoolname1,@schoolname2,@schoolname3)

Is there a way to re-work this stored procedure so that it can capture a range of the forementioned values. For example, the degree years go back to 1900 and I would not want to do parameters for all those years

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-17 : 16:02:12
yes.
have one parameter @schoolyears varchar(8000). It would take values like '71,72,75'
have one parameter @schoolnames varchar(8000). It would take values like 'school1, school2'

use a couple instances of a table valued split function as a table JOINed to your school_lookup1 table.

EDIT:
lookf for this section:
CSV / Splitting delimited lists

in this topic:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210

Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-18 : 02:38:47
Aslo read Sommarskog's article on Arrays in SQL Server.

Madhivanan

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

- Advertisement -