SQL将多个记录插入到Triggered表中

时间:2015-12-04 10:52:51

标签: sql-server tsql triggers

在SQL代理作业上我从主表(X)下载数据。如果状态等于WW'或者' WXY'我只想将所有记录插入另一个表(Y),在那里我创建了一个触发器。你能告诉我在触发器定义中应该改变什么吗?当我尝试将多个记录插入DB2.Y表(代理程序作业失败)时收到错误消息

第一个代码(下载新数据)

DECLARE @STARTTIME datetime
DECLARE @TIME datetime
DECLARE @ENDTIME datetime

SET @TIME=(SELECT MAX(Time) FROM DB2.Y)
SET @STARTTIME=(select dateadd(hour,1,getdate()))
SET @ENDTIME=(SELECT MAX(TIME) FROM DB1.X where TIME is not null)


IF @TIME = @ENDTIME
BEGIN
TRUNCATE TABLE  DB2.Y;
INSERT  INTO DB2.Y (Time) Values (@TIME)
END
ELSE
BEGIN
TRUNCATE TABLE  DB2.Y

INSERT  INTO DB2.Y ([Serv],[Status]) 
SELECT [SERV],[STATUS] FROM DB1.X WHERE TIME > @TIME and [SERV]='Description' and ([STATUS]='WW' or [STATUS]='WXY') ;


UPDATE DB2.Y
SET [Time]= @ENDTIME
END

触发代码:

USE DB2
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE TRIGGER [dbo].[TriggerName] ON Y
AFTER INSERT AS

DECLARE  @SERV varchar(40)
DECLARE @STATUS varchar(3)

SET @SERV=(SELECT [SERV] FROM inserted)
SET @STATUS=(SELECT [STATUS] FROM inserted)

IF @STATUS in ('WW', 'WXY')
 BEGIN
 DECLARE @MSG varchar(500)
 SET @MSG = 'Job "' + @SERV + '" failed!!!'

 EXEC msdb.dbo.sp_send_dbmail @recipients=N'myemail@domain.com', @body= @MSG,  @subject = @MSG, @profile_name = 'profilename'

END

3 个答案:

答案 0 :(得分:0)

您可以使用光标来解决错误。下面的光标将使用STATUS WWWXY的所有插入记录进行迭代,并为每个记录发送一封电子邮件。

DECLARE error_cursor CURSOR FOR 
SELECT [SERV],[STATUS] FROM inserted
WHERE [STATUS] in ('WW', 'WXY')

OPEN error_cursor

FETCH NEXT FROM error_cursor 
INTO @SERV, @STATUS

WHILE @@FETCH_STATUS = 0
BEGIN

   DECLARE MSG varchar(500)
  SET @MSG = 'Job "' + @SERV + '" failed!!!'

   EXEC msdb.dbo.sp_send_dbmail @recipients=N'myemail@domain.com', @body= @MSG,  @subject = @MSG, @profile_name = 'profilename'

   FETCH NEXT FROM vendor_cursor 
   INTO @SERV, @STATUS
END 
CLOSE error_cursor;
DEALLOCATE error_cursor;

答案 1 :(得分:0)

INSERTED可以包含多行INSERT的多行 - 每次操作运行一次,而不是每行一次。

尝试使用这样的触发器:

USE DB2
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE TRIGGER [dbo].[TriggerName] ON Y
AFTER INSERT AS

DECLARE  @SERV varchar(40)
DECLARE @STATUS varchar(3)

IF EXISTS(SELECT [Status] FROM Inserted WHERE [STATUS] in ('WW', 'WXY'))
 BEGIN
 DECLARE @MSG varchar(8000)

 INSERT INTO JobLog(Serv, Status) 
 SELECT Serv, Status FROM Inserted WHERE [STATUS] in ('WW', 'WXY')

 SET @MSG = CAST(@@ROWCOUNT as nvarchar) + 'Job(s)  failed - see log for details'

 EXEC msdb.dbo.sp_send_dbmail @recipients=N'myemail@domain.com', @body= @MSG,  @subject = @MSG, @profile_name = 'profilename'

END

SELECT

答案 2 :(得分:0)

尝试这样的事情......

CREATE TRIGGER [dbo].[TriggerName] ON Y
AFTER INSERT 
AS
BEGIN
  SET NOCOUNT ON;

DECLARE @SERV varchar(40),@STATUS varchar(3), @MSG varchar(500)

Select [SERV], [STATUS] INTO #Temp
FROM inserted
Where [STATUS] IN ('WW', 'WXY')

Declare Cur CURSOR FOR 
SELECT [SERV], [STATUS] FROM #Temp

    OPEN Cur   
    FETCH NEXT FROM Cur INTO @SERV,  @STATUS  

    WHILE @@FETCH_STATUS = 0   
    BEGIN   
         SET @MSG = 'Job "' + @SERV + '" failed!!!'

         EXEC msdb.dbo.sp_send_dbmail @recipients=N'myemail@domain.com'
                                    , @body= @MSG
                                    , @subject = @MSG
                                    , @profile_name = 'profilename'


           FETCH NEXT FROM Cur INTO @SERV,  @STATUS    
    END   

    CLOSE Cur   
    DEALLOCATE Cur

END