Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Insert and Update Queries

Author  Topic 

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2009-12-02 : 16:00:35
HI
I need to update table1 with new entries and updates from table2

Table 1 and table 2 are both exactly the same design.

Do I need to do an Insert and an update query?

E.g.
Insert into Table1
Select * from Table2
Where table2.uniqueid not in (select uniqueid form table1)

Is that right

Also

For the update query is this right (not sure of the syntax)

UPdate table1
Set table1.field1 = table2.field1, table1.field2 = table2.field2
Where table1.uniqueid = table2.uniqueid

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-02 : 16:23:11
Insert

insert into Table1
select a.* from Table2 a left join Table1 b on a.uniqueid = b.uniqueid
where b.uniqueid is null


Update

Update a
set a.field1 = b.field1,
a.field2 = b.field2
from table1 a
inner join table2 b on a.uniqueid = b.uniqueid
Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2009-12-02 : 16:26:46
thanks
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-02 : 17:41:40
ur welcome
Go to Top of Page
   

- Advertisement -