如何在SQL Server中将表数据的值更改为“可用”?
我有一个表production.productioninventory,其中包含以下列和示例数据:如何在SQL Server中将表数据的值更改为“可用”?,sql,sql-server,Sql,Sql Server,我有一个表production.productioninventory,其中包含以下列和示例数据: productID shelf ------------------- 1 A 2 B 3 C 4 N/A 我想将工具架数据更改为“可用”,但不适用 例如: productID shelf ------------------- 1 available 2
productID shelf
-------------------
1 A
2 B
3 C
4 N/A
我想将工具架数据更改为“可用”,但不适用
例如:
productID shelf
-------------------
1 available
2 available
3 available
4 not available
这将有助于:
1用例
2使用IIF
这将有助于:
1用例
2使用IIF
您甚至不需要更改数据。相反,只需添加一个计算列:
alter table production.productioninventory
add availability as (case when shelf = 'N/A' then 'not available' else 'available' end);
这将成为表定义的一部分,可在查询中使用。您甚至不需要更改数据。相反,只需添加一个计算列:
alter table production.productioninventory
add availability as (case when shelf = 'N/A' then 'not available' else 'available' end);
这将成为表定义的一部分,并可在查询中使用。@Squirrel为了简单起见,您可以在构造时使用case并将其合并为一个statement@Squirrel为了简单起见,您可以在构造时使用case并将其合并到一个语句中
UPDATE production.productioninventory
SET shelf = CASE
WHEN shelf = 'N/A' THEN 'not available'
ELSE 'available'
END
UPDATE production.productioninventory
SET shelf = IIF(shelf = 'N/A', 'not available' , 'available')
alter table production.productioninventory
add availability as (case when shelf = 'N/A' then 'not available' else 'available' end);