【发布时间】:2018-10-02 04:59:59
【问题描述】:
我的 SQL Server 数据库中有这些表:
CREATE TABLE weather
(
weatherId integer Identity(1,1) primary key,
weatherDate datetime,
rainout BIT,
temperature float,
inchesOfRain float
)
CREATE TABLE weather_audit
(
weatherAuditId integer Identity(1,1) primary key,
weatherId integer,
date datetime,
rainout BIT,
temperature float,
inchesOfRain float
)
CREATE TABLE maintenance
(
maintenanceId integer Identity(1,1) primary key,
maintenanceDescription nvarchar(100),
dateRequested datetime,
dateResolved datetime,
currentStatus nvarchar(20),
estimatedCost decimal,
)
CREATE TABLE maintenence_audit
(
mainteneceAuditId integer Identity(1,1) primary key,
maintenanceId integer,
description nvarchar(100),
dateRequested datetime,
dateResolved datetime,
currentStatus nvarchar(20),
estimatedCost decimal,
updatedOn datetime
)
我想设置一个触发器,当将一行插入到 inchesOfRain 大于 4 的天气表中时触发。这就是我现在所拥有的,但它不起作用:
CREATE TRIGGER tr_weather_ForInsertUpdate
ON weather
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
IF (SELECT TOP 1 inchesOfRain FROM weather) > 4
INSERT INTO weather_audit (weatherId, weatherDate, rainout, temperature, inchesOfRain)
SELECT
i.weatherId, i.weatherDate, i.rainout, i.temperature, i.inchesOfRain
FROM
Inserted AS I
END
所以如果我要插入这个
INSERT INTO dbo.weather (weatherDate, rainout, temperature, inchesOfRain)
VALUES ('4/21/2018', '0', '70', '6');
它将在weather 表和weather_audit 表中添加一行,因为雨量为 6 英寸
【问题讨论】:
标签: sql sql-server database triggers