1. <tfoot id='75wEZ'></tfoot>

    <i id='75wEZ'><tr id='75wEZ'><dt id='75wEZ'><q id='75wEZ'><span id='75wEZ'><b id='75wEZ'><form id='75wEZ'><ins id='75wEZ'></ins><ul id='75wEZ'></ul><sub id='75wEZ'></sub></form><legend id='75wEZ'></legend><bdo id='75wEZ'><pre id='75wEZ'><center id='75wEZ'></center></pre></bdo></b><th id='75wEZ'></th></span></q></dt></tr></i><div id='75wEZ'><tfoot id='75wEZ'></tfoot><dl id='75wEZ'><fieldset id='75wEZ'></fieldset></dl></div>
    <legend id='75wEZ'><style id='75wEZ'><dir id='75wEZ'><q id='75wEZ'></q></dir></style></legend>

  2. <small id='75wEZ'></small><noframes id='75wEZ'>

        <bdo id='75wEZ'></bdo><ul id='75wEZ'></ul>
    1. SQL Server - 插入触发器后 - 更新同一表中的另一列

      SQL Server - after insert trigger - update another column in the same table(SQL Server - 插入触发器后 - 更新同一表中的另一列)
      <legend id='AGUCg'><style id='AGUCg'><dir id='AGUCg'><q id='AGUCg'></q></dir></style></legend>
          <bdo id='AGUCg'></bdo><ul id='AGUCg'></ul>
            <tbody id='AGUCg'></tbody>
        • <i id='AGUCg'><tr id='AGUCg'><dt id='AGUCg'><q id='AGUCg'><span id='AGUCg'><b id='AGUCg'><form id='AGUCg'><ins id='AGUCg'></ins><ul id='AGUCg'></ul><sub id='AGUCg'></sub></form><legend id='AGUCg'></legend><bdo id='AGUCg'><pre id='AGUCg'><center id='AGUCg'></center></pre></bdo></b><th id='AGUCg'></th></span></q></dt></tr></i><div id='AGUCg'><tfoot id='AGUCg'></tfoot><dl id='AGUCg'><fieldset id='AGUCg'></fieldset></dl></div>

                <small id='AGUCg'></small><noframes id='AGUCg'>

                <tfoot id='AGUCg'></tfoot>
                本文介绍了SQL Server - 插入触发器后 - 更新同一表中的另一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有这个数据库触发器:

                I've got this database trigger:

                CREATE TRIGGER setDescToUpper
                ON part_numbers
                 AFTER INSERT,UPDATE
                AS
                DECLARE @PnumPkid int, @PDesc nvarchar(128)
                
                SET @PnumPkid = (SELECT pnum_pkid FROM inserted)
                SET @PDesc = (SELECT UPPER(part_description) FROM inserted)
                
                UPDATE part_numbers set part_description_upper = @PDesc WHERE pnum_pkid=@PnumPkid
                
                GO
                

                这是个坏主意吗? 即更新同一个表中的列.我希望它为插入和更新触发.

                Is this a bad idea? That is to update a column on the same table. I want it to fire for both insert and update.

                它有效,我只是害怕周期性情况.触发器内部的更新会一次又一次地触发触发器.这会发生吗?

                It works, I'm just afraid of a cyclical situation. The update, inside the trigger, fires the trigger, and again and again. Will that happen?

                请不要挑剔大写的东西.疯狂的情况.

                Please, don't nitpick at the upper case thing. Crazy situation.

                推荐答案

                这取决于当前在 DB 上设置的触发器的递归级别.

                It depends on the recursion level for triggers currently set on the DB.

                如果你这样做:

                SP_CONFIGURE 'nested_triggers',0
                GO
                RECONFIGURE
                GO
                

                或者这个:

                ALTER DATABASE db_name
                SET RECURSIVE_TRIGGERS OFF
                

                不会再次调用上面的触发器,这样您就很安全了(除非您陷入某种僵局;那是可能的,但也许我错了).

                That trigger above won't be called again, and you would be safe (unless you get into some kind of deadlock; that could be possible but maybe I'm wrong).

                不过,我认为这是个好主意.更好的选择是使用 INSTEAD OF 触发器.这样您就可以避免对数据库执行第一次(手动)更新.只会执行触发器中定义的那个.

                Still, I do not think this is a good idea. A better option would be using an INSTEAD OF trigger. That way you would avoid executing the first (manual) update over the DB. Only the one defined inside the trigger would be executed.

                INSTEAD OF INSERT 触发器如下所示:

                An INSTEAD OF INSERT trigger would be like this:

                CREATE TRIGGER setDescToUpper ON part_numbers
                INSTEAD OF INSERT
                AS
                BEGIN
                    INSERT INTO part_numbers (
                        colA,
                        colB,
                        part_description
                    ) SELECT
                        colA,
                        colB,
                        UPPER(part_description)
                    ) FROM
                        INSERTED
                END
                GO
                

                这会自动替换"原来的 INSERT 语句,并将显式 UPPER 调用应用于 part_description 字段.

                This would automagically "replace" the original INSERT statement by this one, with an explicit UPPER call applied to the part_description field.

                INSTEAD OF UPDATE 触发器类似(我不建议您创建单个触发器,将它们分开).

                An INSTEAD OF UPDATE trigger would be similar (and I don't advise you to create a single trigger, keep them separated).

                此外,这解决了@Martin 评论:它适用于多行插入/更新(您的示例没有).

                Also, this addresses @Martin comment: it works for multirow inserts/updates (your example does not).

                这篇关于SQL Server - 插入触发器后 - 更新同一表中的另一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Building a comma separated list?(建立一个逗号分隔的列表?)
                Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

                  <small id='F77LG'></small><noframes id='F77LG'>

                  1. <legend id='F77LG'><style id='F77LG'><dir id='F77LG'><q id='F77LG'></q></dir></style></legend>
                    <i id='F77LG'><tr id='F77LG'><dt id='F77LG'><q id='F77LG'><span id='F77LG'><b id='F77LG'><form id='F77LG'><ins id='F77LG'></ins><ul id='F77LG'></ul><sub id='F77LG'></sub></form><legend id='F77LG'></legend><bdo id='F77LG'><pre id='F77LG'><center id='F77LG'></center></pre></bdo></b><th id='F77LG'></th></span></q></dt></tr></i><div id='F77LG'><tfoot id='F77LG'></tfoot><dl id='F77LG'><fieldset id='F77LG'></fieldset></dl></div>
                          <bdo id='F77LG'></bdo><ul id='F77LG'></ul>
                        • <tfoot id='F77LG'></tfoot>
                            <tbody id='F77LG'></tbody>