在 SQL Server 的创建触发器中调用存储过程

2022-01-01 00:00:00 sql-server triggers

我有一个名为 insert2Newsletter 的存储过程,带有参数

I have a stored procedure named insert2Newsletter with parameters

(@sex nvarchar(10),
@f_name nvarchar(50),
@l_name nvarchar(70),
@email nvarchar(75),
@ip_address nvarchar(50),
@hotelID int,
@maArt nchar(2))

我想在插入触发器中调用这个存储过程.如何从插入的字段中检索相应的字段以及如何在触发器中调用 insert2Newsletter?

I want call this stored procedure in an insert trigger. How do I retrieve the corresponding fields from inserted and how do i call insert2Newsletter within the trigger?

我试过没有成功:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER RA2Newsletter 
   ON  [dbo].[Reiseagent] 
   AFTER INSERT
AS 
DECLARE @rAgent_Name nvarchar(50),
DECLARE @rAgent_Email nvarchar(50),
DECLARE @rAgent_IP nvarchar(50),
DECLARE @hotelID int

BEGIN
    SET NOCOUNT ON;

    -- Insert statements for trigger here
    Select @rAgent_Name=rAgent_Name, @rAgent_Email=rAgent_Email, @rAgent_IP=rAgent_IP, @hotelID=hotelID  From Inserted
    EXEC insert2Newsletter '','',@rAgent_Name,@rAgent_Email,rAgent_IP,@hotelID,'RA' 


END
GO

非常感谢您的反馈...问候...

thx a lot for your feedback... greetings...

推荐答案

finally...

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON

GO
ALTER TRIGGER [dbo].[RA2Newsletter] 
   ON  [dbo].[Reiseagent] 
   AFTER INSERT
 AS
    declare
    @rAgent_Name nvarchar(50),
    @rAgent_Email nvarchar(50),
    @rAgent_IP nvarchar(50),
    @hotelID int,
    @retval int


BEGIN
    SET NOCOUNT ON;

    -- Insert statements for trigger here
    Select @rAgent_Name=rAgent_Name,@rAgent_Email=rAgent_Email,@rAgent_IP=rAgent_IP,@hotelID=hotelID  From Inserted
    EXEC insert2Newsletter '','',@rAgent_Name,@rAgent_Email,@rAgent_IP,@hotelID,'RA', @retval

END

相关文章