我有一个这样的表:
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”来发言。