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 record only if record does not already exis

Author  Topic 

eirikr_1
Starting Member

27 Posts

Posted - 2013-02-11 : 14:39:00
what is wrong with this sql? It does not allow me to insert a test computer, when test is not in database

if not exists (select distinct NetBIOSName from Computers)
begin
insert into Computers(NetBIOSName, IP)
values('test','111.22.33.444')
end

eirikr_1
Starting Member

27 Posts

Posted - 2013-02-11 : 14:48:08
I got it. I missed a condition in
select distinct NetBIOSName from Computers where NetBIOSName='test'
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2013-02-11 : 14:48:18
Unless your table is empty, select distinct NetBIOSName from Computers will always return at least one record.


[Edit]
NM you found it.




How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page
   

- Advertisement -