Trying to avoid using a cursor....
From: TomT (tomt_at_tomt.com)
Date: 09/12/04
- Next message: Uri Dimant: "Re: SQL- Calculated Field"
- Previous message: Uri Dimant: "Re: SELECT IN statement question"
- Next in thread: Uri Dimant: "Re: Trying to avoid using a cursor...."
- Reply: Uri Dimant: "Re: Trying to avoid using a cursor...."
- Reply: David Portas: "Re: Trying to avoid using a cursor...."
- Reply: TomT: "RE: Trying to avoid using a cursor...."
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 11 Sep 2004 22:25:02 -0700
I'm trying to update a column in a table, and as recommended by so many, do
it without resorting to a cursor.
The situation is more complex than what I am presenting here, but if someone
can show me a way to get the simplified version working I can go from there.
E.g., I have a table with 10 rows, and a column 'test'. The first record has
a value of 1 for this column, remaining rows have NULL in the column. I want
to run an procedure that will take the value of that first record in column
test, and place values in that column for each of the next 9 records,
incremented by 1. I.e. row 2 would have 2 in the column, row 3, would have 3,
and so on.
I've tried using the following, but each row gets a value of 2 in the 'test'
column:
update mytable set test = t.maxtest + 1
from
(select max(test) as maxtest
from mytable where test is not null) as t
where test is null
Coming from a VB background, my first impulse was to just use a cursor and
move through the records one by one, but I've read often in this forum that
most anything that can be done with a cursor can (and usually should) be done
without resorting to that method.
The above is as I mentioned a very simplied version of what I need to do, a
set based solution to this will give me what I need to know for the actual
task I need to accomplish.
Thanks for any assistance,
Tom Tucker
- Next message: Uri Dimant: "Re: SQL- Calculated Field"
- Previous message: Uri Dimant: "Re: SELECT IN statement question"
- Next in thread: Uri Dimant: "Re: Trying to avoid using a cursor...."
- Reply: Uri Dimant: "Re: Trying to avoid using a cursor...."
- Reply: David Portas: "Re: Trying to avoid using a cursor...."
- Reply: TomT: "RE: Trying to avoid using a cursor...."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|