June 10, 2016

Cursor Sample

SQL Server

DECLARE @EMPLOYEE_ID AS VARCHAR (25);

DECLARE @EMPLOYEE_NAME AS VARCHAR (100);

DECLARE @EMAIL_ID AS VARCHAR (25);

DECLARE @getAccountID AS CURSOR;

SET @getAccountID = CURSOR
    FOR SELECT t.EMPLOYEE_ID,
               t.EMPLOYEE_NAME,
               t.EMAIL_ID
        FROM   [dbo].[EMPLOYEES_TIBCO] AS t
               INNER JOIN
               [dbo].[Vms_User_Details] AS v
               ON t.EMPLOYEE_ID = v.UserId
        WHERE  t.EMAIL_ID IS NOT NULL;

OPEN @getAccountID;

FETCH NEXT FROM @getAccountID INTO @EMPLOYEE_ID, @EMPLOYEE_NAME, @EMAIL_ID;

WHILE @@FETCH_STATUS = 0
    BEGIN
        UPDATE  [Vms_User_Details]
            SET UserName = @EMPLOYEE_NAME,
                E_Mail   = @EMAIL_ID
        WHERE   UserId = @EMPLOYEE_ID;
        FETCH NEXT FROM @getAccountID INTO @EMPLOYEE_ID, @EMPLOYEE_NAME, @EMAIL_ID;
    END

CLOSE @getAccountID;

DEALLOCATE @getAccountID;

No comments: