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 derfloh 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
Joined
Jan 10, 2003
Messages
140
Location
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
 
why not a case statement in the where clause?

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
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