Are these statements valid?
UPDATE Table1
FROM (SELECT * FROM Table2)
INSERT INTO Table1
(SELECT * FROM Table2)
Are these statements valid?
UPDATE Table1
FROM (SELECT * FROM Table2)
INSERT INTO Table1
(SELECT * FROM Table2)
Your update statement needs a Set for each field you want to update, so no. Correct syntax:
UPDATE table1
SET table1.field1=table2.field1, table1.field2=table2.field2
FROM table1
INNER JOIN table2 ON table1.keyfield = table2.keyfield
Your insert statement will work provided that Table1 and Table2 have the same columns in the same order.
Edit: If you're looking for an example of Update/Insert (Upsert), check out this blog post (which works on SQL 2008--not sure about 2005 but doubt it).
Another option is an Update and then an insert. Example from this blog post :
UPDATE CustomersA
SET CustomerName = B.CustomerName
FROM CustomersA A (NoLock)
INNER JOIN CustomersB B (NoLock) ON A.CustomerId = B.CustomerId
And later run the Insert command
INSERT INTO CustomersA (
CustomerId,
CustomerName
)
SELECT
Id,
Name
FROM CustomersB (NoLock)
WHERE
Id NOT IN (
SELECT CustomerId FROM CustomersA (NoLock)
)
INSERT INTO Table1
(SELECT * FROM Table2)
That could work - if you're lucky.
I'd recommend using a more strict syntax:
INSERT INTO dbo.Table1 (Field1, Field2, ..., FieldN)
SELECT Field1, Field2, ..., FieldN
FROM dbo.Table2
I would always and explicitly specify the schema (dbo) and fields for both my INSERT and my SELECT statements. That way, you can
Marc