Discussion:
Declare @var CURSOR FOR" works only in a stored procedure ?????
(too old to reply)
Jose Luis
2008-07-30 11:46:55 UTC
Permalink
Hi,

When I run the code below:

begin
declare
@account_no int

declare cmf_cur cursor for select * from CMF where
account_no=@account_no

select @account_no=1
open cmf_cur

end
go


this error message is shown:

Msg 7344, Level 15, State 2:
Server 'DESA101_DS', Line 5:
DECLARE CURSOR must be the only statement in a query batch.


but when I add "create procedure p1 as" it runs fine.


Any hint?

Thanks in advance,
Jose Luis
mpeppler@peppler.org
2008-07-30 12:30:00 UTC
Permalink
Hi,
   begin
   declare
   declare cmf_cur cursor for select * from CMF where
   open cmf_cur
   end
   go
   DECLARE CURSOR must be the only statement in a query batch.
but when I add "create procedure p1 as" it runs fine.
Any hint?
If you aren't in a proc you need to add a "go" (or other batch
separator) so that the declare cursor command is separate from the
open, fetch, etc.

Michael
Jose Luis
2008-07-31 06:15:20 UTC
Permalink
Hi,
   begin
   declare
   declare cmf_cur cursor for select * from CMF where
   open cmf_cur
   end
   go
   DECLARE CURSOR must be the only statement in a query batch.
but when I add "create procedure p1 as" it runs fine.
Any hint?
If you  aren't in a proc you need to add a "go" (or other batch
separator) so that the declare cursor command is separate from the
open, fetch, etc.
Michael
declare
@account_no int
go

declare cmf_cur cursor for select * from CMF where
account_no=@account_no
go


With this code this error is shown:


isql -SDESA101_DS -DCUSTOMER101 -Udbo_billing -Pdbo_billing -
ikk.sql
Msg 137, Level 15, State 2:
Server 'DESA101_DS', Line 2:
Must declare variable '@account_no'.


Regards,
Jose Luis
Manish Negandhi
2008-07-31 06:36:47 UTC
Permalink
Hi,
   begin
   declare
   declare cmf_cur cursor for select * from CMF where
   open cmf_cur
   end
   go
   DECLARE CURSOR must be the only statement in a query batch.
but when I add "create procedure p1 as" it runs fine.
Any hint?
If you  aren't in a proc you need to add a "go" (or other batch
separator) so that the declare cursor command is separate from the
open, fetch, etc.
Michael
   declare
   go
   declare cmf_cur cursor for select * from CMF where
   go
   isql -SDESA101_DS -DCUSTOMER101 -Udbo_billing -Pdbo_billing -
ikk.sql
That is because "declare cursor" should be in its own batch and
variable scope is until you give "go" and in this case
you can not "decalare cursor" and define variable inside the same
batch. If you want to run this type of code
outside the stored proc , you should avoid using variables in select
statement, instead you can fetch values into variables

something like..

declare cmf_cur cursor for select account_no from CMF where
account_no > 0
go

declare @account_no int
open cmf_cur
fetch cmf_cur into @account_no
while @@SQLSTATUS = 0
begin
--....your code goes here....
print "account no is %1!",@account_no
end
close @cmf_cur
go

-HTH
Manish Negandhi
[TeamSybase]
Sherlock, Kevin
2008-07-31 15:53:55 UTC
Permalink
As Manish has already implied, this is a scoping problem. The scope of a
cursor defined in a user session is for the entire session, whereas the
scope for the @variable is for a batch. So once the batch is terminated by
a "go" statement, your variable is out of scope, but your cursor is not, so
there is a conflict of scope here. Hence the requirement that DECLARE
CURSOR be the only statement in a batch. Cursor scope for a cursor declared
in a stored procedure is the scope of the procedure, which matches the scope
for any declared @variables in that procedure.

Having said all of that, you can still reproduce what you want with either a
#temp table, or with context variables. IE:

/* here is an example using appcontext */
set nocount on
go
declare c1 cursor for
select name from sysobjects where id = convert(int,
get_appcontext('c1','id'))
go

declare @silent int /* use to supress output */

select
@silent=rm_appcontext('c1','id'),@silent=set_appcontext('c1','id','8')

open c1
fetch c1
close c1

select
@silent=rm_appcontext('c1','id'),@silent=set_appcontext('c1','id','5')

open c1
fetch c1
close c1
go


------------------------------
syslogs


------------------------------
sysprocedures
Post by Jose Luis
Hi,
begin
declare
@account_no int
declare cmf_cur cursor for select * from CMF where
open cmf_cur
end
go
DECLARE CURSOR must be the only statement in a query batch.
but when I add "create procedure p1 as" it runs fine.
Any hint?
If you aren't in a proc you need to add a "go" (or other batch
separator) so that the declare cursor command is separate from the
open, fetch, etc.
Michael
declare
@account_no int
go

declare cmf_cur cursor for select * from CMF where
account_no=@account_no
go


With this code this error is shown:


isql -SDESA101_DS -DCUSTOMER101 -Udbo_billing -Pdbo_billing -
ikk.sql
Msg 137, Level 15, State 2:
Server 'DESA101_DS', Line 2:
Must declare variable '@account_no'.


Regards,
Jose Luis
b***@gmail.com
2019-02-25 17:25:50 UTC
Permalink
Post by Jose Luis
Hi,
begin
declare
@account_no int
declare cmf_cur cursor for select * from CMF where
open cmf_cur
end
go
DECLARE CURSOR must be the only statement in a query batch.
but when I add "create procedure p1 as" it runs fine.
Any hint?
Thanks in advance,
Jose Luis
thanks my friends

Loading...