RE: Trying to avoid using a cursor....
From: TomT (tomt_at_tomt.com)
Date: 09/15/04
- Next message: Kalen Delaney: "Re: Do I care about this?"
- Previous message: SQL Apprentice: "Re: securityadmin roles"
- In reply to: TomT: "Trying to avoid using a cursor...."
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 15 Sep 2004 15:09:04 -0700
Thanks to all who assisted, this is working perfectly after implementing your
suggestions.
"TomT" wrote:
> 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: Kalen Delaney: "Re: Do I care about this?"
- Previous message: SQL Apprentice: "Re: securityadmin roles"
- In reply to: TomT: "Trying to avoid using a cursor...."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|