Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Stored Procedures, IF logic, Multi SQL Stmts

Author  Topic 

sbraudrick
Starting Member

5 Posts

Posted - 2008-10-03 : 17:43:11
I'm still learning SQL syntax, so please forgive me. I have a stored procedure I have written that has nested IF / ELSE statements and multiple INSERT / UPDATE statements. I checked the syntax in enterprise manager and it checked out OK. I then went to the debug function in Query analyzer and passed variables to see if it worked. It ran fine but the records were not updated or inserted. (I'm going to really feel stupid if that's the way the debugger is supposed to work!)

Before I go banging my head against a wall I figured I'd see if someone can take a quick look at the procedure for me to see if there's something glaringly wrong.

The procedure basically checks to see if the "reviewer" has already reviewed this record, then checks to see if the review count requirement has been met and executes the same SQL statements but returns a different value based on the 3 possibilities. My application will then handle the next steps.

Thanks in advance, code follows.

CREATE PROCEDURE usp_IntRevApprove 

@SONum nvarchar(10),
@RevID int,
@reviewer char(10)

AS

Declare @revtime datetime
SET @revtime = GETDATE()

Declare @revcount int
SET @revcount = (SELECT Count(SONUM) FROM OrderReviews WHERE SONum = @SONum AND RevID=@RevID)
SET @revcount = @revcount + 1

Declare @revreq int
SET @revreq = (SELECT reviewreq FROM Revisions WHERE SONum = @SONum AND RevID=@RevID)

Declare @reviewed int
SET @reviewed = (SELECT Count(SONum) FROM OrderReviews WHERE SONum = @SONum AND RevID=@RevID AND reviewer=@reviewer)

IF @reviewed = 0

BEGIN
IF @revcount >= @revreq

BEGIN

UPDATE Revisions
SET reviewcount = @revcount
WHERE SONum = @SONum AND RevID = @RevID

INSERT INTO OrderReviews (SONum, RevID, reviewer, accepted, revtime)
VALUES (@SONum, @RevID, @reviewer, 'yes', @revtime)

RETURN -1

END
ELSE
BEGIN

UPDATE Revisions
SET reviewcount = @revcount
WHERE SONum = @SONum AND RevID = @RevID

INSERT INTO OrderReviews (SONum, RevID, reviewer, accepted, revtime)
VALUES (@SONum, @RevID, @reviewer, 'yes', @revtime)

RETURN -2

END
END
ELSE
BEGIN
RETURN -3
END
GO

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-04 : 02:58:30
what does the sp give as return value?-1,-2 or -3? Also what's the purpose of inner IF ELSE? you're doing the same operations in both IF and ELSE
Go to Top of Page

sbraudrick
Starting Member

5 Posts

Posted - 2008-10-04 : 16:47:32
it returns -2.

The inner IF is purely to determine the return value for my application. Essentially, the application will advance the order to the next step once "revcount" reaches "revreq".

The application is a workflow automation for orders that have to be reviewed by a predetermined number of reviewers prior to moving to the next stage.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-05 : 01:35:56
i've a doubt on below staement
SELECT reviewreq FROM Revisions WHERE SONum = @SONum AND RevID=@RevID

you're find out total count and number reviewed by reviewer seperately. Wht i think is shouldnt you be taking COUNT(reviewreq) in above statement to get number of articles yet to review rather than taking just reviewreq alone?
Go to Top of Page

sbraudrick
Starting Member

5 Posts

Posted - 2008-10-05 : 12:03:38
quote:
Originally posted by visakh16

i've a doubt on below staement
SELECT reviewreq FROM Revisions WHERE SONum = @SONum AND RevID=@RevID

you're find out total count and number reviewed by reviewer seperately. Wht i think is shouldnt you be taking COUNT(reviewreq) in above statement to get number of articles yet to review rather than taking just reviewreq alone?



Actually, the reviewreq (required reviews) is stored within the "Revisions" table because it can change based on requirements for the specific order/revision. This is determined at the time the order/revision is submitted to the system.

As you can see before the IF logic starts I am pulling both the reviewreq and a count of reviews (revcount) in order to compare the values and determine the return value.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-05 : 13:35:06
Have you tried running this code by running profiler to see what statements are executed in background. Also are there any INSERT,UPDATE triggers existing on OrderReviews,Revisions?
Go to Top of Page

sbraudrick
Starting Member

5 Posts

Posted - 2008-10-05 : 13:41:42
Fixed it.

As I suspected, I didn't realize that "Auto Rollback" was enabled by default in the query analyzer debugger. Unchecked the box and the records updated as designed.
Go to Top of Page
   

- Advertisement -