Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I write this stored procedure...

Status
Not open for further replies.

nc297

Programmer
Apr 7, 2010
162
US
create procedure GETNames
@rpt char(1)
as

if rpt = '1', '3', '4', '5'

then

SELECT FirstName, LastName, JobTitle, Department
FROM HumanResources
WHERE rpt=@rpt

else if rpt='2'

SELECT FirstName, LastName, JobTitle, Department
EmployeeDepartment
WHERE rpt='2'

end if

I want rpt='2' to have it's own select statement as different data needs to appear.



 
I would probably write it like this:

Code:
create procedure GETNames
@rpt char(1)
as

if rpt [!]In([/!]'1', '3', '4', '5'[!])[/!]

  SELECT FirstName, LastName, JobTitle, Department
    FROM HumanResources
    WHERE rpt=@rpt

else if rpt='2'

  SELECT FirstName, LastName, JobTitle, Department
    EmployeeDepartment
    WHERE rpt='2'

Although, it's probably better this way:

Code:
create procedure GETNames
@rpt char(1)
as

if rpt = '2'
  SELECT FirstName, LastName, JobTitle, Department
    EmployeeDepartment
    WHERE rpt='2'
Else

  SELECT FirstName, LastName, JobTitle, Department
    FROM HumanResources
    WHERE rpt=@rpt

In the first example, if you add another report type, you would have to modify the code. In the second example, you wouldn't (unless you also wanted special handling for it).


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks I will use the second procedure as rpt='2' is the only report that will have special handling

I will try it in the morning thanks!
 
Thanks this worked but I'm stuck on another problem so I opened a new thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top