SQL Server触发器-使用deletetime将已删除的记录插入到另一个表中

发布于 2024-12-09 12:47:50 字数 230 浏览 1 评论 0原文

目前我有一个 Item 表和一个 ItemWaste 表。 两个表都会有一些字段,例如:名称、金额等。但是 ItemWaste 表还会多一个字段,即 TimeWasted。 我希望自动将 Item 表中的 DELETED 项目插入到 ItemWaste 表中,同时将删除时间插入到 TimeWasted 字段中。

我不知道该怎么做,是使用触发器吗???

希望能在这里得到一些帮助...感谢任何反馈...谢谢...

Currently I have a Item table and a ItemWaste table.
Both tables will have some fields, such as: Name, Amount, etc. But the ItemWaste table will have one more field, which is the TimeWasted.
I wish to automatically insert the DELETED item from the Item table to the ItemWaste table, and at the same time insert the deletion time to the TimeWasted field.

I got no idea how to do this, is it using trigger???

Hope can get some help here... Appreciate any feedback... Thanks....

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

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

发布评论

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

评论(4

一袭白衣梦中忆 2024-12-16 12:47:50

当然 - 不是问题。

您需要一个基本的 AFTER DELETE 触发器 - 像这样:

CREATE TRIGGER trg_ItemDelete 
ON dbo.Item 
AFTER DELETE 
AS
    INSERT INTO dbo.ItemWaste(Name, Amount, TimeWasted)
        SELECT d.Name, d.Amount, GETDATE()
        FROM Deleted d

仅此而已!要记住的一点:触发器被称为每批一次 - 例如,如果您一次删除 100 行,它将被称为一次并且伪表Deleted< /code> 将包含 100 行。触发器不会每行调用一次(这是一个常见的误解)。

Sure - not a problem.

You need a basic AFTER DELETE trigger - something like this:

CREATE TRIGGER trg_ItemDelete 
ON dbo.Item 
AFTER DELETE 
AS
    INSERT INTO dbo.ItemWaste(Name, Amount, TimeWasted)
        SELECT d.Name, d.Amount, GETDATE()
        FROM Deleted d

That's all there is! One point to remember: the trigger is called once per batch - e.g. if you delete 100 rows at once, it will be called once and the pseudo table Deleted will contain 100 rows. The trigger is not called once per row (a common misconception).

习ぎ惯性依靠 2024-12-16 12:47:50

是的,只需编写一个触发器,您就可以在另一个表中执行删除操作时插入一行,请查看触发器

Yes, simply by writting a trigger you can insert a row when an delete action is performed in another table, have a look at Triggers

离笑几人歌 2024-12-16 12:47:50

创建触发器
创建触发器 send_message_trigger
插入或删除后
ON 源表
对于每一行
开始
声明性别值 VARCHAR(10);

IF INSERTING THEN
    INSERT INTO trigger_test (name, gender)
    VALUES (NEW.name, NEW.gender);
    
    -- Send message about the new record added
    INSERT INTO trigger_test (message)
    VALUES ('New record added: ' || NEW.name);
END IF;

IF DELETING THEN
    -- Store the gender of the record being deleted
    SET gender_value = OLD.gender;
    
    DELETE FROM trigger_test
    WHERE name = OLD.name AND gender = OLD.gender;
    
    -- Send message about the record being deleted
    INSERT INTO trigger_test (message)
    VALUES ('Record deleted: ' || OLD.name || ' with gender: ' || gender_value);
END IF;

结尾;

Create trigger
CREATE TRIGGER send_message_trigger
AFTER INSERT OR DELETE
ON source_table
FOR EACH ROW
BEGIN
DECLARE gender_value VARCHAR(10);

IF INSERTING THEN
    INSERT INTO trigger_test (name, gender)
    VALUES (NEW.name, NEW.gender);
    
    -- Send message about the new record added
    INSERT INTO trigger_test (message)
    VALUES ('New record added: ' || NEW.name);
END IF;

IF DELETING THEN
    -- Store the gender of the record being deleted
    SET gender_value = OLD.gender;
    
    DELETE FROM trigger_test
    WHERE name = OLD.name AND gender = OLD.gender;
    
    -- Send message about the record being deleted
    INSERT INTO trigger_test (message)
    VALUES ('Record deleted: ' || OLD.name || ' with gender: ' || gender_value);
END IF;

END;

王权女流氓 2024-12-16 12:47:50

最好将默认日期时间约束添加到表中,这样一旦插入记录,系统就会自动添加当前日期时间。

句法:

alter table tbale_name add constraint  constraint_Name default Getdate() FOR ColumnName

It better to add the Default datetime constraint to table so tat once record is inserted system will automatically add the current datetime.

Syntax:

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