I'm rebuilding the database from mysql to MS SQL.
In mysql such a design exists in procedures:

DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish_cycle = TRUE;
OPEN cur;
SET finish_cycle = FALSE;
WHILE finish_cycle = FALSE DO
    -- какие то операции
  FETCH cur
END WHILE;
CLOSE cur;

Redone it like this:

    OPEN cur;
    FETCH NEXT FROM cur
    WHILE @@FETCH_STATUS = 0  
    BEGIN 
       -- какие то операции
       FETCH NEXT FROM cur
    END
    CLOSE cur_client_sessions 

And how can I interpret the error handler from mysql toMS SQL I can not understand? And do you need it at all? Or how to use it with try / catch?

closed as unclear what you're asking by sp_BlitzErik, Colin 't Hart, Philᵀᴹ, Marian, MDCCL Aug 10 at 15:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Can you explain what MySQL code does that you have trouble implementing on SQL Server? Most people might know one or another, but not both. – EzLo Aug 10 at 10:08
up vote 2 down vote accepted

Your redone code is effectively the same as the original CONTINUE HANDLER FOR NOT FOUND.

I see no exception handler for in the original code but you could surround the redone code in BEGIN TRY/END TRY followed by your exception handler in a BEGIN CATCH/ END CATCH; as described in the documentation to handle errors. This will ensure the T-SQL code doesn't continue execution following errors.

That said, it's generally best to avoid cursors and use set-based processing in T-SQL instead. This typically performs much better than RBAR processing and without the extra procedural coding effort.

  • Specifically, MS SQL sets the system variable @@FETCH_STATUS to a value other than 0 if there are no more rows to return from the cursor when the FETCH NEXT, just as your error handler set finish_cycle to TRUE. – RDFozz Aug 10 at 15:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.