mrosier
asked on
SQL 2005 update query
Hello! I am trying to find out if I can perform a certain update to a table called Records. To keep it simple, let's say I have columns called column1 and column2. I have 5000 records in this table where column1 = 32. For the first 1000 of these records I want to update column2 = 1. The next 2000 after that I want to update column2 = 2. And then the next 2000 column2 = 3. Can I accomplish this in any way using update queries? Thanks!!
ASKER
Thanks Phillip! Will this reference my original table "Records" ? I ask because I have other fields in this table and I am basically adding and populating this new field which I am calling column2. In reality I already have several columns and wanted to update this column2 in this fashion based off this column1 field. The way I am reading your code there, am I starting from scratch with that?
Yes, but how do you determine the "first 1000 rows", etc? Is there a third column that has, say, a datetime stamp or an identity that can be used to suss that out?
In my code, don't use lines 1-9, and change all references to @sample to [Records].
ASKER
I do have a unique ID field in the table if that is what you mean
ASKER
thanks Phillip, do I need to create MyTable first then or should that also be my table "Records"?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Scott, thanks! Do I need to create "cte_assign_row_nums" in the table first or does that create itself with the query provided? And does ID represent my pre-existing unique ID field I mentioned?
The cte is created within the query itself: the keyword "WITH" at the start "tells" SQL that a cte definition is coming.
Yes, the ID is the unique ID column you mentioned. But you can order by any column you want, it doesn't have to be a unique id.
Yes, the ID is the unique ID column you mentioned. But you can order by any column you want, it doesn't have to be a unique id.
ASKER
ahhh ok, and in the end this update applies to column2 in Records correct?
Yes, that's correct. CTEs are great for doing those types of UPDATEs, since they "remember" which individual row the UPDATE needs made to.
ASKER
excellent, thanks Scott! Do I need to include the other fields in Records with this query, or will it work just fine with what we have here?
You should only need to specify the relevant columns in the CTE, it should work fine. If SQL needs another column, it will give you an error about it :-).
ASKER
Thanks Scott :)!
ASKER
Hey Scott, one other clarification on this solution. As you could imagine the records of value column1 = 32 are scattered all throughout the table among other values. In the Case section of the query 1, this query is basically only looking at my records column1 = 32 so row_num values are only being assigned to these reocrds correct? My other records are ignored in this query, correct?
Correct. Other rows will be ignored, only those that meet the WHERE condition are considered.
ASKER
Hey Scott, another question if you don't mind. So far this has worked thanks! But if I wanted to toss another criteria up there should it work as well? For example in your query we have column1 = 32 as the criteria. I assume it should work as well if I said WHERE column1 = 32 AND columnA = 0 this will apply to all my records who have both those criteria, correct? It should do the same thing we have already but look at those two criteria without anything unexpected beyond my way of seeing it here?
Yes, that should work.
ASKER
thanks Scott, if you don't mind I want to keep this question open for a week or so until this process is finished in case we run into any trouble?
OK by me, although I would follow up with it even if it were closed :-).
ASKER
thanks Scott, you're awesome!
Open in new window