我創建了一個觸發器,用於在服務後設備狀態爲「需要購買」時發送郵件。在電子郵件中,需要保修和購買日期以及設備型號。我在觸發器中有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
只是檢查,但它是正確的W_Date被保存到日期時間字段,而P_date進入varchar(20)? – 2015-02-10 14:28:26
對於'delete'觸發器,'inserted'表應該是空的,所以這些分配大部分都是'null'。但是請注意,我也說'插入'*表*。即使您切換到使用「已刪除」,您也需要認識到它可以包含0,1或*多行*。所有這些分配都被打破了,因爲他們似乎認爲觸發表只包含一行 - 這是一個不合理的假設。 – 2015-02-10 14:35:13
哪個dbms? (看起來不像ANSI SQL ...) – jarlh 2015-02-10 14:54:44