SQL 触发更新 table
SQL trigger for updating a table
我有3个表如下:
ticket
:身份证,备注
action
: ID, 日期, ticket_ID, 类型
action_type
: ID, type, name
每次将新记录插入 action
时,我都需要将 ticket.Note
与 action.date
和 action_type.name
连接起来。但仅适用于某些操作类型。
所以采取以下情况:
ticket
是: 0056, someNote
action
是:001234、2015 年 6 月 30 日 14:00:22、0056、4
action_type
是:004, 4, 创建
我需要 #0056
ticket.Note
才能成为 someNote;06-30-2015 create
我试过下面的代码,但我得到 incorrect syntax
错误:
CREATE TRIGGER trg_notes
ON action
AFTER INSERT AS
BEGIN
UPDATE ticket
SET
Note =
Note +
';' +
CONVERT(VARCHAR(10),(select date from inserted),110) +
(select
t.name
from
action_type t
LEFT OUTER JOIN inserted i ON i.action = t.ID
WHERE
i.action = t.ID)
where
ticket.ID = inserted.ticket_ID
AND
(inserted.action = 4
OR
inserted.action = 7
OR
inserted.action = 8
OR
inserted.action = 11
OR
inserted.action = 12)
END
我想你想要实现的是:
CREATE TRIGGER trg_notes
ON action
AFTER INSERT AS
BEGIN
UPDATE t
SET t.Note = t.Note + ';' + CONVERT(VARCHAR(10),i.[date],110) + at.name
FROM inserted i
inner join tickets t ON i.ticket_ID = t.ID
inner join action_type at ON i.[type] = at.[type]
WHERE inserted.[type] IN(4,7,8,11,12)
END
我有3个表如下:
ticket
:身份证,备注action
: ID, 日期, ticket_ID, 类型action_type
: ID, type, name
每次将新记录插入 action
时,我都需要将 ticket.Note
与 action.date
和 action_type.name
连接起来。但仅适用于某些操作类型。
所以采取以下情况:
ticket
是: 0056, someNoteaction
是:001234、2015 年 6 月 30 日 14:00:22、0056、4action_type
是:004, 4, 创建
我需要 #0056
ticket.Note
才能成为 someNote;06-30-2015 create
我试过下面的代码,但我得到 incorrect syntax
错误:
CREATE TRIGGER trg_notes
ON action
AFTER INSERT AS
BEGIN
UPDATE ticket
SET
Note =
Note +
';' +
CONVERT(VARCHAR(10),(select date from inserted),110) +
(select
t.name
from
action_type t
LEFT OUTER JOIN inserted i ON i.action = t.ID
WHERE
i.action = t.ID)
where
ticket.ID = inserted.ticket_ID
AND
(inserted.action = 4
OR
inserted.action = 7
OR
inserted.action = 8
OR
inserted.action = 11
OR
inserted.action = 12)
END
我想你想要实现的是:
CREATE TRIGGER trg_notes
ON action
AFTER INSERT AS
BEGIN
UPDATE t
SET t.Note = t.Note + ';' + CONVERT(VARCHAR(10),i.[date],110) + at.name
FROM inserted i
inner join tickets t ON i.ticket_ID = t.ID
inner join action_type at ON i.[type] = at.[type]
WHERE inserted.[type] IN(4,7,8,11,12)
END