Check record exist before insert

Santosh0683
Posted by Santosh0683 under Sql Server category on | Views : 4871
Check record exist before insert in sql server database

CREATE PROCEDURE [dbo].[uspInst_Addupdate]
(
@InstId int,
@Inst varchar(50),
)
AS
BEGIN
IF @InstId=0
BEGIN
IF NOT EXISTS(SELECT * FROM InstMaster WHERE Inst= @Inst)
INSERT INTO genInstituteMaster(Inst) VALUES(@Inst)
END
ELSE
BEGIN
UPDATE InstMaster
SET Inst= @Inst WHERE (Inst= @Inst)
END
END

Comments or Responses

Posted by: Neeks on: 7/31/2010 Level:Bronze | Status: [Member]
I think we do not require to select entire record

we can achieve using COUNT(*).


IF( (SELECT COUNT(*) FROM InstMaster WHERE Inst= @Inst) = 0)

INSERT INTO genInstituteMaster(Inst) VALUES(@Inst)

END

ELSE

BEGIN

UPDATE InstMaster

SET Inst= @Inst WHERE (Inst= @Inst)

END

Posted by: Santosh0683 on: 8/1/2010 Level:Starter | Status: [Member]
thank to reply Neeks..
ya definitively we can use same.
Posted by: Santosh0683 on: 8/1/2010 Level:Starter | Status: [Member]
thank to reply Neeks..
ya definitively we can use same.

Login to post response