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 question

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-09-28 : 23:11:49
Jerome writes "i have this code to populate my table.

INSERT TKTimeEmp (COID,EmpID,TimeID)
SELECT COID,EmpID,'T1' FROM TKTempEmpData WHERE COID = 'BF001'

the problem is: How can i check if the data that i am inserting was already in the table(Data already exist)?"

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2003-09-29 : 00:34:09
Hi

You could use EXISTS to check :



If NOT Exists (SELECT 1 FROM TKTimeEmp WHERE COID = 'BF001')
INSERT blah





Damian
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-09-29 : 03:33:47
or (and I prefer select * rather than select 1 for existence checks)

INSERT TKTimeEmp (COID,EmpID,TimeID)
SELECT COID,EmpID,'T1'
FROM TKTempEmpData t
left outer join TKTempEmpData t2
on t.COID = t2.COID
and t.EmpID = t2.EmpID
and t2.TimeID = 'T1'
WHERE t.COID = 'BF001'
and t2.COID is null

==========================================
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
   

- Advertisement -