• To use a cursor, you basically need to use a while loop.

    If you're assigning values to your variables inside the loopp using select statements, you might want to reinitialize your variables. That's because if no rows are returned from the select statement, the variable won't change its value.

    To be clear, here's an example.

    DECLARE @db int

    --Initialize the variable

    SET @db = 1

    --Check the value

    SELECT @db

    --Try to assign value to a variable with a select that won't return rows

    SELECT @db = object_id

    FROM tempdb.sys.objects

    WHERE 1 = 2

    ORDER BY object_id

    --Check the value

    SELECT @db

    --Assign value to a variable with a select that returns rows

    SELECT @db = object_id

    FROM tempdb.sys.objects

    ORDER BY object_id

    --Check the value

    SELECT @db

    In another set of ideas, your cursor might be causing slow performance and if you post the complete code with ddl and sample data for the tables involved, we can help you to change it to a solution that will outperform your current method.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2