在asp.net post部署脚本中更新t-sql表

时间:2022-06-25 21:12:17

How can I update a table's columns with my custom values in T-SQL?

如何使用T-SQL中的自定义值更新表的列?

Let's say I have a table:

假设我有一张桌子:

Id  |  Name  |  Currency
----+--------+-----------
1   | name1  |     $
2   | name2  |     $
3   | name3  |     €

I want to update each Currency value to USD or EUR depending on which symbol there is. So the result should look like this:

我想将每个货币值更新为美元或欧元,具体取决于哪个符号。所以结果应该是这样的:

    Id  |  Name  |  Currency
    ----+--------+-----------
    1   | name1  |     USD
    2   | name2  |     USD
    3   | name3  |     EUR

How could I do this??

我怎么能这样做?

2 个解决方案

#1


4  

Nikas.

Nikas。

You could achieve the desired output by updating the table values and using separate cases for each symbol.

您可以通过更新表值并为每个符号使用单独的大小写来实现所需的输出。

UPDATE Currencies
SET Currency = CASE
    WHEN Currency = '$' THEN 'USD'
    WHEN Currency = '€' THEN 'EUR'
    ELSE Currency
END
WHERE
    Currency IN ('$', '€')

#2


0  

Update your table with update statements as below: Replace MyTable with your table name.

使用update语句更新表,如下所示:将MyTable替换为您的表名。

UPDATE MyTable SET Currency='USD' WHERE Currency='$';
UPDATE MyTable SET Currency='EUR' WHERE Currency='€';

#1


4  

Nikas.

Nikas。

You could achieve the desired output by updating the table values and using separate cases for each symbol.

您可以通过更新表值并为每个符号使用单独的大小写来实现所需的输出。

UPDATE Currencies
SET Currency = CASE
    WHEN Currency = '$' THEN 'USD'
    WHEN Currency = '€' THEN 'EUR'
    ELSE Currency
END
WHERE
    Currency IN ('$', '€')

#2


0  

Update your table with update statements as below: Replace MyTable with your table name.

使用update语句更新表,如下所示:将MyTable替换为您的表名。

UPDATE MyTable SET Currency='USD' WHERE Currency='$';
UPDATE MyTable SET Currency='EUR' WHERE Currency='€';