简体   繁体   English

如何在SQL Server 2008 R2中添加具有其他列值作为默认值的列?

[英]How to add a column which has another column value as default value in SQL Server 2008 R2?

I have a table emp which has 4 columns : emp_cd, emp_name, updated_by, update_dt . 我有一个表emp ,它有4列: emp_cd, emp_name, updated_by, update_dt

Now I am to add another column named as inserted_by . 现在,我要添加另一列名为inserted_by

When I insert anything in table, the value of inserted_by column should be set same as updated_by . 当我在表中插入任何东西,价值inserted_by列应设置相同updated_by

When I update table update_by column value will be updated but inserted_by column value should remain unchanged. 当我更新表update_by列值将被更新,但inserted_by列的值必须保持不变。

Use a trigger on INSERT: 在INSERT上使用触发器:

CREATE TRIGGER trig_Insert_Emp
ON [emp]
FOR INSERT
AS
Begin
    Update [emp] 
    Set inserted_by = ( Your_Values_Or_Subquery )
    from Inserted i
    Left Join emp e
    on i.emp_cd = e.emp_cd 
End

Take a look to AFTER INSERT too. 也来看看AFTER INSERT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL Server 2008 r2中添加列值? - How to add column value in SQL Server 2008 r2? SQL Server 2008 R2,为另一列的每个不同值选择一个列的一个值 - SQL server 2008 R2, select one value of a column for each distinct value of another column 通过另一个表中的列更新SQL Server 2008 R2中的表,并且还需要求和值 - Update a table in SQL Server 2008 R2 by a column in another table and also need a sum value 根据另一列的最大值选择一个不同的行SQL Server 2008 R2 - Select a distinct row based on the max value of another column SQL Server 2008 R2 SQL SERVER 2008 R2在另一列中查找列词 - SQL SERVER 2008 R2 find column words in another column 不同列上的不同值的最大列值SQL Server 2008 R2 - MAX Column Value for Distinct Value on Different column SQL Server 2008 R2 计算列中值的出现并添加到SQL 2008 R2中的新列 - Count occurence of a value in a column and add to new column in SQL 2008 R2 SQL Server 2008 R2:查找column1中存在column2值的行 - SQL Server 2008 R2: Find rows where column2 value present in column1 在WHERE子句中使用CASE? SQL Server 2008 R2根据值按不同的列筛选查询 - CASE in WHERE clause? Filter query by different column depending a value SQL Server 2008 R2 如何在SQL Server 2008 R2中使单列成行? - How to make Single Column into Rows in Sql Server 2008 R2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM