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 2005 Forums
 Transact-SQL (2005)
 if exists then insert

Author  Topic 

missMac
Posting Yak Master

124 Posts

Posted - 2008-12-04 : 12:47:52
Hello
Whats the best way to check to see if a record exists before inserting into the table.

here is my code below


insert into dbo.PhoneNumbers (Username, Number, NickName, Group_Name )
select @user, recepient, recepient, @mergeID from filter
where Username = @user and BatchNumber = @recipient


I want to

1. Check the phonenumbers table to see if the number exists
2. select distinct recepient from the filter table

Whats the best way to go about this ?

Thanks
MM


hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-12-04 : 13:02:42
[CODE]
insert into dbo.PhoneNumbers (Username, Number, NickName, Group_Name )
select distinct @user, recepient, recepient, @mergeID from filter f
where Username = @user and BatchNumber = @recipient
and not exists(select 1 from dbo.Phonenumbers where number = f.number)
[/CODE]
Go to Top of Page

missMac
Posting Yak Master

124 Posts

Posted - 2008-12-04 : 14:51:02
thanks Hanbingl
Go to Top of Page
   

- Advertisement -