Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 cursor

Author  Topic 

fmardani
Constraint Violating Yak Guru

433 Posts

Posted - 2005-06-28 : 07:30:31
Hi,
In query analyser I get an eror when writing the follwoing line:

declare curPhoneBookFeed cursor for

if (@CompanyCode = 'A')
begin
select field1, field2, field3 from tblA
end

else if (@CompanyCode = 'B')
begin
select field1, field2, field3 from tblB
end

The error is:
Incorrect syntax near the keyword 'if'.

Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-06-28 : 07:38:29
Can you post the entire code you used?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-06-28 : 07:41:36
Try

if (@CompanyCode = 'A')
begin
declare curPhoneBookFeed cursor for
select field1, field2, field3 from tblA
end

else if (@CompanyCode = 'B')
begin
declare curPhoneBookFeed cursor for
select field1, field2, field3 from tblB
end


Andy


Beauty is in the eyes of the beerholder
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-06-28 : 08:37:22
how bout:

kill curPhoneBookFeed cursor

Corey

Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now."
Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-06-28 : 08:42:27
Yep scrub my answer i'm with Corey on this one


Beauty is in the eyes of the beerholder
Go to Top of Page

fmardani
Constraint Violating Yak Guru

433 Posts

Posted - 2005-06-28 : 09:20:51
where do you use
kill curPhoneBookFeed cursor in the following sql please

if (@CompanyCode = 'A')
begin
declare curPhoneBookFeed cursor for
select field1, field2, field3 from tblA
end

else if (@CompanyCode = 'B')
begin
declare curPhoneBookFeed cursor for
select field1, field2, field3 from tblB
end
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-06-28 : 09:25:45
kill curPhoneBookFeed cursor means don't use a cursor...

its rarely the best option!


and if you must:

declare curPhoneBookFeed cursor for
select field1, field2, field3 from tblA Where @CompanyCode = 'A'
Union All
select field1, field2, field3 from tblB Where @CompanyCode = 'B'

EDIT: should work... but I've never used a cursor (intentionally)


Corey

Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now."
Go to Top of Page

Crito
Starting Member

40 Posts

Posted - 2005-06-28 : 12:52:33
don't use begin and end if there's only one statement in between, it confuses the !@#$% out of the parser.

----------------------------------
Gun for hire, have horse, will travel.
Go to Top of Page
   

- Advertisement -