Re: number of rows in cursor

From: oj (nospam_ojngo_at_home.com)
Date: 03/26/04


Date: Fri, 26 Mar 2004 10:30:06 -0800

Here are some examples:

DECLARE authors_cursor insensitive CURSOR FOR(
  SELECT au_lname
  FROM pubs..authors
  WHERE state like 'CA')

declare @l sysname
OPEN authors_cursor
FETCH NEXT FROM authors_cursor into @l
WHILE (@@FETCH_STATUS = 0)
BEGIN
 --print @au_lname
 FETCH NEXT FROM authors_cursor into @l
END
SELECT @@CURSOR_ROWS as [@@cursor_rows]
CLOSE authors_cursor
DEALLOCATE authors_cursor
go

DECLARE authors_cursor CURSOR keyset FOR(
  SELECT au_lname
  FROM pubs..authors
  WHERE state like 'CA')

declare @l sysname
OPEN authors_cursor
FETCH NEXT FROM authors_cursor into @l
WHILE (@@FETCH_STATUS = 0)
BEGIN
 --print @au_lname
 FETCH NEXT FROM authors_cursor into @l
END
SELECT @@CURSOR_ROWS as [@@cursor_rows]
CLOSE authors_cursor
DEALLOCATE authors_cursor
go

DECLARE authors_cursor CURSOR static FOR(
  SELECT au_lname
  FROM pubs..authors
  WHERE state like 'CA')

declare @l sysname
OPEN authors_cursor
FETCH NEXT FROM authors_cursor into @l
WHILE (@@FETCH_STATUS = 0)
BEGIN
 --print @au_lname
 FETCH NEXT FROM authors_cursor into @l
END
SELECT @@CURSOR_ROWS as [@@cursor_rows]
CLOSE authors_cursor
DEALLOCATE authors_cursor
go

-- 
-oj
http://www.rac4sql.net
"Min J.Deng" <jmdcorp@hotpop.com> wrote in message
news:u9mFHZuEEHA.3980@TK2MSFTNGP09.phx.gbl...
> Thank you for replying and giving me some good idea.
>
> But I still have some problems.
> - @@CURSOR_ROW: is return -1. And in my understanding, that result because
> it is a dynamic cursor which is changing. That is not what I expected.
> So, what can I do to solve the problem?
>
> -----------
> -- This is example is :
> DECLARE authors_cursor CURSOR FOR(
>   SELECT au_lname
>   FROM authors
>   WHERE state like 'CA')
>
> OPEN authors_cursor
>
> FETCH NEXT FROM authors_cursor
>
> WHILE (@@FETCH_STATUS = 0)
> BEGIN
>  --print @au_lname
>  FETCH NEXT FROM authors_cursor
> END
>
> SELECT @@CURSOR_ROWS
>
> CLOSE authors_cursor
> DEALLOCATE authors_cursor
>
> ---------
>
> PS: can you give me an example? and more explaination. I think I should
> learing more from you
>
>
>
> "Brian Moran" <brian@solidqualitylearning.com> wrote in message
> news:u6m03ogEEHA.3676@TK2MSFTNGP09.phx.gbl...
> > and I'd REALLY agree with David.
> >
> > The correct answer to any problem rarely includes a cursor. And I hope
> this
> > doesn't come off sounding the wrong way, but... I suspect that you're a
> > beginner since you weren't able to find @@CURSOR_ROWS in Books Online.
> >
> > IMHO... beginners should NEVER use cursors in production systems becuase
> > it's my experience that beginners typically don't have the experience to
> > recognize when a cursor is a reasonable solution (sometimes they are)
vs.
> > when it's a horribly, horriby ineffecient thing to do (which is most of
> the
> > time....)
> >
> >
> >
> > -- 
> >
> > Brian Moran
> > Principal Mentor
> > Solid Quality Learning
> > SQL Server MVP
> > http://www.solidqualitylearning.com
> >
> >
> > "Min J.Deng" <jmdcorp@hotpop.com> wrote in message
> > news:uWL9gJYEEHA.3392@TK2MSFTNGP11.phx.gbl...
> > > let me ask a question:
> > > -- how can we get number of rows that a cursor can fetch?
> > >
> > >
> > >
> >
> >
>
>


Relevant Pages

  • Re: Deleting all SPROCS and UDFs
    ... DECLARE @PARENT VARCHAR ... FETCH NEXT FROM DROP_CURSOR INTO @PROCNAME ... DECLARE DROP_CURSOR CURSOR FOR select name from sysobjects where type='FN' ...
    (microsoft.public.sqlserver.server)
  • Re: Cursor printout without space between each loop
    ... One method is to insert the results into a temp table using INSERT ... ... DECLARE database_table_name CURSOR ... > FETCH NEXT FROM database_table_name INTO @table_name ...
    (microsoft.public.sqlserver.programming)
  • Re: condensing a range of numbers
    ... First, with a cursor: ... declare C_T cursor ... fetch next from C_T into @i ... >cast(@temp_num as varchar) ...
    (microsoft.public.sqlserver.programming)
  • Re: trigger to update count field - Request for criticism
    ... > declare @Workload_ID bigint, @count int ... > declare my_cursor CURSOR FOR ... > FETCH NEXT FROM my_cursor INTO @workload_id ... > deallocate my_cursor ...
    (microsoft.public.sqlserver.server)
  • Re: Problem mit Cursor: Tabellen statt Datensätze als Ergebnis
    ... > Declare mycur Cursor For Select ... ... > Fetch Next From mycur ... Schau Dir einmal beim DECLARE CURSOR die Optionen LOCAL und FAST_FORWARD ...
    (microsoft.public.de.sqlserver)