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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 insert if not update

Author  Topic 

Nebby
Starting Member

9 Posts

Posted - 2005-03-30 : 07:41:35
hello, i have a little problem,

i now have this query in my SQL query analyzer
it is updating my record that have the key, but this it will not inserting new records.

update MKGUser.Dag_opname
set MKGUser.Dag_opname.[weekdag_tekst]=Z.[weekdag tekst]
from MKGUser.Dag_opname inner join
(SELECT * FROM OPENQUERY( test_access, 'select * from [dag opname]' )
)Z
on MKGUser.Dag_opname.[Dag_opname]=Z.[dag opname]


how can i do that?

thanks

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-03-30 : 08:25:22
updates and inserts are 2 different statements. You've got the 1st part already (the update on an inner join).
Now you have to do the Insert by using either "where NOT Exists" or "OUTER JOIN where <join column(s) of outer table> is NULL".

Be One with the Optimizer
TG
Go to Top of Page

Nebby
Starting Member

9 Posts

Posted - 2005-03-30 : 08:36:43
can you give an example?

i don't know where to put te "where not exist (insert..."

thanks

quote:
Originally posted by TG

updates and inserts are 2 different statements. You've got the 1st part already (the update on an inner join).
Now you have to do the Insert by using either "where NOT Exists" or "OUTER JOIN where <join column(s) of outer table> is NULL".

Be One with the Optimizer
TG

Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-03-30 : 09:13:34
yep, here ya go:

set nocount on
declare @tableA table (id int, col1 int)
declare @tableB table (id int, col1 int)
insert @tableA Select 1,1 union select 2,1
insert @tableB Select 2,2 union select 3,3

print 'before update'
Select * from @TableA

--update
update a set
a.col1 = b.col1
from @tableA a
JOIN @tableB b ON a.id = b.id

print 'after update'
SElect * from @TableA

/*
--insert (where not exists)
insert @TableA (col1)
Select col1
From @TableB b
Where NOT Exists(Select * from @TableA where col1 = b.col1)
*/

--insert (left outer join)
insert @tableA (col1)
Select b.col1
From @TableB b
Left JOIN @TableA a
ON a.id = b.id
Where a.id is NULL


print 'after insert and update'
Select * from @TableA



Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -