• subscribe
July 24, 2003 12:00 AM

Using a Stored Procedure in a CASE Expression

SQL Server Pro
InstantDoc ID #39477
Downloads
39477.zip

I'm trying to create a query that executes a stored procedure as one part of a CASE expression that has a mix of handlers (i.e., the CASE expression needs to handle multiple inputs). The stored procedure works on its own, but when I put it into the CASE expression, it fails. What's happening?

You can't execute a stored procedure from a CASE expression. A CASE expression evaluates a list of conditions and returns the result that meets the criteria. However, you can use a CASE expression to evaluate the handlers' values and return a character string that points to the stored procedure you want to execute, as the code in Listing 1 shows. Then, you can pass the character string to an EXECUTE command that dynamically executes the stored procedure.

In the example code that Listing 1 shows, the objective is to examine the value of @state at runtime. The code then executes the appropriate stored procedure, which is related to the particular state value.



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...