Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WHAT IS WRONG WITH THIS SP????

Status
Not open for further replies.

hinchdog

Programmer
Feb 14, 2001
380
US
I'm just getting started with sp's. I wrote one to loop through a table of administrators, find the current admin (the one whose admin_current column is true), set the status to false (admin_current=0), then move to the next record and set the status to true. I can't seem to get it to work. Could someone examine my code? Thanks.

CREATE PROC sp_ChangeAdmin
AS
BEGIN
DECLARE @ThisID INT
DECLARE @ThisCurrent VARCHAR(18) <--is this right??
DECLARE ThisAdmin CURSOR FOR
SELECT admin_id, admin_current
FROM admin

WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM ThisAdmin INTO @ThisID, @ThisCurrent
IF @ThisCurrent = 1
BREAK
ELSE
CONTINUE
END
UPDATE admin SET admin_current = 0 WHERE admin_id = @ThisID
-- get the next record after admin
FETCH NEXT FROM ThisAdmin INTO @ThisID, @ThisCurrent
-- check if end of records
IF @@FETCH_STATUS = 2
FETCH FIRST FROM ThisAdmin INTO @ThisID, @ThisCurrent
UPDATE admin SET admin_current = 1 WHERE admin_id = @ThisID

CLOSE ThisAdmin
DEALLOCATE ThisAdmin
END
 
nevermind - i forgot to open the cursor. duh! :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top