当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式

时间:2010-02-19 06:35:45

标签: sql sql-server-2005 tsql

这是一个问题

select IssueNo, KendraCode, IssueTime, DateOfIssue, P_MotaBags, P_MotaWeight, P_PatlaBags, P_PatlaWeight, P_SarnaBags, P_SarnaWeight, NewBags,OldBags, TransporterName, TruckNumber, DriverName, TruckOwner,SocietyCode 
,
(SELECT PaddyMotaW, PaddyPatlaW, PaddySarnaW, BagsMota, BagsPatla, BagsSarna,PC_ID, sangrahankendraid, SocietyCode,DM_ID FROM ReceiveFromSociety WHERE DM_ID=S.IssueNo)
from IssueToSangrahanKendra_Soc S
where KendraCode='4403' order by SocietyCode  

问题在哪里

1 个答案:

答案 0 :(得分:5)

您无法在子选择中选择多个字段,只允许使用1个字段。

此外,对代码进行格式化将使您将来更容易阅读。

尝试以下

select  IssueNo, 
        KendraCode, 
        IssueTime, 
        DateOfIssue, 
        P_MotaBags, 
        P_MotaWeight, 
        P_PatlaBags, 
        P_PatlaWeight, 
        P_SarnaBags, 
        P_SarnaWeight, 
        NewBags,
        OldBags, 
        TransporterName, 
        TruckNumber, 
        DriverName, 
        TruckOwner,
        SocietyCode, 
        rfs.*
from    IssueToSangrahanKendra_Soc S LEFT JOIN
        (   SELECT  PaddyMotaW, 
                    PaddyPatlaW, 
                    PaddySarnaW, 
                    BagsMota, 
                    BagsPatla, 
                    BagsSarna,
                    PC_ID, 
                    sangrahankendraid, 
                    SocietyCode,
                    DM_ID 
            FROM    ReceiveFromSociety 
        ) rfs ON rfs.DM_ID =S.IssueNo
where   KendraCode='4403' 
order by    SocietyCode