9

i would like to insert custom values along with table columns when i perform INSERT INTO ...SELECT FROM ... WHERE clause

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)

i would like to insert values for DeletedBy,DeletedDateTime with custom values

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
coolstoner
  • 719
  • 2
  • 9
  • 20

2 Answers2

17
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)
juergen d
  • 201,996
  • 37
  • 293
  • 362
0

You can also set DeletedBy using ORIGINAL_LOGIN function, it will return the username who is performing the operation and help to avoid setting up value every time or parameter for this. Similarly you can use alternative in DeletedDateTime using CURRENT_TIMESTAMP as below:

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)
Shushil Bohara
  • 5,556
  • 2
  • 15
  • 32