如何在执行INSERT INTO ... SELECT FROM时插入自定义值

时间:2016-09-29 09:54:23

标签: sql sql-server

我想在执行:view 子句

时插入自定义值和表列
INSERT INTO ...SELECT FROM ... WHERE

我想使用自定义值

插入INSERT INTO RoleMappingEmployee_Delete_History ( RoleMappingEmployeeKey, SrKey, RoleKey, SubmittedDate, SubmittedBy, IsActive, DeletedBy, DeletedDateTime) SELECT RoleMappingEmployeeKey, SrKey, RoleKey, SubmittedDate, SubmittedBy, IsActive, DeletedBy, DeletedDateTime FROM RoleMappingEmployee WHERE RoleMappingEmployeeKey IN (25902,38188,25887) 的值

2 个答案:

答案 0 :(得分:4)

INSERT INTO RoleMappingEmployee_Delete_History (
    RoleMappingEmployeeKey,
    SrKey,
    RoleKey,
    SubmittedDate,
    SubmittedBy,
    IsActive,
    DeletedBy,
    DeletedDateTime)
SELECT 
    RoleMappingEmployeeKey,
    SrKey,
    RoleKey,
    SubmittedDate,
    SubmittedBy,
    IsActive,
    'peter',
    getdate()
FROM
    RoleMappingEmployee
WHERE
    RoleMappingEmployeeKey IN (25902,38188,25887)

答案 1 :(得分:0)

您还可以使用ORIGINAL_LOGIN函数设置DeletedBy,它将返回执行操作的用户名,并帮助避免每次为此设置值或参数。类似地,您可以使用CURRENT_TIMESTAMP在DeletedDateTime中使用alternative,如下所示:

INSERT INTO RoleMappingEmployee_Delete_History (
    RoleMappingEmployeeKey,
    SrKey,
    RoleKey,
    SubmittedDate,
    SubmittedBy,
    IsActive,
    DeletedBy,
    DeletedDateTime)
SELECT 
    RoleMappingEmployeeKey,
    SrKey,
    RoleKey,
    SubmittedDate,
    SubmittedBy,
    IsActive,
    ORIGINAL_LOGIN(),
    CURRENT_TIMESTAMP
FROM
    RoleMappingEmployee
WHERE RoleMappingEmployeeKey IN (25902,38188,25887)