updating rows with cursor example

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Here is an example how to udpate rows using cursors. But cursors are very slow, and everything can be done without using cursors!
Example(prim is table)
CREATE PROCEDURE [dbo].[krneki] AS
DECLARE @idtemp INT
DECLARE crs CURSOR FOR
SELECT id FROM prim FOR UPDATE OF id
OPEN crs
FETCH NEXT FROM crs
INTO @idtemp
WHILE @@FETCH_STATUS <> -1
BEGIN
IF @@FETCH_STATUS <> -2
BEGIN
UPDATE prim
SET id = 333
WHERE CURRENT OF crs
END
FETCH NEXT FROM crs INTO @idtemp
END
CLOSE crs
DEALLOCATE crs


>
> Hello there
>
> I've build program that needs to update data, in very complex way, so i had
> to use cursor for use it.
>
> The cursor works fine. But i need also to update the current record of the
> cursor
>
> Is there a way to do that?
> --
> øåòé âåìãäîø
> òúéã äðãñú úåëðä
> èì' 03-5611606
> ôìà' 050-7709399
> àéîééì: roy@xxxxxxxxxxxx
>
>
.



Relevant Pages

  • Re: number of rows in cursor
    ... DECLARE authors_cursor insensitive CURSOR FOR( ... declare @l sysname ... FETCH NEXT FROM authors_cursor into @l ...
    (microsoft.public.sqlserver.programming)
  • 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: 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)