我应该使用 MS SQL Management Studio 中的模板来创建新触发器吗?

发布于 2024-09-24 08:49:39 字数 882 浏览 2 评论 0原文

如果您使用 GUI 在 MS SQL Management Studio 中创建新触发器,它会为您提供此模板:

--====================================
--  Create database trigger template 
--====================================
USE <database_name, sysname, AdventureWorks>
GO

IF EXISTS(
  SELECT *
    FROM sys.triggers
   WHERE name = N'<trigger_name, sysname, table_alter_drop_safety>'
     AND parent_class_desc = N'DATABASE'
)
    DROP TRIGGER <trigger_name, sysname, table_alter_drop_safety> ON DATABASE
GO

CREATE TRIGGER <trigger_name, sysname, table_alter_drop_safety> ON DATABASE 
    FOR <data_definition_statements, , DROP_TABLE, ALTER_TABLE> 
AS 
IF IS_MEMBER ('db_owner') = 0
BEGIN
   PRINT 'You must ask your DBA to drop or alter tables!' 
   ROLLBACK TRANSACTION
END
GO

我应该使用此模板吗?

我对触发器一无所知,但我认为我需要使用它们。本例的目的是在插入表时,我需要更新其中一个字段。

请帮助我开始!

If you create a new trigger in MS SQL Management Studio by using the GUI, it gives you this template:

--====================================
--  Create database trigger template 
--====================================
USE <database_name, sysname, AdventureWorks>
GO

IF EXISTS(
  SELECT *
    FROM sys.triggers
   WHERE name = N'<trigger_name, sysname, table_alter_drop_safety>'
     AND parent_class_desc = N'DATABASE'
)
    DROP TRIGGER <trigger_name, sysname, table_alter_drop_safety> ON DATABASE
GO

CREATE TRIGGER <trigger_name, sysname, table_alter_drop_safety> ON DATABASE 
    FOR <data_definition_statements, , DROP_TABLE, ALTER_TABLE> 
AS 
IF IS_MEMBER ('db_owner') = 0
BEGIN
   PRINT 'You must ask your DBA to drop or alter tables!' 
   ROLLBACK TRANSACTION
END
GO

Should I use this template?

I dont know anything about triggers, but I think I need to use them. The purpose in this case is that on an insert to the table, I need to update one of the fields.

Please help me get started!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我乃一代侩神 2024-10-01 08:49:39

好吧,如果您想要一个普通的触发器,即对表本身进行结构更改的触发器,那么首先这是错误的模板。

如果您决定执行影响数据(而不是结构)的触发器,则您需要了解几件事。首先,也是迄今为止最关键的一点,触发器对数据集进行操作,而不是一次对一行数据进行操作。您必须编写任何触发器来处理多行插入、更新或删除。如果您最终使用任何代码将插入或删除的值设置为变量,那么如果涉及多条记录,则 99% 的可能性它将无法正常工作。

您问插入或删除了什么?这是您需要了解的关于触发器的下一件事,有两个伪表(插入和删除)仅在触发器(或输出子句)中可用,其中包含要插入的新信息或更新的值(在插入的表中) )以及旧信息被删除或被更新更改(在已删除的表中)。因此,插入在插入中具有值,删除在删除中具有值,而更新在两者中都有值。在触发器中使用这些来提取需要更改的值。

OK to begin with that is the wrong template if you want an ordinary trigger that one is a trigger on making structural changes to the table itself.

If you decide to do a trigger that affects data (as opposed to structure), there are several things you need to know. First and by far the most critical, triggers operate on sets of data not one row at time. You must write any trigger to handle multiple row inserts.updates or deletes. If you end up with any code setting the value in inserted or deleted to a variable, there is a 99% chance it will not work properly if multiple records are involved.

What is inserted or deleted you ask? That is the next thing you need to know about triggers, there are two pseudotables (inserted and deleted) that are only available in a trigger (or an output clause) which contain the new information being inserted or the updated values (in the inserted table) and the old information being deleted or being changed by an update (in the deleted table). So an insert has values in inserted, a delete has values in deleted and an update has values in both. Use these in your trigger to pull the values you need to change.

弥繁 2024-10-01 08:49:39

既然你对触发器一无所知,我会说不,不要使用模板。

阅读创建触发器的书籍在线页面并手动编写触发器。

该模板代码中的内容可能比您实际需要的更多。阅读手册并保持简单。

Since you don't know anything about triggers, I would say no, don't use the template.

Read the books online page for Create Trigger and write the trigger by hand.

There is probably more in that template code than you actually need. Read the manual and keep it simple.

灵芸 2024-10-01 08:49:39

如果您对触发器一无所知,那么我强烈建议您实施触发器之前先阅读它们。掌握正确的触发器,它们可以让您的生活变得更加轻松;如果弄错了,触发器会给你带来很多麻烦。

我建议从本教程开始

http://www. sqlteam.com/article/an-introduction-to-triggers-part-i

您可以使用上述 SQL 作为模板,也可以简单地编写自己的 SQL。我建议您自己编写,因为您会明白自己在做什么。显然,只有在认真阅读了触发器之后才可以执行此操作。也请查看 MSDN

If you don't know anything about triggers then I would strongly suggest that you read up on them before implementing them. Get Triggers right and they can make your life a lot easier; get it wrong and Triggers will cause you a lot of trouble.

I would suggest starting off with this tutorial

http://www.sqlteam.com/article/an-introduction-to-triggers-part-i

You can use the above SQL as a template or you can simply write your own. I would suggest you write your own as you'll understand what you are doing. Obviously only do this after you have done some serious reading on triggers. Check out MSDN too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文