我之前发布了一个类似的问题 - 但似乎无法获得足够长的响应!对不起,如果我不应该再次发帖!
这是使用SQL Server 2008.我有一个名为invoice;
的表create table INVOICE(
INVOICE_ID numeric(5) PRIMARY KEY IDENTITY,
INVOICE_STATUS varchar(25) not null,
TRADER_STATUS varchar (25))
我想在输入插入时创建一个触发器;并且invoice_status ='pending' - 将trader_status更新(触发)为'open'。输入插入时;并且invoice_status ='overdue' - 将trader_status更新(触发)为'blocked'。
我的触发码;
CREATE TRIGGER [dbo].[test] on [dbo].[invoice]
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
declare @invoice_status varchar(25)
select @invoice_status = (select invoice_status from inserted WHERE invoice_status = 'PENDING')
insert into invoice (trader_status) values ('OPEN')
select @invoice_status = (select invoice_status from inserted WHERE invoice_status = 'OVERDUE')
insert into invoice (trader_status) values ('BLOCKED')
END
GO
非常感谢任何帮助!
答案 0 :(得分:1)
create trigger [dbo].[test] on [dbo].[invoice]
for insert
as
begin
update Invoice
set Trader_Status = 'OPEN'
where Invoice_Id in ( select Invoice_Id from inserted where Invoice_Status = 'PENDING' )
update Invoice
set Trader_Status = 'BLOCKED'
where Invoice_Id in ( select Invoice_Id from inserted where Invoice_Status = 'OVERDUE' )
end
请注意,这将处理由单个语句插入的多个行。