这是我的查询:-
select
(FirstName + ' ' + Lastname) as [Name]
from
c_client
where
MiddleInitial is null
union
select
(FirstName + ' ' + MiddleInitial + '''' + Lastname) as [Name]
from
c_client
where
MiddleInitial is not null
执行后,我得到以下输出:
这是我的新桌子:
CREATE TABLE AddData(Name VARCHAR(MAX))
我想将SELECT
查询生成的结果插入到新表AddData
中。你能帮我做到吗?
答案 0 :(得分:3)
您将使用insert
:
insert into AddData (Name)
Select (FirstName + ' ' + Lastname) as [Name]
from c_client
where MiddleInitial IS NULL
UNION
Select (FirstName + ' ' + MiddleInitial +''''+ Lastname) as [Name]
from c_client
where MiddleInitial IS NOT NULL;
我建议将逻辑写为:
select (FirstName +
coalesce(MiddleInitial + '''', '') +
' ' +
Lastname
) as Name
into AddData
from c_client c;
您不必先创建表。
此外,如果您确实要删除重复项,请使用select distinct
。目前尚不清楚您是故意使用union
来删除重复项还是只是将两个单独的子查询组合在一起。