【问题标题】:SQL Server 2008 Create Trigger after insert... return id... called stored procedureSQL Server 2008在插入后创建触发器...返回ID ...称为存储过程
【发布时间】:2014-06-20 06:06:58
【问题描述】:

我只想要一个简单的 sql 服务器触发器(插入后)从插入的表中取回 ID...所以算法:

  1. 新记录被插入到表中

  2. 触发器被调用

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

我的失败尝试如下:

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

【问题讨论】:

  • SELECT @marketID = 插入的 ID。

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


【解决方案1】:

在触发器中,您可以访问名称为“INSERTED”的表,该表将包含新插入的记录详细信息。下面的小例子

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  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多