我需要在SQL Server中创建一个触发器,以便在事件完成时填充表。
我的代码:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Tr_EsportaMancatiContatti]
ON [dbo].[Lav_CollaudiVodaf_StoricoMancatiContatti]
AFTER INSERT
AS
BEGIN
DECLARE @IDPRATStorico INT;
DECLARE @IDPRAT INT;
DECLARE @CodRichiestaCRM INT;
DECLARE @IDESITO INT;
DECLARE @Telefono VARCHAR;
DECLARE @DataOraContatto INT;
SET NOCOUNT ON;
SET @IDPRATStorico = (SELECT IDPRATStorico FROM inserted);
SET @IDPRAT = (SELECT IDPRAT FROM inserted);
SET @CodRichiestaCRM = (SELECT CodRichiestaCRM FROM inserted);
SET @IDESITO = (SELECT IDESITO FROM inserted);
SET @Telefono = (SELECT Telefono FROM inserted);
SET @DataOraContatto = (SELECT DataOraContatto FROM inserted);
IF @IDESITO = 18 AND count(@IDPRAT) < 3
BEGIN
INSERT Lav_CollaudiVodaf_StoricoMancatiContatti
SET IDPRATStorico=@IDPRATStorico
SET CodRichiestaCRM=@CodRichiestaCRM
SET IDESITO=@IDESITO
SET Telefono=Telefono
SET DataOraContatto=@DataOraContatto
WHERE IdPrat=@IDPRAT;
END
END;
它会引发一些错误。
目标:我有一个填充了联系人的表,我需要将这些记录插入到另一个表中,如果IDESITO是18并且新表中的计数是&lt; 3。
任何建议都表示赞赏。
答案 0 :(得分:0)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Tr_EsportaMancatiContatti]
ON [dbo].[Lav_CollaudiVodaf_StoricoMancatiContatti]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Lav_CollaudiVodaf_StoricoMancatiContatti(
IDPRATStorico,
CodRichiestaCRM,
IDESITO,
Telefono,
DataOraContatto
)
SELECT TOP 3
IDPRATStorico,
CodRichiestaCRM,
IDESITO,
Telefono,
DataOraContatto
FROM INSERTED
WHERE IDESITO = 18
END;
我不确定你想用count(@IDPRAT)&lt;我想你可能只想要前3行