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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

awking00Information Technology SpecialistCommented:
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
ttist25Author Commented:
MS SQL Server 2K8
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

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
Kyle AbrahamsSenior .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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ttist25Author Commented:
Thanks for your help again Kyle
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.