在触发器中检索数据

时间:2015-02-10 14:24:48

标签: sql sql-server tsql triggers

我创建了一个触发器,只要维修后设备的状态为“需要购买”,就会发送邮件。在电子邮件中,需要保修和购买日期以及设备型号。我在触发器中有3个select语句。检索模型但日期不起作用且数据未保存。这是下面的触发器。如果我在消息中区分日期,那么一切都有效。

ALTER TRIGGER [dbo].[sendpurchaserequest]
   ON  [dbo].[services]
   AFTER INSERT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    declare @type as varchar(50)
    declare @serial as varchar(50)
    declare @department as varchar(50)
    declare @action as varchar(150)
    declare @status as varchar(50)
    declare @message as Varchar(1000)
    declare @warrant as datetime
    declare @model as varchar(150)
    declare @purchase as varchar(20)

    SET NOCOUNT ON;

    -- Insert statements for trigger here
    set @type = (select Equipmenttype from inserted)
    set @action = (select supportaction from inserted)
    set @department = (select userdepartment from inserted)
    set @serial = (select serialnum from inserted)
    set @status = (select [status] from inserted)
    set @warrant = (select W_date from Equipment where Serial_no=@serial)
    set @model = (select Model from Equipment where Serial_no=@serial)
    set @purchase = (select P_date from Equipment where Serial_no=@serial)

    if @status = 'Purchase Needed     '
    begin
    set @message = 'Diagnostic tests on the following equipment have revealed that it is in need of replacement parts to be procured.

See details below:</br></br>' + 'Equipment Type:' + @type + '</br> Model: ' + @model +'</br> Serial Number: ' + @serial + '</br></br> User Department: ' + @department + '</br> Corrective Action: ' + @action + '</br> Purchase Date: ' + @purchase + '</br> Warrant Date: ' + @warrant

    print @message

                    EXECUTE msdb.dbo.sysmail_start_sp
                    EXEC msdb.dbo.sp_send_dbmail
                    @recipients='someone@site.com',
                    @body = @message,
                    @subject = 'Purchase Needed for Equipment',
                    @body_format ='HTML'

    end

1 个答案:

答案 0 :(得分:0)

你可以尝试一下这个应该为每个被删除的项目执行它 - 如果你想插入然后显然将它改为插入的表而且你的而不是应该插入:

ALTER TRIGGER [dbo].[sendpurchaserequest]
   ON  [dbo].[services]
   after insert
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    declare @type as varchar(50)
    declare @serial as varchar(50)
    declare @department as varchar(50)
    declare @action as varchar(150)
    declare @status as varchar(50)
    declare @message as Varchar(1000)
    declare @warrant as datetime
    declare @model as varchar(150)
    declare @purchase as datetime

    SET NOCOUNT ON;

    -- Insert statements for trigger here

    Declare email cursor fast_forward for
    Select i.Equipmenttype, i.supportaction, i.userdepartment, i.serialnum, i.[status], e.W_date, e.Model, e.P_date
    from inserted i
    inner join 
    Equipment e on e.Serial_no = i.serial

    Open email

    Fetch next from email into @type, @action, @department, @serial, @status, @warrant, @model, @purchase

    While @@FETCH_STATUS = 0
    begin

    if @status = 'Purchase Needed'
    begin
    set @message = 'Diagnostic tests on the following equipment have revealed that it is in need of replacement parts to be procured.

See details below:</br></br>' + 'Equipment Type:' + ISNULL(@type, '') + '</br> Model: ' + ISNULL(@model, '') +'</br> Serial Number: ' + ISNULL(@serial, '') + '</br></br> User Department: ' + ISNULL(@department, '') + '</br> Corrective Action: ' + ISNULL(@action, '') + '</br> Purchase Date: ' + ISNULL(Cast(@purchase as varchar),'') + '</br> Warrant Date: ' + ISNULL(Cast(@warrant as varchar), '')

    print @message

                    EXECUTE msdb.dbo.sysmail_start_sp
                    EXEC msdb.dbo.sp_send_dbmail
                    @recipients='someone@site.com',
                    @body = @message,
                    @subject = 'Purchase Needed for Equipment',
                    @body_format ='HTML'
    end
    Fetch next from email into @type, @action, @department, @serial, @status, @warrant, @model, @purchase
    end
End

顺便提一下,我也注意到你的@purchase是varchar(20)你确定你的P_date是varchar(20)吗?如果不是,你需要把它作为这个