This is another I know I've done this before but can't remember the syntax thing.
update tableone
set fieldex = 'valueOne'
where clsid % 3 = 0
and fieldex is null
For every ID that's divisible by 3 with no remainder, it updates.
Theoretically, you could pass in the n (3, in this case), and put in some more programming and loop it. But that's more complex code that I might put in the front end, or an admin console, and not in TSQL (personal preference).
Monday, July 30, 2012
Sunday, July 29, 2012
SQL Updating One Table from Another Table
Recently, I had to do a dataconversion (field needed float - long story) but the table stored nvarchars. And SQL Studio wouldn't let me do a direct conversion in the designer. So, I made a backup table, then nulled out the offending field, changed the datatype, then had to bring back the correct data.
I've done this before, but I never remember the syntax, so here it is.
I often forget how to do this in T-SQL, so as an opener for this blog, here it is.
UPDATE Tech
SET Tech.Zipcode = s.ZipCode
FROM Tech t
INNER JOIN SafeBackupTech s
ON t.UserName = s.UserName
Also, you can do it this way:
UPDATE Tech
SET ZipCode = SafeBackupTech.ZipCode
FROM Tech, SafeBackupTech
WHERE Tech.UserName = SafeBackupTech.UserName
Thanks to StackOverflow for the answers to this and many other brain-fart related questions.
(http://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match)
I've done this before, but I never remember the syntax, so here it is.
I often forget how to do this in T-SQL, so as an opener for this blog, here it is.
UPDATE Tech
SET Tech.Zipcode = s.ZipCode
FROM Tech t
INNER JOIN SafeBackupTech s
ON t.UserName = s.UserName
Also, you can do it this way:
UPDATE Tech
SET ZipCode = SafeBackupTech.ZipCode
FROM Tech, SafeBackupTech
WHERE Tech.UserName = SafeBackupTech.UserName
Thanks to StackOverflow for the answers to this and many other brain-fart related questions.
(http://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match)
Subscribe to:
Comments (Atom)