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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help on cursors

Status
Not open for further replies.

geffry

Programmer
Jun 26, 2002
33
US
Hi,

I am using sybase 12_1 on unix. I need to drop all procedures starting with a particular pattern.

declare @procname varchar(60)
declare proc_cursor cursor for select name from sysobjects where type = 'P' and name like 'abc%'
open proc_cursor
fetch next from proc_cursor into @procname
while (@@fetch_status <> -1)
begin
exec ('drop procedure ' + @procname)
fetch next from proc_cursor into @procname
end
deallocate proc_cursor
go


when i run this i get the following error:

DECLARE CURSOR must be the only statement in a query batch.
Msg 156, Level 15, State 1:
Server 'O', Line 4:
Incorrect syntax near the keyword 'from'.
Msg 137, Level 15, State 2:
Server 'O', Line 5:
Must declare variable '@@fetch_status'.
Msg 156, Level 15, State 1:
Server 'O', Line 9:
Incorrect syntax near the keyword 'from'.
Msg 102, Level 15, State 1:
Server 'O', Line 12:
Incorrect syntax near 'proc_cursor'.

thanks in advance
 
Hi,
Try the code below.

Create procedure check_sp as
begin
declare @procname varchar(60)
declare proc_cursor cursor for select name from emp
open proc_cursor
fetch proc_cursor into @procname
while (@@sqlstatus = 0)
begin
exec('drop procedure' + @procname)
fetch proc_cursor into @procname
end
close proc_cursor
deallocate cursor proc_cursor
end

Vijay
 
Hi,
Sorry, ignore the above reply and try this code.

create procedure check_sp as
begin
declare @procname varchar(60)
declare proc_cursor cursor for select name from sysobjects where type = 'P' and name like 'abc%'
open proc_cursor
fetch proc_cursor into @procname
while (@@sqlstatus = 0)
begin
exec('drop procedure' + @procname)
fetch proc_cursor into @procname
end
close proc_cursor
deallocate cursor proc_cursor
end

Vijay
 
Thanks Vijay.. It worked.

Cursors should be used as part of Procedures ?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top