SQL Server 2008在插入后创建触发器...返回id ...调用存储过程

时间:2014-06-20 06:06:58

标签: sql sql-server sql-server-2008 stored-procedures

我只想要一个SIMPLE sql server触发器(插入后)从插入的表中获取ID ...所以算法:

  1. 将新记录插入表格

  2. 触发器被称为

  3. 触发器从插入的记录中获取id并调用存储过程

  4. 我失败的尝试如下:

    CREATE TRIGGER [dbo].[insertNewMarket] 
       ON  [dbo].[markets]
       AFTER insert
    AS 
    BEGIN
        declare @marketID int
        SET NOCOUNT ON;
        --SELECT ID from inserted
        --set @marketID = Inserted.ID--get the inserted id and pass to stored proc
        exec marketInsert @marketID 
    END
    GO
    

1 个答案:

答案 0 :(得分:3)

CREATE TRIGGER [dbo].[insertNewMarket]   
   ON  [dbo].[markets]  
   AFTER insert  
AS   
BEGIN  
    declare @marketID int  
    SET NOCOUNT ON;  
    SELECT @marketID = ID from inserted  
    exec marketInsert @marketID   
END  
GO