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

时间:2014-04-02 15:42:20

标签: sql sql-server

我收到此错误,我不确定如何修复它。我试图决定@type =一件事是从那个临时表中选择一切。有2个临时表,所以有2个选择。 还有第三种选择,他们可以选择两个表,我正在一起工作。 堆栈溢出错误的解释似乎不合适,因为我之前使用过它并且它起作用(至少是union部分)。 作为旁注,我想在工会部分做一个order by子句,但它不会让我。有没有办法做到这一点? 继承人导致错误的部分。两个临时表在单独运行时工作正常,但不能通过此select语句运行。我正在使用sql server 2005

Select
        Case 
            WHEN @type like 'Sent%' THEN
                (Select * from #Sent)
            WHEN @Type like 'Confirm%' THEN
                (Select * From #Confirmed)
            Else
                ( Select 
                    T.QT#
                    ,T.QTLine
                    ,T.DIVISION
                    ,T.Customer
                    ,T.CustomerAccount
                    ,T.Industry
                    ,T.Type
                    ,T.ProgramName
                    ,T.SalesGroup
                    ,T.Recipient
                    ,T.ItemGroup
                    ,T.OEM
                    ,T.CustomerClassification
                    ,T.Finish
                    ,T.Item
                    ,T.EAU
                    ,T.PriceUnit
                    ,T.Price
                    ,T.ExtValue
                    ,T.VMPercent
                    ,T.VM$
                    ,T.RFQRecdTime
                    ,T.SentTime
                    ,T.QuotationToSentTurnTime
                    ,T.RFQRecdDate
                    ,T.SentDate
                    ,T.ConfirmedDate
                    ,T.QuotetoOrderTurnTime
                    ,T.FiscalMonthSent
                    ,T.FiscalMonthConfirmed
                    ,T.Status
                FROM
                    (
                        Select
                            QT#
                            ,QTLine
                            ,DIVISION
                            ,Customer
                            ,CustomerAccount
                            ,Industry
                            ,[Type]
                            ,ProgramName
                            ,SalesGroup
                            ,Recipient
                            ,ItemGroup
                            ,[OEM]
                            ,CustomerClassification
                            ,Finish
                            ,Item
                            ,EAU
                            ,PriceUnit
                            ,Price
                            ,ExtValue
                            ,VMPercent
                            ,VM$
                            ,RFQRecdTime
                            ,SentTime
                            ,QuotationToSentTurnTime
                            ,RFQRecdDate
                            ,SentDate
                            ,ConfirmedDate
                            ,QuotetoOrderTurnTime
                            ,FiscalMonthSent
                            ,FiscalMonthConfirmed
                            ,[Status]
                        From
                            #Sent
                    UNION ALL
                        Select
                            QT#
                            ,QTLine
                            ,DIVISION
                            ,Customer
                            ,CustomerAccount
                            ,Industry
                            ,[Type]
                            ,ProgramName
                            ,SalesGroup
                            ,Recipient
                            ,ItemGroup
                            ,[OEM]
                            ,CustomerClassification
                            ,Finish
                            ,Item
                            ,EAU
                            ,PriceUnit
                            ,Price
                            ,ExtValue
                            ,VMPercent
                            ,VM$
                            ,RFQRecdTime
                            ,SentTime
                            ,QuotationToSentTurnTime
                            ,RFQRecdDate
                            ,SentDate
                            ,ConfirmedDate
                            ,QuotetoOrderTurnTime
                            ,FiscalMonthSent
                            ,FiscalMonthConfirmed
                            ,[Status]
                        From
                            #Confirmed
    ) T
    GROUP BY
                    T.QT#
                    ,T.QTLine
                    ,T.DIVISION
                    ,T.Customer
                    ,T.CustomerAccount
                    ,T.Industry
                    ,T.Type
                    ,T.ProgramName
                    ,T.SalesGroup
                    ,T.Recipient
                    ,T.ItemGroup
                    ,T.OEM
                    ,T.CustomerClassification
                    ,T.Finish
                    ,T.Item
                    ,T.EAU
                    ,T.PriceUnit
                    ,T.Price
                    ,T.ExtValue
                    ,T.VMPercent
                    ,T.VM$
                    ,T.RFQRecdTime
                    ,T.SentTime
                    ,T.QuotationToSentTurnTime
                    ,T.RFQRecdDate
                    ,T.SentDate
                    ,T.ConfirmedDate
                    ,T.QuotetoOrderTurnTime
                    ,T.FiscalMonthSent
                    ,T.FiscalMonthConfirmed
                    ,T.Status

2 个答案:

答案 0 :(得分:0)

我用If ... else

替换了case语句
    IF @Type like 'Sent%'

            (SELECT 
                        QT#
                        ,QTLine
                        ,DIVISION
                        ,Customer
                        ,CustomerAccount
                        ,Industry
                        ,[Type]
                        ,ProgramName
                        ,SalesGroup
                        ,Recipient
                        ,ItemGroup
                        ,[OEM]
                        ,CustomerClassification
                        ,Finish
                        ,Item
                        ,EAU
                        ,PriceUnit
                        ,Price
                        ,ExtValue
                        ,VMPercent
                        ,VM$
                        ,RFQRecdTime
                        ,SentTime
                        ,QuotationToSentTurnTime
                        ,RFQRecdDate
                        ,SentDate
                        ,ConfirmedDate
                        ,QuotetoOrderTurnTime
                        ,FiscalMonthSent
                        ,FiscalMonthConfirmed
                        ,[Status]
            FROM #Sent)

        IF @Type like 'Confirm%' 
            (SELECT 
                        QT#
                        ,QTLine
                        ,DIVISION
                        ,Customer
                        ,CustomerAccount
                        ,Industry
                        ,[Type]
                        ,ProgramName
                        ,SalesGroup
                        ,Recipient
                        ,ItemGroup
                        ,[OEM]
                        ,CustomerClassification
                        ,Finish
                        ,Item
                        ,EAU
                        ,PriceUnit
                        ,Price
                        ,ExtValue
                        ,VMPercent
                        ,VM$
                        ,RFQRecdTime
                        ,SentTime
                        ,QuotationToSentTurnTime
                        ,RFQRecdDate
                        ,SentDate
                        ,ConfirmedDate
                        ,QuotetoOrderTurnTime
                        ,FiscalMonthSent
                        ,FiscalMonthConfirmed
                        ,[Status] FROM #Confirmed)
        ELSE
            ( SELECT 
                T.QT#
                ,T.QTLine
                ,T.DIVISION
                ,T.Customer
                ,T.CustomerAccount
                ,T.Industry
                ,T.Type
                ,T.ProgramName
                ,T.SalesGroup
                ,T.Recipient
                ,T.ItemGroup
                ,T.OEM
                ,T.CustomerClassification
                ,T.Finish
                ,T.Item
                ,T.EAU
                ,T.PriceUnit
                ,T.Price
                ,T.ExtValue
                ,T.VMPercent
                ,T.VM$
                ,T.RFQRecdTime
                ,T.SentTime
                ,T.QuotationToSentTurnTime
                ,T.RFQRecdDate
                ,T.SentDate
                ,T.ConfirmedDate
                ,T.QuotetoOrderTurnTime
                ,CONVERT(VarChar,T.FiscalMonthSent) As FiscalMonthSent
                ,CONVERT(VARCHAR,T.FiscalMonthConfirmed) AS FiscalMonthConfirmed
                ,T.Status
            FROM
                (
                    SELECT
                        QT#
                        ,QTLine
                        ,DIVISION
                        ,Customer
                        ,CustomerAccount
                        ,Industry
                        ,[Type]
                        ,ProgramName
                        ,SalesGroup
                        ,Recipient
                        ,ItemGroup
                        ,[OEM]
                        ,CustomerClassification
                        ,Finish
                        ,Item
                        ,EAU
                        ,PriceUnit
                        ,Price
                        ,ExtValue
                        ,VMPercent
                        ,VM$
                        ,RFQRecdTime
                        ,SentTime
                        ,QuotationToSentTurnTime
                        ,RFQRecdDate
                        ,SentDate
                        ,ConfirmedDate
                        ,QuotetoOrderTurnTime
                        ,CONVERT(VarChar,FiscalMonthSent) AS FiscalMonthSent
                        ,CONVERT(VarChar,FiscalMonthConfirmed) AS FiscalMonthConfirmed
                        ,[Status]
                    FROM
                        #Sent
                UNION ALL
                    SELECT
                        QT#
                        ,QTLine
                        ,DIVISION
                        ,Customer
                        ,CustomerAccount
                        ,Industry
                        ,[Type]
                        ,ProgramName
                        ,SalesGroup
                        ,Recipient
                        ,ItemGroup
                        ,[OEM]
                        ,CustomerClassification
                        ,Finish
                        ,Item
                        ,EAU
                        ,PriceUnit
                        ,Price
                        ,ExtValue
                        ,VMPercent
                        ,VM$
                        ,RFQRecdTime
                        ,SentTime
                        ,QuotationToSentTurnTime
                        ,RFQRecdDate
                        ,SentDate
                        ,ConfirmedDate
                        ,QuotetoOrderTurnTime
                        ,CONVERT(VarChar,FiscalMonthSent) AS FiscalMonthSent
                        ,CONVERT(VarChar,FiscalMonthConfirmed) AS FiscalMonthConfirmed
                        ,[Status]
                    FROM
                        #Confirmed
) T
GROUP BY
                T.QT#
                ,T.QTLine
                ,T.DIVISION
                ,T.Customer
                ,T.CustomerAccount
                ,T.Industry
                ,T.Type
                ,T.ProgramName
                ,T.SalesGroup
                ,T.Recipient
                ,T.ItemGroup
                ,T.OEM
                ,T.CustomerClassification
                ,T.Finish
                ,T.Item
                ,T.EAU
                ,T.PriceUnit
                ,T.Price
                ,T.ExtValue
                ,T.VMPercent
                ,T.VM$
                ,T.RFQRecdTime
                ,T.SentTime
                ,T.QuotationToSentTurnTime
                ,T.RFQRecdDate
                ,T.SentDate
                ,T.ConfirmedDate
                ,T.QuotetoOrderTurnTime
                ,T.FiscalMonthSent
                ,T.FiscalMonthConfirmed
                ,T.Status
    )

答案 1 :(得分:0)

您可以使用两个SELECT的单个并集来满足所有三种情况。

基本上,您希望在参数为#Sent时读取LIKE 'Sent%',或者在 LIKE 'Confirm%'时读取其他内容,同时#Confirmed 1}}:参数应该是LIKE 'Confirm%'不是 LIKE 'Sent%'

所以,这就是你的查询的样子:

SELECT
    QT#,
    ...
FROM #Sent
WHERE @type     LIKE 'Sent%'
   OR @type NOT LIKE 'Confirm%'

UNION

SELECT
    QT#,
    ...
FROM #Confirmed
WHERE @type     LIKE 'Confirm%'
   OR @type NOT LIKE 'Sent%'
;

请注意,@type的第三个值或模式不应该为NULL。如果它可以为NULL并且NULL应该与两个表匹配,那么您需要通过替换

来稍微修改条件
... OR @type NOT LIKE ...

类似

... OR ISNULL(@type, '') NOT LIKE ...

请注意,GROUP BY没有使用,UNION代替UNION ALL。在您的查询中,您使用从每个表中提取的相同列对组合集进行分组,并且最终在输出中返回相同的列。我的理解是该方法应该消除两个表中可能发生的重复。但这实际上是轮子的重新发明,因为UNION就是这样做的:它结合了两组来消除重复。因此,您的查询也会在这方面得到简化,如图所示。