2015-02-10 82 views
0

我創建了一個觸發器,用於在服務後設備狀態爲「需要購買」時發送郵件。在電子郵件中,需要保修和購買日期以及設備型號。我在觸發器中有3個選擇語句。檢索模型,但日期不起作用,數據不會保存。下面是觸發器。一切正常,如果我在郵件中除了評論日期檢索觸發器內的數據

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 [email protected]) 
    set @model = (select Model from Equipment where [email protected]) 
    set @purchase = (select P_date from Equipment where [email protected]) 

    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='[email protected]', 
        @body = @message, 
        @subject = 'Purchase Needed for Equipment', 
        @body_format ='HTML' 

    end 
+0

只是檢查,但它是正確的W_Date被保存到日期時間字段,而P_date進入varchar(20)? – 2015-02-10 14:28:26

+0

對於'delete'觸發器,'inserted'表應該是空的,所以這些分配大部分都是'null'。但是請注意,我也說'插入'*表*。即使您切換到使用「已刪除」,您也需要認識到它可以包含0,1或*多行*。所有這些分配都被打破了,因爲他們似乎認爲觸發表只包含一行 - 這是一個不合理的假設。 – 2015-02-10 14:35:13

+0

哪個dbms? (看起來不像ANSI SQL ...) – jarlh 2015-02-10 14:54:44

回答

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='[email protected]', 
        @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)?如果沒有,你需要把它作爲這個

+0

我的購買和保修有datetime類型,但沒有工作,所以我試着varchar。插入後觸發器應該說 – user3590040 2015-02-10 15:50:18

+0

@ user3590040我已經修改了上述內容,現在應該可以使用 – 2015-02-10 16:00:58

+0

電子郵件已發送,但正文爲空 – user3590040 2015-02-13 14:16:19