我有3张如下表。
表A:(有一列员工ID)
Employee Id
101
102
103
表B:(有一列EmployeeName)
EmpName
Sai
sarath
vijay
表C :(有一列DeptName)
DeptName**.
IT
ACCOUNTS
MANAGEMENT
这里我想将所有三个表列值插入一个临时表中。 像
Create table #mytable (employeeid nvarchar(255),empname nvarchar(255),deptname nvarchar(255))
insert into #mytable (employeeid)
select employeeid from tableA
insert into #mytable (empname)
select empname from tableB
insert into #mytable (deptname)
select deptname from tableC
但是我的临时表(即)#mytable给了我下面的结果。
**Employeeid Empname DeptName**
101 null null
102 null null
103 null null
null sai null
null Sarath null
null vijay null
null null IT
null null ACCOUNTS
null null MANAGEMENT
但我想只显示数据行而不是空值。请给我任何建议,将所有三个表,即A,B,C一次插入临时表,即#mytable。并且只显示包含行的数据。
我尝试过如下查询
insert into tb_MBUPSheetData123 ([BusinessStrategyMessaging],Capability,[DealSource],Language,LicenseType,[Proactive activity],[Proposal Type],RFXSolution,
SLBusinessSolutionArea,SolutionForProposal,SupportingSolution,Conversations,TopicForProposal,SalesDeskAgentAlias)
values ((select [BusinessStrategyMessaging] from tb_MBUPSheetData where BusinessStrategyMessaging is not null),
(select [DealSource] FROM tb_MBUPSheetData where DealSource is not null),
(select [Capability] FROM tb_MBUPSheetData where Capability is not null),
(select [Language] FROM tb_MBUPSheetData where Language is not null)
,(select [LicenseType] FROM tb_MBUPSheetData where LicenseType is not null),
(select [Proactive activity] FROM tb_MBUPSheetData where [Proactive activity] is not null),
(select [ProposalType] FROM tb_MBUPSheetData where [Proposal Type] is not null),
(select[RFXSolution] FROM tb_MBUPSheetData where RFXSolution is not null),
(select [SLBusinessSolutionArea] FROM tb_MBUPSheetData where SLBusinessSolutionArea is not null),
(select [SolutionForProposal]FROM tb_MBUPSheetData where SolutionForProposal is not null),
(select [SupportingSolution] FROM tb_MBUPSheetData where SupportingSolution is not null),
(select Conversations FROM tb_MBUPSheetData where Conversations is not null),
(select [TopicForProposal] FROM tb_MBUPSheetData where TopicForProposal is not null),
(select [SalesDeskAgentAlias] FROM tb_MBUPSheetData where SalesDeskAgentAlias is not null))
但我收到的错误
子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。 声明已经终止。
任何建议......
答案 0 :(得分:0)
首先,您的三个表必须具有外键关系,例如员工ID noe,您可以编写类似
的语法insert into mytable(employeeid,empname,deptname) select a.Eamployee_Id,b.EmpName,c.DeptName from table_a a,table_b b,table_c c where a.Eamployee_Id=b.Eamployee_Id and a.Eamployee_Id=c.Eamployee_Id;
答案 1 :(得分:0)
如果你想在#temp表中一次输入所有条目,你可以试试这个: -
insert into #mytable values(select employeeid from tableA where_condition,select empname from tableB where_condition,select deptname from tableC where_condition);