Here I am going to explain you, how IF EXISTS works.. look the below query
--Create Table
CREATE Table tblEmployee (ID int identity(100,1) constraint pk_Id primary key,
empName nvarchar(100), empRoleId int, Active bit DEFAULT 1)
GO
--Insert Values
INSERT INTO tblEmployee (empName, empRoleId)
VALUES ('Pradeep', 3)
Go
--Execute Query
DECLARE @empName nvarchar(100)
DECLARE @empRoleId INT
SET @empName = 'Satya P'
SET @empRoleId = 3
IF EXISTS (SELECT ID FROM tblEmployee WHERE empName = @empName AND empRoleID = @empRoleID)
BEGIN
PRINT 'Record Already Exists!'
END
ELSE
BEGIN
INSERT INTO tblEmployee(empName,empRoleID)
VALUES (@empName, @empRoleID)
PRINT 'Record Inserted Suceesfully!'
END
--Check the Inserted Record
SELECT * FROM tblEmployee
I hope you will enjoy...
Comments