简体   繁体   English

触发器:将新插入的列中的 null 值替换为同一行中的另一列值

[英]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.

相关问题 用另一个表中同一列的值替换列中的字符串值 - Replace a string value in a column by values from same column in another table 如何通过从另一张表中具有相同id的行中的列中获取值来将值插入到连续行中的表的列中 - how to insert values into a column of a table in successive rows by fetching values from a column in a row with a same id in another table 根据另一行中另一列的值将值填充为空列 - Populating values to null column based on value of some another column from another row Sql Server触发器将值从新行插入另一个表 - Sql Server trigger insert values from new row into another table 根据另一列的值将值插入新表的列中 - Insert value into column in new table based on value from another column Oracle 在插入另一张表期间触发一张表中列/行的更改值 - Oracle trigger change value of column/row from one table during insert of another table 如果相同的值,则从另一个表更新 Postgres 表的列值,否则为不匹配的值插入新行 - Update a Postgres table columns values from another table if same values otherwise insert a new row for unmatched value 如何使用触发器 function 将行的值从一列移动到另一列? - How to move values of a row, from a column to another with a trigger function? 根据另一列的值将一行中的值更新到新行 - Update values from one row to a new row based on another column's value 更新SQL:从一列获取值,在另一列替换为Null - Update SQL: Take Value from One Column and Replace Null in Another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM