Solved

SQL Server 2005/2008

Posted on 2011-02-24
4
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 250 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 250 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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

630 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