Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with automating the update of a database in sql

Status
Not open for further replies.

futbwal15

IS-IT--Management
Jan 12, 2007
38
US
does anyone out there know how to code sql into bringing out a value from a list that DOESNT exist in the table. It sounds simple to do with a "doesnt equal to" but this isnt the answer i am looking for.

for example, you run the query and if itmes exist, then they are updated, if an item doesnt exist, then it is created and inserted into the table.

Does anyone have a basic idea of how to do this?
 
Are you trying to load a set of records or one at a time?

Here is something basic.


Code:
if exists (select * from SomeTable where PK_Col = somePK)

begin

update a
set a.someValue = b.someValue
from SomeTable a inner join SomeOtherTable b 
on a.PK_Col = b.PK_Col

end

else

begin

insert into SomeTable (PK_Col, someValue)
select PK_Col, someValue from SomeOtherTable

end

A google search for 'UPSERT SQL Server' should help you out as well.

Good Luck,

Alex


Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top