September 21, 2011 at 9:08 am
Hi,
I have 2 tables with the very same LONG structure (table_1 and table_2).
I was wondering if it would be possible to update table_1 with the content of table_2 without specifying the columns via a very long SET statement.
In other words:
Normal way of working: UPDATE table_1 A SET A.col1 = B.col1, A.col2 = B.col2, .... INNER JOIN table_2 B ON A.col1 = B.col1
Since the list of columns in the structure is quite large, I would like to have something similar to the INSERT statement
INSERT INTO table_1 A (col1, col2, col3, col4, ....)
SELECT B.col1, B.col2, B.col3... FROM table_2 B
would become:
UPDATE table_1 (col1, col2, ....)
SELECT B.col1, B.col2, ....
Is this possible?
Many thanks
September 21, 2011 at 9:45 am
SET statement is mandatory part of UPDATE statement (check BOL for UPDATE), therefore you can not avoid using it.
September 21, 2011 at 3:47 pm
boeledi (9/21/2011)
Hi,I have 2 tables with the very same LONG structure (table_1 and table_2).
I was wondering if it would be possible to update table_1 with the content of table_2 without specifying the columns via a very long SET statement.
In other words:
Normal way of working: UPDATE table_1 A SET A.col1 = B.col1, A.col2 = B.col2, .... INNER JOIN table_2 B ON A.col1 = B.col1
Since the list of columns in the structure is quite large, I would like to have something similar to the INSERT statement
INSERT INTO table_1 A (col1, col2, col3, col4, ....)
SELECT B.col1, B.col2, B.col3... FROM table_2 B
would become:
UPDATE table_1 (col1, col2, ....)
SELECT B.col1, B.col2, ....
Is this possible?
Many thanks
Are you updating ALL the columns in the target table or just a lot of them? In that same breath... is there a PK associated with these two tables to make a a join easier?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 21, 2011 at 10:45 pm
if you update all columns from table2 to table1 u can use merge,(for more u can check BOL) or if u want to update a set of columns then u need to mention it in ur update statement.
September 21, 2011 at 11:18 pm
I tend to agree with Celko on this one. This is a prime candidate for a redesign.
That said, what are the differences between the two tables? How many records in each?
Are all records in table1 definitely to be overwritten by those in table2?
Do both tables have the same number of records and the same number of columns and the same column names?
If so, it really almost seems like you should rename the two tables.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
September 22, 2011 at 3:51 am
CELKO (9/21/2011)
However, this sounds like it might as candidate for a better schema design. Why would you ever have the same data in two places when the whole purpose of any DB is to re educe ot remove redundancies?
An OLAP database that merges data from load tables?
Merge definitely has better syntax for this, but at the end of the day, you still need to explicitly list the columns.
If you're doing this for a lot of tables (and Celko's right in that if this is OLTP, something is wrong with your design), then you could always build statements from the metadata views (sys.columns or INFORMATION_SCHEMA.columns)
September 22, 2011 at 6:00 am
I agree that this table should probably be re-designed.
However, you can achieve what you're after by using a trigger INSTEAD OF INSERT, that could turn INSERTs into UPSERTs using the MERGE statement.
-- Gianluca Sartori
September 24, 2011 at 6:30 pm
Apparently the OP has left the building. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
September 26, 2011 at 1:57 am
Jeff Moden (9/24/2011)
Apparently the OP has left the building. 😉
Elvis? 🙂
-- Gianluca Sartori
September 26, 2011 at 4:44 am
Heh... yeah... "Dat's da ticket". 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
September 27, 2011 at 1:24 am
You can use sql-code below instead of update
truncate table table_1
&
INSERT INTO table_1 A (col1, col2, col3, col4, ....)
SELECT B.col1, B.col2, B.col3... FROM table_2 B
September 27, 2011 at 2:02 am
You can use sql-code below instead of update
truncate table table_1
&
INSERT INTO table_1 A (col1, col2, col3, col4, ....)
SELECT B.col1, B.col2, B.col3... FROM table_2 B
September 27, 2011 at 6:15 am
yubo1 (9/27/2011)
You can use sql-code below instead of updatetruncate table table_1
&
INSERT INTO table_1 A (col1, col2, col3, col4, ....)
SELECT B.col1, B.col2, B.col3... FROM table_2 B
The problem is that the OP wants to avoid using all of the column names to do such a thing on wide tables.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 27, 2011 at 12:54 pm
I am still there but as an observer 😉
September 27, 2011 at 1:33 pm
boeledi (9/27/2011)
I am still there but as an observer 😉
So, any comments?
-- Gianluca Sartori
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply