Connecting Tech Pros Worldwide Forums | Help | Site Map

if then else in stored procedure

Member
 
Join Date: Mar 2007
Posts: 52
#1: Apr 28 '08
There is a piece of code from stored procedure.

Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2.  
  3. SET NOCOUNT ON;
  4.     SELECT 
  5.       pi.*
  6. FROM rates pi
  7. WHERE
  8. (pi.pid = @pid or @pid is NULL)     
  9. AND
  10. (pi.scode = @scode or @scode is NULL)
  11.  
  12. END
I want to put a condition in this:
If scode = 'p' then
where condition must be like this:
WHERE
(pi.pid = @pid or @pid is NULL)
AND
((pi.scode = @scode and pi.scode ='p' ) or @scode is NULL)

Please tell me how to modify the code?
thks

ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Apr 28 '08

re: if then else in stored procedure


Quote:

Originally Posted by Garima12

There is a piece of code from stored procedure.

Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2.  
  3. SET NOCOUNT ON;
  4.     SELECT 
  5.       pi.*
  6. FROM rates pi
  7. WHERE
  8. (pi.pid = @pid or @pid is NULL)     
  9. AND
  10. (pi.scode = @scode or @scode is NULL)
  11.  
  12. END
I want to put a condition in this:
If scode = 'p' then
where condition must be like this:
WHERE
(pi.pid = @pid or @pid is NULL)
AND
((pi.scode = @scode and pi.scode ='p' ) or @scode is NULL)

Please tell me how to modify the code?
thks

On this part:

If scode = 'p'

Is scode a variable or a column?

-- CK
Reply