插入前触发 [英] Trigger before Insert

查看:107
本文介绍了插入前触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在SQL Server 2008中有一个表(Customer)。此表中有4列ID,FirstName, LastName,代码。



现在我希望每当有人在列(ID,FirstName和LastName)中插入数据时,代码列应自动填充有价值。



代码列中的值为=> ID+FirstName的第一个字母+LastName的第一个字母。



例如 - 如果我在ID字段中插入101,John在FirstName字段和LastName字段中的Lambart中,Code列应该具有值101JL。





我想要通过触发来实现。



请帮助。谢谢高级:)

解决方案

你可以使用而不是触发器

为此

http ://blog.sqlauthority.com/2013/01/24/sql-server-how-to-use-instead-of-trigger-guest-post-by-vikas-munjal-koenig-solutions/ [ ^ ]


我可能会让你偏离这个问题。您可以考虑使用持久性计算列。



http://blog.sqlauthority.com/2010/08/03/sql-server-computed-column-persisted-and%C2%A0performance/ [ ^ ]


在这种情况下,您不需要触发器 - 您可以只使用计算列 - 更容易,更便宜



计算栏 [< a href =http://technet.microsoft.com/en-us/library/ms191250(v=sql.105).aspx\"target =_ blanktitle =New Window> ^ ]



将列添加到表中,然后在计算列规范中添加以下内容。



转换( varchar(10),[ID])+左([Fir stName],1)+ Left([LastName],1)





该列仅有效显示 - 您无法插入或更新colun,但更改任何引用的列将自动调整计算的值

Hi all,

I have a table (Customer) in SQL Server 2008. There are 4 columns in this table "ID", "FirstName", "LastName", "Code".

Now I want that whenever someone insert data in columns (ID, FirstName and LastName), the "Code" column should be automatically filled with a value.

The value in "Code" column will be => "ID" + First letter of "FirstName" + First letter of "LastName".

For example - If I insert "101" in ID field, "John" in FirstName field and "Lambart" in LastName field then the "Code" column should have a value "101JL".


I want to do it by trigger.

Please help. Thanks in advanced :)

解决方案

you ca use instead of trigger
For this
http://blog.sqlauthority.com/2013/01/24/sql-server-how-to-use-instead-of-trigger-guest-post-by-vikas-munjal-koenig-solutions/[^]


Might be I am deviating you from the issue. You can think of having computed column with persistence.

http://blog.sqlauthority.com/2010/08/03/sql-server-computed-column-persisted-and%C2%A0performance/[^]


In this case you don't need a trigger - you can just have a computed column - much easier and less expensive

Computed columns[^]

Add the column to the table, then in the Computed Column specification add the following.

Convert(varchar(10), [ID]) + Left([FirstName],1) + Left([LastName],1)


The column is effectively display only - you cannot insert to or update the colun, but changes to any of the referenced columns will automatically adjust the computed vale


这篇关于插入前触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆