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.
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 ? Can anyone explain why ?
Note: Both the insert and the Update statement are fine when executed by themselves.
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
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
Yes ,you can insert,update,delete using trigger. view is nothing but a table. Like creating another table to the main table.
The CREATE TRIGGER statement allows you to create a new trigger that is fired automatically whenever an event such as INSERT , DELETE , or UPDATE occurs against a table. In this syntax: The schema_name is the name of the schema to which the new trigger belongs. The schema name is optional.
2, you can only create one trigger for an event in a table e.g., you can only create one trigger for the BEFORE UPDATE or AFTER UPDATE event. MySQL 5.7. 2+ lifted this limitation and allowed you to create multiple triggers for a given table that have the same event and action time.
You can create multiple triggers for the same subject table, event, and activation time. The order in which those triggers are activated is the order in which the triggers were created. Db2 records the timestamp when each CREATE TRIGGER statement executes.
The IF
statement in Transact-SQL expects a single statement after the condition:
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":
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. As for both PRINTs and the UPDATE, they execute unconditionally, i.e. after every insert into Account
. So, you probably need to add BEGIN
and END
around INSERT, UPDATE and both PRINTs:
...
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;
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With