We help IT Professionals succeed at work.
Get Started

Stored Procedure not working correctly

292 Views
Last Modified: 2014-04-30
The below SP is not Updating when it should. Instead it's inserting a new record. I've tested the "SELECT" in the IF statement by running it in SSMS using real values in place of the parameters.

ALTER PROCEDURE [dbo].[spAddUpdate_DETAILS_Equipment]
	@equipment_ID		INT=NULL,
	@seqNO				INT=NULL,
	@manufacturer_ID	INT=NULL,
	@serialNO			VARCHAR(50)=NULL,
	@itemNO				VARCHAR(50)=NULL,
	@modelNO			VARCHAR(50)=NULL,
	@channel_ID			INT=NULL,
	@capacity			DECIMAL=NULL,
	@capacityUnit_ID	INT=NULL,
	@lowerRange			DECIMAL=NULL,
	@upperRange			DECIMAL=NULL,
	@rangeUnit_ID		INT=NULL,
	@description		VARCHAR(MAX)=NULL,
	@active				INT=NULL,
	@createdBy_ID		INT=NULL,
	@updatedBy_ID		INT=NULL,
	@approvedBy_ID		INT=NULL
AS
BEGIN

	IF EXISTS (SELECT equipment_ID FROM DETAILS_Equipment WHERE equipment_ID = @equipment_ID AND seqNO = @seqNO)
		BEGIN
			UPDATE	DETAILS_Equipment
			   SET	manufacturer_ID = @manufacturer_ID
					,itemNO = @itemNO
					,modelNO = @modelNO
					,capacity = @capacity
					,capacityUnit_ID = @capacityUnit_ID
					,lowerRange = @lowerRange
					,upperRange = @upperRange
					,rangeUnit_ID = @rangeUnit_ID
					,[description] = @description
					,updatedBy_ID = @updatedBy_ID
					,dateUpdated = GETDATE()
					,approvedBy_ID = @approvedBy_ID
			 WHERE	equipment_ID = @equipment_ID AND seqNO = @seqNO
		END
	ELSE
		SET @seqNO = @seqNO + 1
		
		BEGIN
			INSERT INTO DETAILS_Equipment
				(equipment_ID
				 ,seqNO
				 ,manufacturer_ID
				 ,serialNO
				 ,itemNO
				 ,modelNO
				 ,channel_ID
				 ,capacity
				 ,capacityUnit_ID
				 ,lowerRange
				 ,upperRange
				 ,rangeUnit_ID
				 ,[description]
				 ,active
				 ,createdBy_ID
				 ,dateCreated
				 ,updatedBy_ID
				 ,dateUpdated
				 ,approvedBy_ID )
			 VALUES			 
				(@equipment_ID
				 ,@seqNO
 				 ,@manufacturer_ID
				 ,@serialNO
				 ,@itemNO
				 ,@modelNO
				 ,@channel_ID
				 ,@capacity
				 ,@capacityUnit_ID
				 ,@lowerRange
				 ,@upperRange
				 ,@rangeUnit_ID
				 ,@description
				 ,@active
				 ,@createdBy_ID
				 ,GETDATE()
				 ,@updatedBy_ID
				 ,GETDATE()
				 ,@approvedBy_ID)
		END	
	
	RETURN
END

Open in new window

Comment
Watch Question
Facets Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
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

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions
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