如何在 Transact SQL 中实现这一点(SQL Server 2008)

发布于 2024-09-04 10:23:24 字数 541 浏览 2 评论 0原文

我只想要一个像postgresql版本这样的更新触发器...在我看来没有新的。和旧的。在 MSSQL 中?

CREATE OR REPLACE FUNCTION "public"."ts_create" () RETURNS trigger AS
DECLARE
BEGIN
  NEW.ctime := now();
  RETURN NEW;
END;

已经用谷歌搜索了,但不幸的是没有找到任何东西......想法?

更新: ……,这样吗?

CREATE TRIGGER tr01 ON Auctions for insert
As
update auctions set mtime = GETDATE() where Id = inserted.Id;

或者这个:

CREATE TRIGGER tr01 ON Auctions for insert
As
inserted.ctime := GETDATE();

当然这两者都不起作用;)

I just want an update trigger like this postgresql version... It seems to me there is no NEW. and OLD. in MSSQL?

CREATE OR REPLACE FUNCTION "public"."ts_create" () RETURNS trigger AS
DECLARE
BEGIN
  NEW.ctime := now();
  RETURN NEW;
END;

Googled already, but nothing to be found unfortunately... Ideas?

Update:
Sth, like this?

CREATE TRIGGER tr01 ON Auctions for insert
As
update auctions set mtime = GETDATE() where Id = inserted.Id;

or this:

CREATE TRIGGER tr01 ON Auctions for insert
As
inserted.ctime := GETDATE();

bothg dont work of course ;)

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

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

发布评论

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

评论(3

那片花海 2024-09-11 10:23:24

在 SQL Server 中,伪表称为 已插入和更新

但是,对于您的特定示例,我将在 ctime 列上使用默认约束:

CREATE TRIGGER schema.tbl_name_insert ON schema.tbl_name
    AFTER INSERT
    AS
        SET NOCOUNT ON

        UPDATE schema.tbl_name
        SET mtime = getdate()
        FROM schema.tbl_name
        INNER JOIN JOIN INSERTED
            ON schema.tbl_name.PK = INSERTED.PK

CREATE TRIGGER schema.tbl_name_insert ON schema.tbl_name
    AFTER INSERT
    AS
        SET NOCOUNT ON

        UPDATE schema.tbl_name
        SET mtime = getdate()
        FROM schema.tbl_name
        WHERE schema.tbl_name.PK IN (SELECT PK FROM INSERTED)

触发器针对整个 INSERT 语句触发一次,因此,一般来说,INSERT AND DELETED 表可能包含多行。

In SQL Server the pseudo tables are called INSERTED and UPDATED.

I would use a default constraint on the ctime column for your particular example, however:

CREATE TRIGGER schema.tbl_name_insert ON schema.tbl_name
    AFTER INSERT
    AS
        SET NOCOUNT ON

        UPDATE schema.tbl_name
        SET mtime = getdate()
        FROM schema.tbl_name
        INNER JOIN JOIN INSERTED
            ON schema.tbl_name.PK = INSERTED.PK

or

CREATE TRIGGER schema.tbl_name_insert ON schema.tbl_name
    AFTER INSERT
    AS
        SET NOCOUNT ON

        UPDATE schema.tbl_name
        SET mtime = getdate()
        FROM schema.tbl_name
        WHERE schema.tbl_name.PK IN (SELECT PK FROM INSERTED)

A trigger fires once for an entire INSERT statement, so, in general, the INSERT AND DELETED tables may contain multiple rows.

堇年纸鸢 2024-09-11 10:23:24

对于触发器,SQL 提供名为“inserted”和“deleted”的可用虚拟表。这是我已经使用了几个月的示例例程触发器(SQL 2005):(

CREATE TRIGGER [MAP].[TR_iu_MyTable__LastUpdated]
 on [dbo].[MyTable]
 after insert, update

AS

    SET NOCOUNT on

    UPDATE MAP.MyTable
     set LastUpdated = getdate()
     where MyTableId in (select MyTableId from inserted)

GO

请注意,这是一个“之后”触发器,因此我只需要运行更新。)

For triggers, SQL makes available virtual tables named "inserted" and "deleted". Here's a sample routine trigger I've had in place for months (SQL 2005):

CREATE TRIGGER [MAP].[TR_iu_MyTable__LastUpdated]
 on [dbo].[MyTable]
 after insert, update

AS

    SET NOCOUNT on

    UPDATE MAP.MyTable
     set LastUpdated = getdate()
     where MyTableId in (select MyTableId from inserted)

GO

(Note that this is an "after" trigger, so I only need to run an UPDATE.)

情未る 2024-09-11 10:23:24

首先,对于插入,您可以将默认值设置为 GETDATE()。

然后在更新时,尝试这样的事情:

CREATE TRIGGER TimeUpdater ON Auctions FOR UPDATE
AS
UPDATE auctions 
SET mtime = GETDATE() 
FROM UPDATED u
WHERE Id = u.Id

First, for the insert you can set a default value to be GETDATE().

Then on update, try something like this:

CREATE TRIGGER TimeUpdater ON Auctions FOR UPDATE
AS
UPDATE auctions 
SET mtime = GETDATE() 
FROM UPDATED u
WHERE Id = u.Id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文