English 中文(简体)
在发射触发器后,我希望我的审计表显示插入的行数。
原标题:I want my audit table to show the count of inserted rows, after the trigger is fired

我有一个这样的表:

Carmark carYear carprice Carcondition
Mazda 2010 58695 Fair
Dodge 2004 65987 Excellent
Honda 2020 47896 Excellent

我创建了一个触发点,即trg_InsertUpdateDelete_CarSale ,用于记录安东·德莱特的合并记录。

Create TRIGGER trg_InsertUpdateDelete_CarSale
ON dbo.CarSale 
FOR INSERT, UPDATE, DELETE
AS
BEGIN 
--> Declaring local variables
        Declare 
           @TableName Varchar(20),
           @CarCondition Varchar (100),
           @InsertedRow Int,
           @DeletedRow Int
       

--> Assigning values for the local variables 
           Set @TableName =  dbo.CarSale 

           -- Set the CarCondition variable to  Excellent  and fetch records
          SET @CarCondition =  excellent 
          
            Set @InsertedRow = (Select Count(CarSale_ID) from inserted)
            Set @DeletedRow = (Select Count(CarSale_ID) from deleted)

 --> Creating Logic for control flow 

   If exists (Select * From deleted) 
         Begin 
         If exists (Select * From inserted)  
                       
            Begin 
              Insert into dbo.CarTables_Audit (TableName, CarCondition, TotalRowCount, OperationStatus)
              Select @TableName, @carCondition, @InsertedRow,  Updated 
            End 
                 Else 
            Begin
              Insert into dbo.CarTables_Audit (TableName, CarCondition, TotalRowCount,OperationStatus)
            Select @TableName, @CarCondition, @DeletedRow,  Deleted 
          End 

       End; 

   Else 
      Begin 
          Insert into dbo.CarTables_Audit (TableName, CarCondition, TotalRowCount,OperationStatus)
            Select @TableName, @CarCondition, @InsertedRow,  Inserted 
    End 

End

我想通过宣布“@tableName, @Car conditions, @Carprice”来发言。

问题回答

下面是原始触发器:

CREATE TRIGGER trg_InsertUpdateDelete_CarSale
ON dbo.CarSale 
FOR INSERT, UPDATE, DELETE
AS
BEGIN 

    DECLARE @InsertedRow int = (Select Count(CarSale_ID) from inserted)
    DECLARE @DeletedRow int = (Select Count(CarSale_ID) from deleted)
    DECLARE @RowCount int = @DeletedRow
    DECLARE @OpStatus varchar(20)

    If @DeletedRow > 0 AND @InsertedRow > 0
        Set @OpStatus =  Updated 
    Else If @DeletedRow > 0
        Set @OpStatus =  Deleted 
    Else Begin
        Set @OpStatus =  Inserted 
        Set @RowCount = @InsertedRow
    End
      
    DECLARE @CarCondition varchar(100) =  excellent 
          
    INSERT INTO dbo.CarTables_Audit
       (TableName, CarCondition, TotalRowCount, OperationStatus)
    VALUES
      ( dbo.CarTable , @CarCondition, @RowCount, @OpStatus)
END

但是,这在如何处理<条码> 有条件<>/代码”方面似乎明显是错误的,问题本身也不明确。

It seems odd to have an audit table with a field like TotalRowCount for handling multiple rows, but only a single condition. Since SQL Server can batch up triggers from multiple sessions for performance, you should also expect to have times when the trigger runs several rows and different conditions, and therefore the audit table would lie. But nothing in the question right now tells us how you want to handle that situation.

我可能这样做:

CREATE TRIGGER trg_InsertUpdateDelete_CarSale
ON dbo.CarSale 
FOR INSERT, UPDATE, DELETE
AS
BEGIN 
    INSERT INTO dbo.CarTables_Audit
       (TableName, CarCondition, TotalRowCount, OperationStatus)      
    SELECT  dbo.CarTable , coalesce(i.Condition, d.Condition), count(CarSale_ID)
        , CASE WHEN count(d.CarSale_ID) = 0 THEN  Inserted 
               WHEN count(i.CarSale_ID) = 0 THEN  Deleted 
               ELSE  Updated  END
    FROM inserted i
    FULL JOIN deleted d on i.CarSale_ID = d.CarSale_ID
    GROUP BY coalesce(i.Condition, D.Condition)
END

而且,这似乎是错误的,因为你再说一遍:你可以坐在一起的多场会议,意思是,你可能有一些合法删除,而其他合法插入的条件相同,这将算作更新。

还有一次尝试:

CREATE TRIGGER trg_InsertUpdateDelete_CarSale
ON dbo.CarSale 
FOR INSERT, UPDATE, DELETE
AS
BEGIN 
    WITH PreGrouped as (
        -- First group individual sales and set condition and status for each   
        SELECT Coalesce(i.CarSale_ID, d.CarSale_ID) CarSale_ID, 
            coalesce(i.Condition, d.Condition) Condition,
            case when i.CarSale_ID IS NULL THEN  Deleted 
                 when d.CarSale_ID IS NULL THEN  Inserted 
                 else  Updated  END as OperationStatus           
        FROM inserted i
        FULL JOIN deleted d on i.CarSale_ID = d.CarSale_ID
        GROUP BY coalesce(i.CarSale_ID, d.CarSale_ID)
    )
    INSERT INTO dbo.CarTables_Audit
       (TableName, CarCondition, TotalRowCount, OperationStatus) 
    -- Then group by condition and status
    SELECT  dbo.CarSale , Condition, COUNT(CarSale_ID), OperationStatus
    FROM PreGrouped 
    GROUP BY Condition, OperationStatus
END

This looks like something I d be comfortable putting in an audit table. Now the only issue that remains if someone creates a sale and very quickly deletes it, so they are batched together for the trigger, it would count as an update. But I would find that set of events very strange and hard to produce.

值得注意的是,这有可能每当触发点火时在审计表中插入多个段落,但只有在确实存在多个批量的情况下。 我也假设,现在的定有违约值的定时栏,但我们只是重新插入该栏,让违约值做事。 确实很难在没有时间调查的情况下进行审计。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签