Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

Tuesday, March 20, 2012

check if cursor exists

I declare a cusror named crInv inside a loop.
Since I open this cursor a lot of times I want to check if is already opened.
I try to use the Cursor_status function but it always returns -3.
The syntax is:

DECLARE crInv SCROL CURSOR FOR
SELECT Val1, Val2 FROM TABLE1 WHERE Val3=450

If Cursor_Status('local','crInv')>0 BEGIN
CLOSE crInv
DEALLOCATE crInv
END

This code is inside a loop.
If I PRINT Cursor_Status('local','crInv') before and after the DECLARE statement it always returns -3.
What is wrong??

Best regards,
ManolisDECLARE crInv LOCAL SCROLL CURSOR FOR ...

The typical default for a cursor is GLOBAL|||Sounds like an extremely expensive process...

Ever think about a set based process?

Can you post the "Loop" code?sql

Thursday, March 8, 2012

Check and send data with xp_sendmail

How yould i loop trought all the records in a table and fetching a specic record that is flagged and sending for each record found a email with that records data to a mail recipient. this should be part of a step in a sql job. PLZ HELPHad a little problem in understanding your phrases. You may check cursor in BOL for looping actions.|||create proc abc as
declare c1 cursor as select primary key from table name where condition
open c1
while (@.@.fetch_status =0)

begin
exec xp_sendmail ....
fetch next
end

something like this and schedule as job !!!