0

I'm working with SQL Server 2005 on a stored procedure, but it appears to be going through both parts of the following statement, and not skipping the second half if it is supposed to.

Basically it should update the row if there is data there, and remove the row if there isn't.
All the fields except for the ID fields are from user input (or a lack thereof).

DECLARE @PartID1 char(10)
	If @PartDescription1 IS NOT NULL AND @PartCost1 IS NOT NULL
		Update PART
		Set PartDescription = @PartDescription1, PartCost = @PartCost1, PartCharge = @PartCharge1, OrderedFrom = @PartOrderedFrom1, OrderNumber = @PartOrderNumber1
		Where ID = @PrtID1
	Else
		If @PrtID1 IS NOT NULL AND @PartDescription1 IS NULL
			Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID1
			Delete From PART Where ID = @PrtID1
	Select @PartID1=@PrtID1

Thank you,
J'Tok

1
Contributor
1
Reply
2
Views
9 Years
Discussion Span
Last Post by J'Tok
0

I figured it out. blocks need to be wrapped in BEGIN/END.

Here is is:

DECLARE @PartID1 char(10)
	If @PartDescription1 IS NOT NULL AND @PartCost1 IS NOT NULL
BEGIN
		Update PART
		Set PartDescription = @PartDescription1, PartCost = @PartCost1, PartCharge = @PartCharge1, OrderedFrom = @PartOrderedFrom1, OrderNumber = @PartOrderNumber1
		Where ID = @PrtID1
END
	Else If @PrtID1 IS NOT NULL AND @PartDescription1 IS NULL
BEGIN
			Delete From PROBLEM_PART Where PROBLEM_ID = @ProblemID AND PART_ID = @PrtID1
			Delete From PART Where ID = @PrtID1
END
	Select @PartID1=@PrtID1
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.