Solved

SQL Server 2005/2008

Posted on 2011-02-24
4
303 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
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.
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.

710 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