SQL Update entire row

Hello,

I have two tables (table_a and table_b).  Each table has the same columns/data types.  
I'm trying to update the entire row in table_b from table_a joining on table_a.ID=tableb.ID.  

Is there another way to do this besides UPDATE or do I need to just suck it up and list all of the columns (SET table_b.col1=table_a.col1 etc.)?
LVL 1
ttist25Asked:
Who is Participating?
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
You could still do the delete/ insert, but only the where clause would change to get you your subset.  

EG:
delete from tbl_b where col = XXXX;

insert into tbl_b select * from tbl_a where col = XXXX and <Other criteria to generate subset into tbl B>.

Open in new window


Note there are downsides to the above approach as well (really update is better) in that you're generating more log data, but it'll save time on the coding side (for me I'm constantly reminding myself not to be lazy.  Better to code it once and do it right then have the system do something over and over again because I didn't want to take the extra minutes).  

Otherwise as you discovered you're stuck without relying on dynamic sql for the update statement.  I'm not one for the dynamic approach as it becomes hard to modify / understand as time goes on and I normally use excel as well to generate mundane sql statements like this when I have a lot of columns.
0
 
awking00Commented:
What dbms are you using?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
assuming there's no PK ID in tbl_b you could do a delete and just insert into the table.

eG:

delete from tbl_b where col1 = xxxx

insert into tbl_b select * from tbl_a where col1 = XXX

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ttist25Author Commented:
MS SQL Server 2K8
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
With the PKID - there can still be one what I really meant to say was that you don't mind the value changing.
0
 
ttist25Author Commented:
Hi Kyle!

That would do it as the question was posed and when I read your answer I thought "I'm such an idiot" but, then I realized I've left some information out.  

I actually have 3 tables.  Table_A is the "main" table with all of the records.  Table_B is a subset of Table_A (same columns but not all of the same rows).  Table_C is also a subset of Table_A (again same columns but not all of the same rows).  

I need to update Table_A from Table_B and then again from Table_C.  I've pretty much resigned myself to just using sp_help and Excel to build the giant list of a.field = b.field.  I know I could build some kind of dynamic SQL thing to automate it but, I've already done it with sp_help and copy/paste to Excel and a little (=A1$"=b."&B1&",").  

I was hoping there might be an alternative to UPDATE or some quick thing like SET a.*=b.* or something but no luck.
0
 
ttist25Author Commented:
Thanks for your help again Kyle
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.