SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


case statement in stored procedure


case statement in stored procedure

Author
Message
Betty Sun
Betty Sun
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1212 Visits: 54
Hi all,

I have a stored procedure. But it seems that you cannot use variable for a table. variables are only can be used in condition expression? What about case statement.
I keep getting the following message:

Msg 156, Level 15, State 1, Procedure ExtractDataforProducer, Line 5
Incorrect syntax near the keyword 'Case'.
Msg 1087, Level 15, State 2, Procedure ExtractDataforProducer, Line 7
Must declare the table variable "@TableName".
Msg 1087, Level 15, State 2, Procedure ExtractDataforProducer, Line 13
Must declare the table variable "@TableName".


//////////////////////////////////

Create proc ExtractDataforProducer (@StartDate Datetime, @EndDate Datetime, @SubProducerFlag char(1), @TableName varchar(15), @AgentCode varchar(10))
As


Case @SubProducerFlag
When 'Y' Then
Select * from @TableName tn
inner join tis_vs vs on tn.pnref=vs.pnref
and agentCode like @AgentCode
and convert(char(10), vs.txndatetime,126) between convert(char(10),@StartDate,126) and convert(char(10), @EndDate, 126)
order by vs.txndatetime
Else
Select * from @TableName tn
inner join tis_vs vs on tn.pnref=vs.pnref
and agentCode=@AgentCode
and convert(char(10), vs.txndatetime,126) between convert(char(10),@StartDate,126) and convert(char(10), @EndDate, 126)
order by vs.txndatetime
End

Go
Betty Sun
Betty Sun
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1212 Visits: 54
Hi all,

I rewrite to another one.

Create proc ExtractDataforProducer (@StartDate Datetime, @EndDate Datetime, @SubProducerFlag char(1), @TableName varchar(15), @AgentCode varchar(10))
As
Select * from @TableName tn
inner join tis_vs vs on tn.pnref=vs.pnref
and agentCode

Case When @SubProducerFlag='Y'
Then like End,
Case When @SubProducerFlag !='Y'
Then = End
@AgentCode
and convert(char(10), vs.txndatetime,126) between convert(char(10),@StartDate,126) and convert(char(10), @EndDate, 126)
order by vs.txndatetime

But I still get an error message:
Msg 1087, Level 15, State 2, Procedure ExtractDataforProducer, Line 3
Must declare the table variable "@TableName".

Maybe the table cannot be variable here?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225058 Visits: 40420
Betty Sun (7/9/2008)
Hi all,

I have a stored procedure. But it seems that you cannot use variable for a table. variables are only can be used in condition expression? What about case statement.
I keep getting the following message:

Msg 156, Level 15, State 1, Procedure ExtractDataforProducer, Line 5
Incorrect syntax near the keyword 'Case'.
Msg 1087, Level 15, State 2, Procedure ExtractDataforProducer, Line 7
Must declare the table variable "@TableName".
Msg 1087, Level 15, State 2, Procedure ExtractDataforProducer, Line 13
Must declare the table variable "@TableName".


//////////////////////////////////

Create proc ExtractDataforProducer (@StartDate Datetime, @EndDate Datetime, @SubProducerFlag char(1), @TableName varchar(15), @AgentCode varchar(10))
As


Case @SubProducerFlag
When 'Y' Then
Select * from @TableName tn
inner join tis_vs vs on tn.pnref=vs.pnref
and agentCode like @AgentCode
and convert(char(10), vs.txndatetime,126) between convert(char(10),@StartDate,126) and convert(char(10), @EndDate, 126)
order by vs.txndatetime
Else
Select * from @TableName tn
inner join tis_vs vs on tn.pnref=vs.pnref
and agentCode=@AgentCode
and convert(char(10), vs.txndatetime,126) between convert(char(10),@StartDate,126) and convert(char(10), @EndDate, 126)
order by vs.txndatetime
End

Go


CASE is a function, not a control flow statement. In this case, you need to use the IF ELSE construct.

Cool

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225058 Visits: 40420
Also, you can't use a variable in a select statement for a tablename, unless the variable itself is a table variable (confused?).

What you are trying to do would require the use a dynamic sql. Read BOL for more info, then ask more questions for clarity.

Cool

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
GSquared
GSquared
SSC Guru
SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)

Group: General Forum Members
Points: 139807 Visits: 9731
I have a question: Based on your queries, it looks like you have multiple tables with the same columns. Is that correct?

The way you're trying to use Case looks like Visual Basic. It doesn't work that way in SQL.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Atif-ullah Sheikh
Atif-ullah Sheikh
SSCrazy Eights
SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)SSCrazy Eights (8K reputation)

Group: General Forum Members
Points: 8027 Visits: 5217
Dynamic SQL is the solution.
Generate the query in your sp and Execute it with EXEC.

I tried by the following code;


Declare @Sql varchar(max)
Set @Sql = 'Select * from ' + @TableName + ' tn
inner join tis_vs vs on tn.pnref=vs.pnref
and agentCode '

if @SubProducerFlag='Y' Set @Sql = @Sql + ' like ''' + @AgentCode + '%'''
if @SubProducerFlag !='Y' Set @Sql = @Sql + ' = ''' + @AgentCode + ''''


Set @Sql = @Sql + ' and convert(char(10), vs.txndatetime,126) between ''' + convert(char(10), @StartDate ,126) + '''
and ''' + convert(char(10), @EndDate , 126) + ''' order by vs.txndatetime '


Print @Sql
Exec @Sql





I hope it will help you. Remember that there are alot of issues regarding string processing like SQL injection.

Atif Sheikh

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search