简体   繁体   English

T-SQL插入触发器在多个表上插入,更新if条件

[英]T-SQL Insert Trigger to insert,update on if condition on multiple tables

I'm having a few issues trying to resolve an SQL Trigger to automatically set a user to blocked & create the block record including date in another table, if their Due date is equal to a set date. 我有一些问题试图解决SQL触发器自动设置用户被阻止并创建包含另一个表中的日期的块记录,如果他们的截止日期等于设置日期。

The issue is that when the trigger is set off by an insert, the print statements are executed and the insert occurs, but the insert into the table does not, or the update statement ? 问题是当插入触发器触发时,执行print语句并发生插入,但是插入到表中没有,或者更新语句? Can anyone explain why ? 有谁能解释为什么?

Note: Both the insert and the Update statement are fine when executed by themselves. 注意:insert和Update语句在自己执行时都可以。

ACCOUNT TABLE 账户表

CREATE TABLE [dbo].[Account](
[AccountNo] [int] IDENTITY(1,1) NOT NULL,
[CustomerNo] [int] NOT NULL,
[PaymentNo] [int] NULL,
[CreditNo] [int] NULL,
[BlockID] [dbo].[number] NULL,
[Balence] [dbo].[currency] NOT NULL,
[AmountDue] [dbo].[currency] NOT NULL,
[DueDate] [dbo].[dates] NULL,
[AutherisedBy] [nvarchar](50) NOT NULL,
[DateCreated] [date] NOT NULL,

BLOCKEDUSER TABLE BLOCKEDUSER TABLE

CREATE TABLE [dbo].[BlockedUsers](
[BlockID] [int] IDENTITY(1,1) NOT NULL,
[DateEnforced] [dbo].[dates] NOT NULL,
[Blocked] [dbo].[switch] NOT NULL,

TRIGGER 触发

ALTER TRIGGER [dbo].[Add_Blocked_User]
ON [dbo].[Account]
FOR INSERT
AS  
BEGIN
SET NOCOUNT ON;

Declare @ID int
Select @ID = [AccountNo] from inserted  
If(Select [DueDate] from inserted) = '2011-01-01'

INSERT INTO dbo.BlockedUsers(DateEnforced,Blocked)
VALUES (GETDATE(),1)
PRINT 'New Block Date Added'

UPDATE Account 
Set BlockID = IDENT_CURRENT('BlockID')
where @ID = @ID
PRINT 'Account Blocked'

END

GO

Fully Working Example : Completed using Help Below. 完全工作示例:使用下面的帮助完成。

ALTER TRIGGER [dbo].[Add_Blocked_User]
ON [dbo].[Account]
AFTER INSERT
AS 
BEGIN

SET NOCOUNT ON;

Declare @ID int
Select @ID = [AccountNo] from inserted  
 If(Select [DueDate] from inserted)Not Between (select CONVERT(date, getdate() - 30)) And (select CONVERT(date, getdate()))
Begin
    INSERT INTO dbo.BlockedUsers(DateEnforced,Blocked)
    VALUES (GETDATE(),1)
    PRINT 'New Block Date Added'

    UPDATE Account 
    Set BlockID = (Select Max(BlockID) From BlockedUsers)
    where [AccountNo] = (Select [AccountNo] from inserted)  
    PRINT 'Account Blocked'
 End

 END

GO

The IF statement in Transact-SQL expects a single statement after the condition: Transact-SQL中的IF语句需要在条件之后的单个语句:

IF condition
  statement;

If you want to perform more than one statement in the same branch, you must enclose them in the BEGIN / END "brackets": 如果要在同一分支中执行多个语句,则必须将它们括在BEGIN / END “括号”中:

IF condition
BEGIN
  statement;
  statement;
  ...
END;

In your trigger, only the INSERT statement executes depending on the result of the (Select [DueDate] from inserted) = '2011-01-01' condition. 在触发器中,只有INSERT语句执行,具体取决于(Select [DueDate] from inserted) = '2011-01-01'条件的结果。 As for both PRINTs and the UPDATE, they execute unconditionally, ie after every insert into Account . 对于PRINT和UPDATE,它们无条件地执行,即在每次插入到Account So, you probably need to add BEGIN and END around INSERT, UPDATE and both PRINTs: 因此,您可能需要在INSERT,UPDATE和两个PRINT周围添加BEGINEND

...
If(Select [DueDate] from inserted) = '2011-01-01'
BEGIN
INSERT INTO dbo.BlockedUsers(DateEnforced,Blocked)
VALUES (GETDATE(),1);
PRINT 'New Block Date Added';

UPDATE Account 
Set BlockID = IDENT_CURRENT('BlockID')
where @ID = @ID;
PRINT 'Account Blocked';
END;
...

You did 你做到了

FOR INSERT

You want to use 你想用

AFTER INSERT, UPDATE

FOR INSERT tells Sql Server that your trigger will entirely replace the normal insert operation. FOR INSERT告诉Sql Server你的触发器将完全取代正常的插入操作。 AFTER INSERT tells Sql Server to go ahead and insert the row, and then execute this code as a post-processing step. AFTER INSERT告诉Sql Server继续插入行,然后执行此代码作为后处理步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM