MS SSQL: How to use case when as exec parameter

后端 未结 1 1839
死守一世寂寞
死守一世寂寞 2021-01-07 12:13

Using SQL Server 2008 R2 I\'m trying to execute a stored procedure using exec with two parameters. The first parameter is an unique identifier. The second parameter can ei

相关标签:
1条回答
  • 2021-01-07 13:10

    I expected it to look something like this:

    exec spAddCaseToDeletedInso @Case, (select case when
    dbo.getCaseOwnerByCaseId(@Case) = 1 then '3' else '2' end)
    

    Your assumption is simply wrong. Check EXECUTE documentation

    Execute a stored procedure or function
    [ { EXEC | EXECUTE } ]
        { 
          [ @return_status = ]
          { module_name [ ;number ] | @module_name_var } 
            [ [ @parameter = ] { value 
                               | @variable [ OUTPUT ] 
                               | [ DEFAULT ] 
                               }
            ]
          [ ,...n ]
          [ WITH RECOMPILE ]
        }
    [;]
    

    As you see you can pass as @parameter:

    • value
    • @variable
    • DEFAULT

    So you need to use variable to hold result of function call and pass it to stored procedure.

    0 讨论(0)
提交回复
热议问题