1

可能重复:
sql server 触发器帮助 - 同一张表更新

我使用的是 Microsoft SQL Server 2008。我有两张表信用和发票。

信用表代码:

create table CREDIT(
CREDIT_ID numeric(5) PRIMARY KEY IDENTITY,
CREDIT_STATUS varchar (25))

发票表的代码:

create table INVOICE(
INVOICE_ID numeric(5) PRIMARY KEY IDENTITY,
CREDIT_ID numeric(5) not null,
INVOICE_STATUS varchar(25) not null,
FOREIGN KEY (CREDIT_ID) REFERENCES CREDIT(CREDIT_ID))

信用的插入语句:

 insert into credit
 values ('open')

我想创建一个触发器 - 当在发票表上输入插入语句时 - 如果 invoice_status 为“待处理” - credit_status 保持“打开”。如果 invoice_status 为“逾期”,则 credit_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 = 'overdue')
insert into credit (credit_status) values ('Blocked')

select @invoice_status = (select invoice_status from inserted WHERE invoice_status = 'pending')
insert into credit (credit_status) values ('Open')  

END
GO

很抱歉有任何混淆!

任何帮助是极大的赞赏!

谢谢!

4

0 回答 0