how to write an if statement in stored procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanika1507
    New Member
    • Sep 2007
    • 34

    how to write an if statement in stored procedure

    hi .
    Actually i have a statement If @Stat in (20,30) i want to
    to modify it as
    if @Stat = 20 set Status="Rejecte d"
    if @Stat = 30 set Status=" Accepted"

    Can you please help me out here .
    i have tried but i am getting an error...

    Server: Msg 170, Level 15, State 1, Line 4
    Line 4: Incorrect syntax near '='.
    Server: Msg 170, Level 15, State 1, Line 5
    Line 5: Incorrect syntax near '='.

    thanks in advance
    Last edited by sanika1507; Sep 28 '07, 03:55 PM. Reason: written a bit wrongly
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by sanika1507
    hi .
    Actually i have a statement If @Stat in (20,30) i want to
    to modify it as
    if @Stat = 20 set Status="Rejecte d"
    if @Stat = 30 set Status=" Accepted"

    Can you please help me out here .
    i have tried but i am getting an error...

    Server: Msg 170, Level 15, State 1, Line 4
    Line 4: Incorrect syntax near '='.
    Server: Msg 170, Level 15, State 1, Line 5
    Line 5: Incorrect syntax near '='.

    thanks in advance
    are you trying to store the string "Rejected" to a variable? if yes, try:

    Code:
    if  @Stat  = 20   set @Status='Rejected'
    if  @Stat = 30    set @Status='Accepted'
    if you're trying to update a table and Status is a column, you have to do a

    Code:
    if  @Stat  = 20   
    update mytable
    set Status='Rejected'

    Comment

    • sanika1507
      New Member
      • Sep 2007
      • 34

      #3
      Originally posted by ck9663
      are you trying to store the string "Rejected" to a variable? if yes, try:

      Code:
      if  @Stat  = 20   set @Status='Rejected'
      if  @Stat = 30    set @Status='Accepted'
      if you're trying to update a table and Status is a column, you have to do a

      Code:
      if  @Stat  = 20   
      update mytable
      set Status='Rejected'

      thanks for your help . but the status is not a variable .its a column name in a table.........

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Originally posted by sanika1507
        thanks for your help . but the status is not a variable .its a column name in a table.........
        if you're trying to replace the value of Status based on the value of the variable @stat, then the second one should do it. if stat and status are on the same table, then you have to do something like...

        Code:
        update myTable
        set Status = case when stat = '20' then 'Rejected' when stat = '30' then Accepted end

        Comment

        Working...