简体   繁体   English

条件更新语句

[英]Conditional Update Statement

I have a process that is dumping a CSV file into a temp table then I am formatting the data to put into an actual table.我有一个进程将 CSV 文件转储到临时表中,然后我正在格式化数据以放入实际表中。

I have run into an issue where sometimes one of the values is coming up in scientific notation and my table is set to DECIMAL(13,9) .我遇到了一个问题,有时其中一个值以科学记数法出现,而我的表设置为DECIMAL(13,9) Essentially what I am wanting to do is update any of the values that are in scientific notation to 0 because they are so low they might as well be 0 in this use case.基本上我想要做的是将任何以科学记数法表示的值更新为 0,因为它们太低了,在这个用例中也可能是 0。

There are 9 columns that could potentially have this scientific notation and I know that I could just have 9 update statements that update each column one at a time.有 9 列可能具有这种科学记数法,我知道我只能有 9 个更新语句,一次更新每一列。 I can do this if I have to, just thought someone might have a better idea than that.如果必须的话,我可以这样做,只是认为有人可能有比这更好的主意。

So, my question is: is there a way to update all of these 9 columns with one statement?所以,我的问题是:有没有办法用一个语句更新所有这 9 列?

Here is the dataset:这是数据集:

SELECT Value, MyPropOccupancy, CompSetOccupancy, IndexOccupancy,
       MyPropADR, CompSetADR, IndexADR, MyPropRevPar, CompSetRevPar, IndexRevPar
    FROM #fileColumnsChanges

在此处输入图片说明

So in this circumstance, I would want to update CompSetOccupancy (Year To Date) and CompSetADR (Running 12 Month) to 0.因此,在这种情况下,我想将 CompSetOccupancy(年初至今)和 CompSetADR(运行 12 个月)更新为 0。

You can write just one UPDATE here with 9 'setters'.您可以在此处仅使用 9 个“二传手”编写一个UPDATE If your numeric values in that temp table are based on (n)varchar you need to take this into account before resetting the actual value.如果该临时表中的数值基于(n)varchar ,则需要在重置实际值之前考虑到这一点。 Below I used 0.0001 as an example threshold:下面我使用0.0001作为示例阈值:

UPDATE
  #fileColumnsChanges
SET
  CompSetOccupancy = IIF(CAST(CompSetOccupancy AS FLOAT) < 0.0001, 0, CompSetOccupancy)
, CompSetADR = IIF(CompSetADR AS FLOAT) < 0.0001, 0, CompSetADR);

Otherwise you can just omit the CAST :否则你可以省略CAST

  CompSetOccupancy = IIF(CompSetOccupancy < 0.0001, 0, CompSetOccupancy)

Obviously you have to amend the UPDATE with the other fields.显然,您必须使用其他字段修改UPDATE

PS It helps answering questions like these if you'd show us what is/was in the actual #table. PS 如果您向我们展示实际#table 中的内容,它有助于回答此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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