This is my query:-
这是我的疑问: -
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
After executing it, I'm getting this output:
执行后,我得到这个输出:
This is my new table:
这是我的新表:
CREATE TABLE AddData(Name VARCHAR(MAX))
I want to insert the result generated by the SELECT
query into my new table AddData
. Can you help me do this?
我想将SELECT查询生成的结果插入到我的新表AddData中。你能帮帮我吗?
1 个解决方案
#1
3
You would use 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;
I would instead suggest writing the logic as :
我建议写逻辑为:
select (FirstName +
coalesce(MiddleInitial + '''', '') +
' ' +
Lastname
) as Name
into AddData
from c_client c;
You won't have to create the table first.
您不必先创建表。
Also, if you do want to remove duplicates then use select distinct
. It is not clear if you are using union
intentionally to remove duplicates or just to combine two separate subqueries.
此外,如果您确实要删除重复项,请使用select distinct。目前尚不清楚您是否故意使用联合删除重复项或仅仅组合两个单独的子查询。
#1
3
You would use 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;
I would instead suggest writing the logic as :
我建议写逻辑为:
select (FirstName +
coalesce(MiddleInitial + '''', '') +
' ' +
Lastname
) as Name
into AddData
from c_client c;
You won't have to create the table first.
您不必先创建表。
Also, if you do want to remove duplicates then use select distinct
. It is not clear if you are using union
intentionally to remove duplicates or just to combine two separate subqueries.
此外,如果您确实要删除重复项,请使用select distinct。目前尚不清楚您是否故意使用联合删除重复项或仅仅组合两个单独的子查询。