简体   繁体   中英

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.

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.

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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM