Solved

SQL Server 2005/2008

Posted on 2011-02-24
4
297 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
Comment Utility
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:
ewangoya earned 250 total points
Comment Utility

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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article I will describe the Backup & Restore 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now