我有两个不同的表,我希望从SQL Server中使用存储过程或SQL查询按日期在一个#temp
表中获取数据。
表1 DcMaster
(交付卡拉汉表 - 9列)
ID Stus DcEntryDate DcEntryTime CustomerID MasTotalQty ByHand VehicleNum UserID
----------------------------------------------------------------------------------------------------------
1 DC 2018-05-07 2018-05-07 23:58:54 3 15 Sara KGH-6678 15
2 DC 2018-05-07 2018-05-07 23:59:35 5 1200 Hamid KGH-6678 15
3 DC 2018-05-08 2018-05-07 20:21:31 2 680 Zeeshan KGH-6678 15
4 DC 2018-05-11 2018-05-07 09:10:29 3 1000 Sara KGH-6678 15
表2 DcRMaster
(交付卡拉汉返回表 - 10列)
ID Stus DcREntryDate DcREntryTime ReffNum CustomerID MasTotalQty ByHand VehicleNum UserID
--------------------------------------------------------------------------------------------------------------
1 DcR 2018-05-16 2018-05-07 23:58:54 4 3 500 Sara KGH-6678 15
2 DcR 2018-05-19 2018-05-07 23:59:35 2 5 200 Hamid KGH-6678 15
临时表Display Table
- 我想在这样的#Temp表中显示(11列)
Tr ID Stus DcREntryDate DcREntryTime ReffNum CustomerID MasTotalQty ByHand VehicleNum UserID
------------------------------------------------------------------------------------------------------------------
1 1 DcR 2018-05-16 2018-05-07 23:58:54 4 3 500 Sara KGH-6678 15
2 1 DC 2018-05-07 2018-05-07 23:58:54 3 15 Sara KGH-6678 15
3 2 DC 2018-05-07 2018-05-07 23:59:35 5 1200 Hamid KGH-6678 15
4 3 DC 2018-05-08 2018-05-07 20:21:31 2 680 Zeeshan KGH-6678 15
5 4 DC 2018-05-11 2018-05-07 09:10:29 3 1000 Sara KGH-6678 15
2 2 DcR 2018-05-19 2018-05-07 23:59:35 2 5 200 Hamid KGH-6678 15
提前谢谢你的帮助。
您可以尝试使用UNION OR UNION ALL运算符来合并这样的两个表中的数据(我希望它可以帮到您)
SELECT Tr,ID,Stus,DcREntryDate,DcREntryTime,NULL AS ReffNum,CustomerID,MasTotalQty,ByHand,VehicleNum,UserID
FROM DcMaster
UNION ALL
SELECT Tr,ID,Stus,DcREntryDate,DcREntryTime,ReffNum,CustomerID,MasTotalQty,ByHand,VehicleNum,UserID
FROM DcRMaster
ORDER BY ID
create view combine
as
SELECT Tr,ID,Stus,DcREntryDate,DcREntryTime,NULL AS ReffNum,CustomerID,MasTotalQty,ByHand,VehicleNum,UserID
FROM DcMaster
UNION ALL
SELECT
Tr,ID,Stus,DcREntryDate,DcREntryTime,ReffNum,CustomerID,MasTotalQty,ByHand,VehicleNum,UserID
FROM DcRMaster
ORDER BY ID
go
select * into #temp from combine
go
select * from #temp
1.对于两个表的并集,要添加的列数不同,并通过提供空列来平衡2个表。
2.然后为查询创建视图,以获得结果。
3.之后使用带有值查询的复制表来创建具有结果值的临时表。
4.然后选择语句以显示#temp表中的值。