使用SQL从一个#Temp表中的两个表中获取显示数据

时间:2018-09-02 23:55:23

标签: sql sql-server

我有两个不同的表,我希望使用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

预先感谢您对我的帮助。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用UNION或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

答案 1 :(得分:0)

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表中的值。