We help IT Professionals succeed at work.

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.

zimmer9
zimmer9 asked
on
59 Views
Last Modified: 2017-03-13
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)
Comment
Watch Question

SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Vitor Montalv√£oIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions