SET NOCOUNT ON;
BEGIN TRY
    BEGIN TRANSACTION;

    IF EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'PDV_VD_TEF'
          AND COLUMN_NAME = 'NSU_SITEF'
          AND DATA_TYPE <> 'varchar'
    )
    BEGIN

        IF EXISTS (
            SELECT 1
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = 'PDV_VD_TEF'
              AND COLUMN_NAME = 'NSU_SITEF_TMP'
        )
        BEGIN
            EXEC('ALTER TABLE PDV_VD_TEF DROP COLUMN NSU_SITEF_TMP');
        END

        EXEC('ALTER TABLE PDV_VD_TEF ADD NSU_SITEF_TMP VARCHAR(50)');
        EXEC('UPDATE PDV_VD_TEF SET NSU_SITEF_TMP = CAST(NSU_SITEF AS VARCHAR(50))');
        EXEC('ALTER TABLE PDV_VD_TEF DROP COLUMN NSU_SITEF');
        EXEC sp_rename 'PDV_VD_TEF.NSU_SITEF_TMP', 'NSU_SITEF', 'COLUMN';
    END

    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION;

    PRINT 'Erro: ' + ERROR_MESSAGE();
END CATCH;