【问题标题】:Find all records that have the same value in a field within a Trigger查找触发器内字段中具有相同值的所有记录
【发布时间】:2011-07-03 12:59:07
【问题描述】:

我正在尝试从表中编写一个插入后触发器

   CREATE TABLE [dbo].[PartsWork](
     [parts_work_id] [int] IDENTITY(1,1) NOT NULL,
     [experiment_id] [int] NOT NULL,
     [partition_id] [int] NULL,
     [sim_time] [time](7) NULL,
     [real_time] [datetime] NULL,
     [construction] [bit] NULL,
     [destruction] [bit] NULL,
     [part_id] [int] NOT NULL,
    )

我需要找出与插入集具有相同 assembly_id 的行数是多少。

assembly_id 是下表中的一个字段:

CREATE TABLE [dbo].[Parts](
[part_id] [int] IDENTITY(1,1) NOT NULL,
[part_name] [nvarchar](50) NULL,
[part_type] [nvarchar](50) NULL,
[assembly_id] [int] NOT NULL,
 )

我需要将该值作为 int 存储在变量中。

谢谢,

【问题讨论】:

    标签: sql sql-server tsql


    【解决方案1】:

    您需要将特殊的INSERTED table(仅在触发器中可用)加入 [dbo].[Parts] 以获得 COUNT。

    一些想法:

    • 您必须考虑触发器中的多行插入/更新。例如,您可以有 2 个 assembly_ids
    • 当分配给一个变量时,你想用它做什么?该值在触发器外部不可见
    • 使用存储过程进行处理可能会更好,并使用 OUTPUT clause 从 INSERT 中取回数据

    【讨论】:

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