Results 1 to 11 of 11

Thread: [Resolved] CASE and SPROCS

  1. #1

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Resolved [Resolved] CASE and SPROCS

    I am trying to do this and that damn thing won't let me

    VB Code:
    1. select @Something
    2.    case SomethingElse
    3.       when 2 then proc_AProc2
    4.       when 3 then proc_AProc3
    5.    end

    Is there something endemically wrong in this? I can't use a UDF because the proc has a transaction in it. I can't lift the transaction outside, because other places use the sproc on its own.

    Is it just a syntactic balls-up on my part?

    [edit] I am using SQL Server 2000 [/edit]
    Last edited by yrwyddfa; Dec 22nd, 2006 at 06:09 AM.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE and SPROCS

    Take a look at this LINK
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: CASE and SPROCS

    Quote Originally Posted by Mark Gambo
    Take a look at this LINK
    An interesting link, for sure, but it doesn't resolve my problem.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  4. #4
    Addicted Member
    Join Date
    Oct 2004
    Location
    Clane, Ireland
    Posts
    179

    Re: CASE and SPROCS

    The code you posted, is it VB or SQL Server?

    Have you tried removing the second line i.e. CASE SomethingElse ?

    If SQL SERVER, should you not say EXEC procedure?
    JP

    Please rate the postings

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: CASE and SPROCS

    I think Marks pointing you at Steve's post about needing to use an if instead of a Select.

    In TSQL a select can be used as part of a SQL statement but not as a logic branching statement, you need to use Ifs for that.

  6. #6
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE and SPROCS

    Quote Originally Posted by FunkyDexter
    I think Marks pointing you at Steve's post about needing to use an if instead of a Select.

    In TSQL a select can be used as part of a SQL statement but not as a logic branching statement, you need to use Ifs for that.
    Exactly, I didn't have a lot of time to elaborate when I posted earlier.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: CASE and SPROCS

    All of the procs I am using return a single value; are you saying that I cannot use a SPROC in this way with a CASE statement and have to use if/then's? I have about 20 conditions that go in this CASE statement and that's going to be awful mess if that's the case . . .
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: CASE and SPROCS

    If-blocks are really your only answer...

    But what does:

    Code:
    select @Something
       case SomethingElse
          when 2 then proc_AProc2
          when 3 then proc_AProc3
       end
    this really mean? You want to EXECUTE the PROC_APROC2 or PROC_APROC3 based on some condition?

    Code:
    If x=y Exec proc_AProc2
    If x=z Exec proc_AProc3
    .
    .
    .
    Don't worry about the fact that the IF's are all checked - that's the least of the speed issue of a query...

    If you find that there is danger put lots of ELSE statements:

    Code:
    If x=y Exec proc_AProc2
    Else   If x=z Exec proc_AProc3
           Else If x=z and x=y Exec proc_AProc4
    .
    .
    .
    or use GOTO - it's truly allowed in T-SQL and unavoidable...

    Code:
    If x=y
    Begin
       Exec Proc_AProc2
       Goto Bottom
    End
    
    If x=z
    Begin
       Exec Proc_AProc2
       Goto Bottom
    End
    
    If x=y and x=z
    Begin
       Exec Proc_AProc2
       Goto Bottom
    End
    
    Bottom:

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    Addicted Member mabbas110's Avatar
    Join Date
    Oct 2005
    Location
    Karachi , Pakistan
    Posts
    172

    Re: CASE and SPROCS

    I can not understand what u r trying to do. You are subistuting the proc name in the @something so that to run afterwards if this is the case then
    select @Something
    case SomethingElse
    when 2 then "proc_AProc2"
    when 3 then "proc_AProc3"
    end
    but i think so that its sql server code so it would be

    select @Something
    case SomethingElse
    when 2 then exec proc_AProc2
    when 3 then exec proc_AProc3
    end
    or

    select @Something
    case SomethingElse
    when 2 then 'proc_AProc2'
    when 3 then 'proc_AProc3'
    end

    exec @something
    Thanks and Regards,

    Muhammad Abbas

  10. #10

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: CASE and SPROCS

    Cheers, Szlamany.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  11. #11
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE and SPROCS

    Don't forget to mark this thread as resolved.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width