Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server 2005/2008

Posted on 2011-02-24
4
Medium Priority
?
308 Views
Last Modified: 2012-05-11
I get Syntax error
Msg 156, Level 15, State 1, Procedure AAA, Line 45
Incorrect syntax near the keyword 'Case'.
Msg 156, Level 15, State 1, Procedure AAA, Line 48
Incorrect syntax near the keyword 'Else'.
Msg 102, Level 15, State 1, Procedure AAA, Line 53
Incorrect syntax near 'End'.

Can't we execute SP in Case statement under IF Statement???


Create Procedure [dbo].[AAA] 
	@StoredProcName VarChar(100), 
	@TermID INT = Null, 
	@User VarChar(40) = Null, 
	@Action VarChar(100) = Null


AS

  SET NOCOUNT ON


	If exists(Select * from Clients..Licenses 
				where [Name] like 'Clare%')
		Begin
			Set @StoredProcName = @StoredProcName + '_192'
			
			Case When @StoredProcName = 'AAA_192' then 
					Execute @StoredProcName @User,@Action 
				When @StoredProcName = 'AAA1_192' then Execute @StoredProcName 
			Else Execute @StoredProcName @TermID,@User,@Action 
			End

		End			

GO

Open in new window

0
Comment
Question by:onebite2
4 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34975713
change to :


 Set @StoredProcName = @StoredProcName + '_192'
                         
                        Case @StoredProcName
                                when 'AAA_192' then  Execute @StoredProcName @User,@Action  
                                When 'AAA1_192' then Execute @StoredProcName  
                        Else Execute @StoredProcName @TermID,@User,@Action  
                        End
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 1000 total points
ID: 34975787

Create Procedure [dbo].[AAA]
      @StoredProcName VarChar(100),
      @TermID INT = Null,
      @User VarChar(40) = Null,
      @Action VarChar(100) = Null


AS

  SET NOCOUNT ON


      If exists(Select * from Clients..Licenses where [Name] like 'Clare%')
      BEGIN
            SET @STOREDPROCNAME = @STOREDPROCNAME + '_192'
                  
            IF @STOREDPROCNAME = 'AAA_192'
                  EXECUTE @STOREDPROCNAME @USER,@ACTION
            ELSE IF @STOREDPROCNAME = 'AAA1_192'  
                EXECUTE @STOREDPROCNAME
            ELSE
                EXECUTE @STOREDPROCNAME @TERMID,@USER,@ACTION
      END
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 1000 total points
ID: 34976167
you would need to use dynamic sql....

you can't execute a "variable" like that...


what you are attempting could be quite dangerous (security/sql injection etc..)

you could do...

Declare @execcmd varchar(max)
Set @execcmd='exec '+@StoredProcName + '_192'
                  +Case When @StoredProcName = 'AAA_192'
                           then      '  '''+@User+''','+coalesce(''''+@Action+'''','')
                            When @StoredProcName = 'AAA1_192'
                                   then ''
                             Else ' '+convert(varchar(10,@TermID)+',''''+@User+''','+coalesce(''''+@Action+'''','')  
                  End
print(@execcmd)  -- for debug  comment out when satisfied....
Exec(@execcmd)


you need to convert the parameters to character  to embed in the command string
0
 

Author Closing Comment

by:onebite2
ID: 34979896
I know i could IF conditional statement, i was looking for some dynamic execution something like lowfat suggested with no sql injection.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question