简体   繁体   English

在SQL中选择内部联接选择

[英]Select Inner Join Select in SQL

Hi i Have a Select Inner Join Statement it seems ok but i got error. 嗨,我有一个选择内部加入声明,这似乎还可以,但我出错了。 this is my query 这是我的查询

ALTER PROCEDURE [dbo].[POBalance]  @PONumber nvarchar(50)
AS BEGIN
Select( 
Select 
    A.Description,
    C.qty as POqty,
    B.QtyDelivered as PDQty, 
    case when A.partialflag ='false' 
    then '0'
    else
    A.qty  end as Balance,
    A.Unit,
    A.Unitprice,
    A.Partialflag 
from tblPOdetails as A

Inner Join  ( SELECT  id, SUM(Qty) AS QtyDelivered
                                    FROM         dbo.tblPDdetails
                                    WHERE     (PONo = @PONumber)
                                    GROUP BY id)as B On A.id = B.id   
Inner Join tblpodetailshistory as C on A.id =C.id

where A.PONo = @PONumber)
END

I got this Error. 我收到此错误。

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. 如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式。

Thank you in Advance. 先感谢您。

You should include the column names and From keyword in the outer select statement 您应该在外部select语句中包含列名和From关键字

ALTER PROCEDURE [dbo].[POBalance]  @PONumber nvarchar(50)
AS BEGIN
Select Description, POqty, PDQty, Balance, Unit, Unitprice, Partialflag  from ( 
Select 
    A.Description,
    C.qty as POqty,
    B.QtyDelivered as PDQty, 
    case when A.partialflag ='false' 
    then '0'
    else
    A.qty  end as Balance,
    A.Unit,
    A.Unitprice,
    A.Partialflag 
from tblPOdetails as A

Inner Join  ( SELECT  id, SUM(Qty) AS QtyDelivered
                                    FROM         dbo.tblPDdetails
                                    WHERE     (PONo = @PONumber)
                                    GROUP BY id)as B On A.id = B.id   
Inner Join tblpodetailshistory as C on A.id =C.id

where A.PONo = @PONumber)
END

Actually you don't need to write the outer select, it can be rewritten as 实际上,您不需要编写外部选择,可以将其重写为

ALTER PROCEDURE [dbo].[POBalance]  @PONumber nvarchar(50)
AS BEGIN

Select 
    A.Description,
    C.qty as POqty,
    B.QtyDelivered as PDQty, 
    case when A.partialflag ='false' 
    then '0'
    else
    A.qty  end as Balance,
    A.Unit,
    A.Unitprice,
    A.Partialflag 
from tblPOdetails as A

Inner Join  ( SELECT  id, SUM(Qty) AS QtyDelivered
                                    FROM         dbo.tblPDdetails
                                    WHERE     (PONo = @PONumber)
                                    GROUP BY id)as B On A.id = B.id   
Inner Join tblpodetailshistory as C on A.id =C.id

where A.PONo = @PONumber
END

The issue is at the beginning of your query, 问题出在查询的开头,

Select( Select ....

Remove the initial "Select (" and retry the query. When you run a 删除初始的“选择(”,然后重试查询。当您运行

Select (Select ..) 

the second select can only return one column (unless you concatenate the returned data). 第二个选择只能返回一列(除非您串联返回的数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM