Using GO TO in SQL

Ranjeet_8
Posted by Ranjeet_8 under Sql Server category on | Points: 40 | Views : 945
Table Structure
CREATE TABLE [dbo].[Employee](
[First_Name] [varchar](50) NULL,
[City] [varchar](50) NULL
)

INSERT Employee (First_Name, City)
VALUES('Ashu' , 'Delhi')


Using GOTO
DECLARE @FName varchar(50)
DECLARE @CName varchar(50)
SET @FName = 'Ashu'
SET @CName = 'Delhi'
IF EXISTS (SELECT First_Name FROM Employee WHERE First_Name = @FName)
BEGIN
GOTO DuplicateInsert
END
INSERT Employee (First_Name, City)
VALUES(@FName , @CName)
DuplicateInsert:
PRINT @FName + ' Already Exists In Employee Table'

Comments or Responses

Login to post response