0

我之前发布了一个类似的问题 - 但似乎无法得到足够长的回复!对不起,如果我不应该再次发布!

这是使用 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

任何帮助是极大的赞赏!

4

1 回答 1

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

请注意,这将处理由单个语句插入的多个行。

于 2012-11-26T21:41:02.767 回答