[英]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.