[英]Trigger: Replace null values in column from new insert with another column value from SAME row
Say I have a table with these columns:假设我有一个包含这些列的表:
PrimaryKey | Celsius (float) | Fahrenheit (float) |
The two units values can be converted to each other, but at any given time only one unit is used for the data acquisition, meaning that the other unit will record as null.这两个单位值可以相互转换,但在任何给定时间,只有一个单位用于数据采集,这意味着另一个单位将记录为 null。
I need to convert the null value recorded in the new insert to one that is a conversion of the recorded value, to store both values with the entry.我需要将新插入中记录的 null 值转换为记录值的转换,以便将这两个值与条目一起存储。
I got something like this so far:到目前为止,我得到了这样的东西:
CREATE TRIGGER Temp_switch
ON DATASET
FOR INSERT
AS
DECLARE @Temp_C float,
@Temp_F float
SET @Temp_C = (SELECT Temperature_C FROM inserted)
SET @Temp_F = (SELECT Temperature_F FROM inserted)
BEGIN
SET NOCOUNT ON
UPDATE DATASET
SET DATASET.Temperature_F = (@Temp_C * 1.8) + 32
FROM DATASET
INNER JOIN inserted ON DATASET.DatasetId = inserted.DatasetId
WHERE @Temp_F IS NULL
UPDATE DATASET
SET DATASET.Temperature_C = (@Temp_F - 32)/1.8
FROM DATASET
INNER JOIN inserted ON DATASET.DatasetId = inserted.DatasetId
WHERE @Temp_C IS NULL
END
Then I tried something using Coalesce:然后我尝试了使用 Coalesce 的东西:
CREATE TRIGGER Temp_switch ON DATASET
FOR INSERT
AS
BEGIN
UPDATE DATASET
SET Temperature_F = COALESCE(i.Temperature_F, (i.Temperature_C * 1.8) + 32),
Temperature_C = COALESCE(i.Temperature_C, (i.Temperature_C - 32) / 1.8)
FROM DATASET d INNER JOIN
inserted i
ON d.DatasetId = i.DatasetId
WHERE i.Temperature_F IS NULL OR i.Temperature_C IS NULL;
END;
Both of these do fill the Temperature_F column when the unit is Celsius, but ALL the Temperature_F values are the same, as if they are all getting the same Celsius reference value from the inserted row - which should be changing for every entry, as the stored procedure that enters data to the DATASET table only adds one row at a time.当单位为摄氏度时,这两个都填充了 Temperature_F 列,但是所有的 Temperature_F 值都是相同的,就好像它们都从插入的行中获取相同的摄氏度参考值一样——每个条目都应该改变,因为存储的向 DATASET 表输入数据的过程一次只添加一行。 I am left with something that looks like this (made up example):
我留下了一些看起来像这样的东西(虚构的例子):
SessionId | Celsius (float) | Fahrenheit (float) |
3 | 22.0 | 84.00 |
3 | 24.5 | 84.00 |
3 | 23.0 | 84.00 |
3 | 25.5 | 84.00 |
3 | 23.0 | 84.00 |
So I need something that can reference the Celsius or Fahrenheit value from the same inserted row, and calculate the other column based on this value.所以我需要一些可以从同一插入行引用摄氏或华氏值的东西,并根据该值计算另一列。 Any ideas?
有任何想法吗?
There is a typo in the trigger:触发器中有一个错字:
CREATE TRIGGER Temp_switch ON DATASET
FOR INSERT
AS
BEGIN
UPDATE DATASET
SET Temperature_F = COALESCE(i.Temperature_F, (i.Temperature_C * 1.8) + 32),
Temperature_C = COALESCE(i.Temperature_C, (i.Temperature_F - 32) / 1.8)
---------------------------------------------------------^
FROM DATASET d INNER JOIN
inserted i
ON d.DatasetId = i.DatasetId
WHERE i.Temperature_F IS NULL OR i.Temperature_C IS NULL;
END;
I think the problem is that the previous trigger was not deleted.我认为问题在于之前的触发器没有被删除。 But here is a db<>fiddle showing that this works.
但这里有一个db<>fiddle表明它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.