如何创建触发器? [英] How to create a Trigger?

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

问题描述

我在SQL Server 2005中有两个表.第一个表是"stock",第二个表是"order".当用户在订单表中输入某些内容时,会自动更新某些库存值.

例如
库存表中有一列:物料,在物料值中存储的是500
在订单表中只有一列:项目
现在,如果我将订单表的商品列值输入为250,则应该将库存商品的列自动更新为250.

我知道可以使用触发器来完成此操作,但我不知道如何创建触发器.所以请帮帮我.

在此先感谢

I have two tables in SQL Server 2005. First table is ''stock'' and second table is ''order''. When user enter something in order table there is an automatic update to some stock value.

e.g
In stock table there is one column: Item and in Item value stored is 500
In order table there is one column: Item
Now, if i enter a order table''s item column value as 250 then there should be an automatic update to stock value item column to 250.

I know this can be done using trigger but i do not know how to create trigger. So please help me.

Thanks in advance

推荐答案

请参考此处以创建触发器语法和其他帮助.

刚刚在codeproject中的此处搜索了一篇非常不错的文章,
http://www.codeproject.com/KB/database/OverviewOfTriggers.aspx

您可能会提及的其他人,

http://www.dotnetspark.com/kb/657-how-to-create- trigger.aspx

http://msdn.microsoft.com/en-us/library/ms189799.aspx


谢谢
Arindam D Tewary
Please refer here for create trigger syntax and other helps.

Just searched for a very nice article here inside codeproject,
http://www.codeproject.com/KB/database/OverviewOfTriggers.aspx

Others you might refer,

http://www.dotnetspark.com/kb/657-how-to-create-trigger.aspx

http://msdn.microsoft.com/en-us/library/ms189799.aspx


Thanks
Arindam D Tewary


最好建议您创建一个存储过程,该存储过程可以在事务内为您执行此操作.因此,您先更新表1,然后再更新表2.如果两个更新都可以,则提交事务.无论是在这里还是通过Google搜索,都有很多示例.

您会在触发器上收到很多不同的看法:我不在乎它们,但是许多人强烈不喜欢它们,因此请采纳最适合您需求的建议.
You may be better advised to create a stored procedure which does this for you inside a transaction. So you update table 1 then update table2. If both updates are okay then commit the transaction. There are many examples of this both here and by searching with Google.

You''ll get a lot of mixed opinions on triggers: I don''t mind them but many people dislike them intensely so take the piece of advice that best suits your needs.


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

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