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)
 Updating tables with an IF

Author  Topic 

vwilsonjr
Starting Member

45 Posts

Posted - 2002-11-25 : 13:15:15
I have a table that I imported into a database (from a text file). I want to update another table with the information in the new table. The logic behind it is simple but has me stumped.

if (newtable.employeeID) = (usertable.employeeID)
update record
else
insert record

I'm stumped in the if statement. Any help would be great.


nr
SQLTeam MVY

12543 Posts

Posted - 2002-11-25 : 13:19:09
just do it in two statements (in a transaction?)

update tbl
set fld = fld
from newtbl
where tbl.id = newtbl.id

insert tbl
select * from newtbl
where not exists (select * from tbl where tbl.id = newtbl.id)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

vwilsonjr
Starting Member

45 Posts

Posted - 2002-11-25 : 14:21:18
What does "set fld = fld" do?

Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2002-11-26 : 03:46:38
quote:

What does "set fld = fld" do?





Set fld = fld would set fld equal to itself (IE not change the value)

That is the part of the statement that you need to change to whatever you want to update. As in

set fieldiwanttochange = 1

or

set usertable.name = newtable.name

-------
Moo.

Edited by - mr_mist on 11/26/2002 04:04:34
Go to Top of Page
   

- Advertisement -