如何创建包含来自另一个表的逻辑的SQL触发器

时间:2013-08-02 16:05:39

标签: sql sql-server triggers

我有一个消息传递应用程序,我正在尝试我的第一个触发器。我想做的是对数据库视图进行插入检查,以查看用户是否已超出其配额。如果他们已在消息传递表中插入消息以通知用户。

USE [PhoneMessaging]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trgAfterInsert] ON [dbo].[Messages]
FOR INSERT
AS

declare @uid smallint;
    declare @Message nvarchar(1000);
    declare @MessageFrom smallint;
    declare @vdate date;
    declare @priority smallint;
    declare @MessageLimit smallint;

    select @uid=i.MessageTo from inserted i;    

    set @Message='You have reached your storage limit.';
    set @vdate = CONVERT(VARCHAR,GETDATE(),111);
    set @MessageFrom = 0
    set @priority = 3

    set @MessageLimit = (SELECT dbo.vUserMessageTotals.MessageLimit from dbo.vUserMessageTotals WHERE uid = @uid)

    /*IF (SELECT dbo.vUserMessageTotals.MessageReceivedTotal from dbo.vUserMessageTotals) >= @MessageLimit*/

    insert into Messages
           (MessageTo,[Message],vDate,MessageFrom) 
    values(@uid,@Message,@vdate,@MessageFrom)

    PRINT 'AFTER INSERT trigger fired.'

0 个答案:

没有答案