-- 步骤1: 检查并处理 F_unit_name 字段
IF EXISTS (SELECT * FROM sys.columns WHERE name = 'F_ship_name' AND object_id = OBJECT_ID('T_thirdparty_users'))
BEGIN
    IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = 'F_unit_name' AND object_id = OBJECT_ID('T_thirdparty_users'))
    BEGIN
        EXEC sp_rename 'T_thirdparty_users.F_ship_name', 'F_unit_name', 'COLUMN';
        PRINT '步骤1完成: 字段 F_ship_name 已重命名为 F_unit_name';
    END
    ELSE
    BEGIN
        PRINT '步骤1完成: 字段 F_unit_name 已存在';
    END
END
ELSE
BEGIN
    IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = 'F_unit_name' AND object_id = OBJECT_ID('T_thirdparty_users'))
    BEGIN
        ALTER TABLE T_thirdparty_users ADD F_unit_name VARCHAR(100);
        PRINT '步骤1完成: 字段 F_unit_name 已添加';
    END
    ELSE
    BEGIN
        PRINT '步骤1完成: 字段 F_unit_name 已存在';
    END
END
GO

-- 步骤2: 更新数据
UPDATE T_thirdparty_users 
SET F_unit_name = '大连中远' 
WHERE F_unit_name IS NULL OR F_unit_name = '';

PRINT '步骤2完成: 更新了 ' + CAST(@@ROWCOUNT AS VARCHAR) + ' 条记录';
GO

-- 步骤3: 验证结果
SELECT F_ID, F_real_name, F_unit_name 
FROM T_thirdparty_users 
WHERE F_unit_name = '大连中远';

PRINT '步骤3完成: 验证完成';
GO