Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all

 

I am trying to use cursors in a database procedure, here is the cursor code that I am using:

DECLARE Manager_Cursor CURSOR FOR
SELECT Manager_ID FROM Managers WHERE Org_ID = @OrgID
OPEN Manager_Cursor;
FETCH NEXT FROM Manager_Cursor;
WHILE @@FETCH_STATUS = 0
	BEGIN
		DELETE FROM Groups_Reference
		WHERE Manager_ID = CURRENT OF Manager_Cursor
		FETCH NEXT FROM Manager_Cursor;
	END
CLOSE Manager_Cursor;
DEALLOCATE Manager_Cursor;

 

However I am getting the following error:

Incorrect syntax near the keyword 'current'

 

Any suggestions.

 

Mike55

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted
Try this instead
WHERE CURRENT OF Manager_Cursor;

Many thanks for the reply, here is what I used in the end:

DECLARE @Manager nvarchar (10)

DECLARE Manager_Cursor CURSOR FOR
SELECT Manager_ID FROM Managers WHERE Org_ID = @OrgID
OPEN Manager_Cursor;
FETCH NEXT FROM Manager_Cursor INTO @Manager;
WHILE @@FETCH_STATUS = 0
	BEGIN
		DELETE FROM Groups_Reference
		WHERE Manager_ID = @Manager
		FETCH NEXT FROM Manager_Cursor INTO @Manager;
	END
CLOSE Manager_Cursor;
DEALLOCATE Manager_Cursor;

 

Mike55.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

  • Administrators
Posted

It looks as though you are deleting records from one table (groups_reference) based on the results of a query on another table (Managers) - is that right?

 

If so you may want to look at the functionality provided by DELETE FROM FROM as cursors will often provide worse performance and are not an implicit transaction - your code above could cause problems if a system failure occured during the WHILE loop; you would need to wrap the function inside a BEGIN TRAN ... COMMIT block to ensure reliability.

 

Somethng like the following should do it - it hasn't been tested, if it works try comparing both vesions with Query Analyzer's 'Show Execution Plan' function.

 

DELETE FROM Groups_Reference
FROM Managers AS M INNER JOIN Groups_Reference  as GR
on M.Manager_ID = GR.Manager_ID
WHERE Org_ID = @OrgID

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Many thanks, will try out your suggestion.

 

Mike55.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...