Solved

SQL Server 2005/2008

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

685 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