Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How would you modify the following SQL Server 2008 stored procedure to prevent it's execution if the value being inserted into the field "BRANCH" is a duplicate ? Branch is the Primary Key.

How would you modify the following SQL Server 2008 stored procedure to prevent it's execution if the value being inserted into the field "BRANCH" is a duplicate value because "branch" is the Primary Key?

ALTER PROC [dbo].[proc_CSL_Insert_branch]
(@strBranch varchar(3), @strRegion varchar(2), @txtRollup varchar(6), @txtCenter varchar(6),
@txtManager varchar(45), @txtBraddress varchar(50), @txtCity  varchar(35), @txtState varchar(2), @txtZip varchar(10),
@txtPhone varchar(12), @txtFax varchar(12), @txtAssistant varchar(35), @txtMemo varchar(50),  
@txtBrname varchar(50), @txtEmailaddr varchar(50), @txtType varchar(1), @chkStatus bit)  
AS
INSERT INTO dbo.tbl_CSL_Branches (branch, region, rollups, center,  manager, Braddress, city,  states,  zip,
phone, fax,  assistantnm,  memo, branchname, EMAILADD, typ,  closed)
VALUES (@strBranch, @strRegion, @txtRollup, @txtCenter, @txtManager, @txtBraddress, @txtCity, @txtState, @txtZip,
@txtPhone, @txtFax, @txtAssistant, @txtMemo, @txtBrname, @txtEmailaddr, @txtType, @chkStatus)
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you want to return something? Just replace the PRINT code to what you want to be returned:
ALTER PROC [dbo].[proc_CSL_Insert_branch]
	(@strBranch varchar(3), @strRegion varchar(2), @txtRollup varchar(6), @txtCenter varchar(6),
	@txtManager varchar(45), @txtBraddress varchar(50), @txtCity  varchar(35), @txtState varchar(2), @txtZip varchar(10), 
	@txtPhone varchar(12), @txtFax varchar(12), @txtAssistant varchar(35), @txtMemo varchar(50),  
	@txtBrname varchar(50), @txtEmailaddr varchar(50), @txtType varchar(1), @chkStatus bit)  
AS

BEGIN TRY
	INSERT INTO dbo.tbl_CSL_Branches (branch, region, rollups, center,  manager, Braddress, city,  states,  zip, phone, fax,  assistantnm,  memo, branchname, EMAILADD, typ,  closed)
	VALUES (@strBranch, @strRegion, @txtRollup, @txtCenter, @txtManager, @txtBraddress, @txtCity, @txtState, @txtZip, @txtPhone, @txtFax, @txtAssistant, @txtMemo, @txtBrname, @txtEmailaddr, @txtType, @chkStatus) 
END TRY

BEGIN CATCH
	IF @@ERROR = 2627
		PRINT 'PK violation'
	ELSE
		PRINT 'Other error'
END CATCH

Open in new window