Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. calling stored procedure in case statement - Sql Server 2000

calling stored procedure in case statement - Sql Server 2000

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vamsimohan21
    wrote on last edited by
    #1

    Can any one please tell me how to call a stored procedure from case statement.

    Thanks In Advance

    T 1 Reply Last reply
    0
    • V vamsimohan21

      Can any one please tell me how to call a stored procedure from case statement.

      Thanks In Advance

      T Offline
      T Offline
      The Man from U N C L E
      wrote on last edited by
      #2

      A Case statement in SQL is an embedded part of a Select statement so you can't call a stored procedure in the middle, only a function. However I guess you mean something more like the following:

      If @Test = 1
      Execute [Procedure1];
      Else If @Test = 2
      Execute [Procedure2];
      Else If @Test = 3
      Execute [Procedure3];
      Else
      Execute [Procedure4];

      If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

      P 1 Reply Last reply
      0
      • T The Man from U N C L E

        A Case statement in SQL is an embedded part of a Select statement so you can't call a stored procedure in the middle, only a function. However I guess you mean something more like the following:

        If @Test = 1
        Execute [Procedure1];
        Else If @Test = 2
        Execute [Procedure2];
        Else If @Test = 3
        Execute [Procedure3];
        Else
        Execute [Procedure4];

        If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

        P Offline
        P Offline
        Pranay Rana
        wrote on last edited by
        #3

        you can also use case when for this.

        M T 2 Replies Last reply
        0
        • P Pranay Rana

          you can also use case when for this.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          You are still not going to be able to exec another proc from withing the case statement! Case and Exec() do not go together!

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • P Pranay Rana

            you can also use case when for this.

            T Offline
            T Offline
            The Man from U N C L E
            wrote on last edited by
            #5

            No you can't. To quote the Micosoft documentation[^]: The CASE expression cannot be used to control the flow of execution of Transact-SQL statements, statement blocks, user-defined functions, and stored procedures. For a list of control-of-flow methods, see Control-of-Flow Language (Transact-SQL)[^]. The question was regarding calling stored procedures and that is clearly not possible from within a case statement.

            If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups