Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If inside the Cursor statement

Status
Not open for further replies.

TMRO

Technical User
Jan 10, 2003
140
CA
Hi everybody,

I need to declare a cursor but depending on the current day the select statement is different (the where condition actually is different).

declare cursorname cursor for
if @day = '1'
begin
select a, b, c from table where ...
end
if @day = '2'
begin
select a, b, c from table where ...
end

and I'm receiving the message: "Incorrect syntax near the keyword 'if'."

If the message is true, then how can I solve the problem?

Thx,
TMRO
 
Thx cLFlaVA,

I tried that and I received another syntax error.
I'm trying now to put the if before the cursor and put the criteria in a variable.

Actually the variable is for the order clause (not where condition)

so this is my code now:

if @d = 1
begin
@day = 'MON11' -- where MON11 is one of the column
end
if @d = 2
begin
@day = 'TUE11' -- where TUE11 is one of the column
end

select * from tablename order by @day

and I received another error something with "Variables are only allowed when ordering by an expression referencing a column name."

I took out the cursor for easy read.
Thx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top