我试图在一个存储过程中执行两个选择查询。但是它没有给我任何输出。没有错误,但记录未显示。
首先,我尝试了此存储过程
CREATE PROCEDURE [dbo].[Sp_PriceList_Die_Wise]
@DieNo As Nvarchar(15),
@MetalCode As Int
AS
BEGIN
Select CP.BatchQty,CP.CastingPrice,U.UNITName As 'Unit',MachPrice,U.UNITName As 'M/C Unit' from CustomerPriceList As CP Left Outer Join UNITMaster As U On CP.MUNITID=U.UNITID where MOULDCODE=@DieNo And METALCODE=@MetalCode
SELECT TOP (10) SubOADetail.OANO, SubOADetail.ID, SubOADetail.QTY, SubOADetail.RATE, UNITMaster.UNITName As Unit, OATest.MachPrices, UNITMaster_1.UNITName AS Unit FROM UNITMaster AS UNITMaster_1 RIGHT OUTER JOIN OATest ON UNITMaster_1.UNITID = OATest.MachUnitID RIGHT OUTER JOIN SubOADetail LEFT OUTER JOIN UNITMaster ON SubOADetail.UNIT = UNITMaster.UNITID ON OATest.ID = SubOADetail.ID AND OATest.OANO = SubOADetail.OANO LEFT OUTER JOIN OADetails ON SubOADetail.OANO = OADetails.OANO WHERE SubOADetail.MOULDCODE = @DieNo AND SubOADetail.METALCODE =@MetalCode ORDER BY OADetails.OADATE DESC
END
这给了我这样的输出
但是我想要在单个表中输出,所以我创建了这个存储过程
CREATE PROCEDURE [dbo].[Sp_PriceList_Die_Wise]
@DieNo As Nvarchar(15),
@MetalCode As Int
AS
BEGIN
WITH main AS
(
Select CP.BatchQty,CP.CastingPrice,U.UNITName As 'Unit',
MachPrice,U.UNITName As 'M/C Unit' from CustomerPriceList As CP
Left Outer Join UNITMaster As U On CP.MUNITID=U.UNITID
where MOULDCODE=@DieNo And METALCODE=@MetalCode
), sub AS
(
SELECT TOP (10) SubOADetail.OANO, SubOADetail.ID, SubOADetail.QTY, SubOADetail.RATE,
UNITMaster.UNITName As Unit, OATest.MachPrices, UNITMaster_1.UNITName AS MCUnit
FROM UNITMaster AS UNITMaster_1 RIGHT OUTER JOIN OATest ON UNITMaster_1.UNITID = OATest.MachUnitID
RIGHT OUTER JOIN SubOADetail LEFT OUTER JOIN UNITMaster ON SubOADetail.UNIT = UNITMaster.UNITID
ON OATest.ID = SubOADetail.ID AND OATest.OANO = SubOADetail.OANO LEFT OUTER JOIN OADetails
ON SubOADetail.OANO = OADetails.OANO WHERE SubOADetail.MOULDCODE = @DieNo
AND SubOADetail.METALCODE =@MetalCode ORDER BY OADetails.OADATE DESC
)
SELECT *
FROM main m join sub s
ON m.MachPrice = s.MachPrices
END
但这给我这样的空白记录
答案 0 :(得分:1)
您正在使用revoke()
,并且您的第一个表(主表)没有任何记录,因此您将不会获得任何输出。
如果即使主记录中不存在匹配记录,也要从其他表中进行选择,则需要将INNER JOIN
更改为JOIN
,如下所示。
RIGHT JOIN
编辑:
SELECT *
FROM main m right join sub s
ON m.MachPrice = s.MachPrices
将适用于提供的样本数据,以防万一,如果您拥有左CTE的数据在右CTE中没有匹配的记录,反之亦然,而您仍想选择记录,那么在这种情况下,需要RIGHT JOIN
和LEFT JOIN
的组合,为此,您可以像下面这样使用RIGHT JOIN
。
FULL OUTER JOIN