根据两个预先存在的列的内容向现有 table 添加新列
Adding a new column to an existing table based on the contents of two preexisting columns
我正在尝试向 SQL 服务器中的现有 table 添加一个新列。我希望根据 table 中两个现有列的内容来确定这个新列的值。例如,有一列称为制造商,另一列称为供应商。当这两列的内容相同时我希望新列显示'Same',如果两列内容不相同我希望新列显示[=19] =].
下面的语句向我展示了我正在寻找的所有值,但我不知道如何根据这些结果如上所述添加新列。我一直在论坛上搜索,查看了一些 Case 语句,但没有成功。
Select *
from dbo.[Merchandise]
where [Manufacturer] = [Vendor]
如有任何帮助,我们将不胜感激。
听起来像 case
表达式:
select m.*,
(case when manufacturor = vendor then 'Same' else 'Not the same' end) as newcolumn
from merchandise;
听起来你想要一个计算列:
alter table marchandise
add newcol
as (case when manufacturer = vendor then 'same' else 'not the same' end)
;
这会向 table 添加一个新列,称为 newcol
,其值是根据其他两列的值自动计算的。
我可以使用以下方法添加带有条件的列。
alter table TABLE_NAME
add NEW_COL_NAME as (case
when COL1_NAME = COL2_NAME then 'same'
else 'not the same'
end);
给你的,可能喜欢
alter table Merchandise
add NEW_COL_NAME as (case
when Manufacturer = Vendor then 'Same'
else 'Not the Same'
end);
我正在尝试向 SQL 服务器中的现有 table 添加一个新列。我希望根据 table 中两个现有列的内容来确定这个新列的值。例如,有一列称为制造商,另一列称为供应商。当这两列的内容相同时我希望新列显示'Same',如果两列内容不相同我希望新列显示[=19] =].
下面的语句向我展示了我正在寻找的所有值,但我不知道如何根据这些结果如上所述添加新列。我一直在论坛上搜索,查看了一些 Case 语句,但没有成功。
Select *
from dbo.[Merchandise]
where [Manufacturer] = [Vendor]
如有任何帮助,我们将不胜感激。
听起来像 case
表达式:
select m.*,
(case when manufacturor = vendor then 'Same' else 'Not the same' end) as newcolumn
from merchandise;
听起来你想要一个计算列:
alter table marchandise
add newcol
as (case when manufacturer = vendor then 'same' else 'not the same' end)
;
这会向 table 添加一个新列,称为 newcol
,其值是根据其他两列的值自动计算的。
我可以使用以下方法添加带有条件的列。
alter table TABLE_NAME
add NEW_COL_NAME as (case
when COL1_NAME = COL2_NAME then 'same'
else 'not the same'
end);
给你的,可能喜欢
alter table Merchandise
add NEW_COL_NAME as (case
when Manufacturer = Vendor then 'Same'
else 'Not the Same'
end);