Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we use CASE with EXEC

I want to select a stored proc to execute based on user input. Something like -

EXEC
CASE @InputParam 
  WHEN 'XML' THEN GetXMLData @ID, 'y'
  WHEN 'TABLE' THEN GetTableData @ID, 'y'
END

Can this be done with CASE or should I consider using the If construct?

like image 566
neuDev33 Avatar asked Apr 13 '12 15:04

neuDev33


2 Answers

You want to use the IF construct here:

IF @InputParam = 'XML'
    EXEC GetXMLData @ID, 'y'
IF @InputParam = 'TABLE'
    EXEC GetTableData @ID, 'y'
like image 72
JNK Avatar answered Oct 07 '22 01:10

JNK


In this scenario I think that even if SQL Server allowed that, an IF would be more clear.

IF @InputParam = 'XML'
BEGIN
    exec GetXMLData @ID, 'y'
END
ELSE IF @InputParam = 'TABLE'
BEGIN
    exec GetTableData @ID, 'y'
END
like image 39
Jeremy Pridemore Avatar answered Oct 07 '22 01:10

Jeremy Pridemore