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)
 query question

Author  Topic 

PatDeV
Posting Yak Master

197 Posts

Posted - 2009-01-03 : 14:55:03
Hi all,

I want to do is insert data if company is not exist and if company, date and id exist then update the rest of the data!!

How can i do that. what would be best approach for me!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-03 : 15:05:45
There are lots of post regarding this issue. Please search for it in Searc tab.
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-01-04 : 23:33:29
IF NOT EXISTS(SELECT 1 FROM urtable WHERE companyid = @companyid)
BEGIN
INSERT Statement
END

ELSE
BEGIN
UPDATE Statement
END



@companyid is i/p parameter

Jai Krishna
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-01-04 : 23:35:49
Hi U can also try this

DECLARE @cnt INT

SELECT @cnt = COUNT(1) FROM urtable WHERE companyid = @companyid

INSERT INTO urtable
SELECT @companyid,,,,,,,,,,,,
WHERE @cnt = 0

UPDATE urtable
SET companyid = @companyid,,,,,,,,,,,
WHERE @cnt <> 0

Jai Krishna
Go to Top of Page
   

- Advertisement -