Avatar of zimmer9
zimmer9
Flag 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)
Microsoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jim Horn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Vitor Montalvão

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23