如果语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中
Posted
技术标签:
【中文标题】如果语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中【英文标题】:ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator 【发布时间】:2012-10-19 05:29:59 【问题描述】:下面是我的存储过程,但执行时出现以下错误:
消息 104,级别 16,状态 1,过程 ACEsp_AuditInvoice,第 84 行 ORDER BY 项必须出现在选择列表中,如果语句 包含 UNION、INTERSECT 或 EXCEPT 运算符。
请查看我的存储过程,并告诉我如何避免上述错误。 在此先感谢
USE [FAC]
GO
/****** Object: StoredProcedure [dbo].[ACEsp_AuditInvoice] Script Date: 10/18/2012 09:26:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Ryan
-- Create date: 18 Oct 2012
-- version: 12.10
-- Description: Originated from ACEsp_InvVsListPrice, modified for invoice auditing
-- parameter by date, month and year
-- Additional 10 columns for invoice auditing
-- =============================================
--[dbo].[ACEsp_AuditInvoice] '2012-09-17','2012-09-17','SEPTEMBER','2012'
ALTER PROCEDURE [dbo].[ACEsp_AuditInvoice]
-- Add the parameters for the stored procedure here
@dtfrom datetime,
@dtto datetime,
@MNTH varchar(12),
@YR INT
--@prcMakro varchar(15)
WITH RECOMPILE
AS
create table #brand (
[CODE] [char](15) COLLATE SQL_Latin1_General_CP1_CI_AS ,
[IVBRAND] [char](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
)
create table #SubCat (
[CODE] [char](15) COLLATE SQL_Latin1_General_CP1_CI_AS ,
[IVSUBCAT] [char](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
)
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @SDFROM VARCHAR(20)
DECLARE @SDTO VARCHAR(20)
SET @SDFROM = CONVERT(VARCHAR(10), @DTFROM, 101) + ' 00:00:00'
SET @DTFROM = CAST(@SDFROM AS DATETIME)
SET @SDTO = CONVERT(VARCHAR(10), @DTTO, 101) + ' 23:59:59'
SET @DTTO = CAST(@SDTO AS DATETIME)
insert into #brand
select USCATVAL, Image_URL
from IV40600
where USCATNUM = 2
insert into #SubCat
select USCATVAL, Image_URL
from IV40600
where USCATNUM = 4
-- Insert statements for procedure here
select 'posted' as Trx, d.quantity, d.qtytoinv, d.qtycance, (d.qtytoinv - d.qtyfulfi) 'qtyfulfivar',
(u.equomqty * d.qtyfulfi) 'qtybaseuom',
(d.XTNDPRCE -
(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)) 'variance',
CASE(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)
WHEN 0 THEN 0
ELSE
(d.XTNDPRCE /
((d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)))-1
end as 'percentvar',
p.PRCSHID, h.docdate, h.bachnumb, h.ReqShipDate,
d.lnitmseq, d.locncode, d.UNITPRCE,
p.psitmval as [LIST PRICE], i.stndcost, d.qtybsuom, c.slprsnid, sp.slprsnfn,
sp.sprsnsln, c.salsterr, st.slterdsc, h.custnmbr, h.custname,
c.shrtname as 'CUSTOGROUP', c.custclas as 'CUSTOCLAS', c2.ZIP as 'CHANSEG',
h.sopnumbe, d.itemnmbr, d.itemdesc,
case i.itemtype
WHEN 1 THEN 'SALES INVENTORY'
WHEN 2 THEN 'DISCONTINUED'
WHEN 3 THEN 'KITS'
end as 'ITEMTYPE',
d.qtyfulfi, d.uofm, d.XTNDPRCE, u.equivuom 'BASEUOM', u.equomqty 'PACKING',
d.qtyfulfi*u.equomqty 'bqty',
isnull( (SELECT TOP 1 LISTPRICE FROM tbl_disco_historical_baseuom HH WHERE HH.ITEMNMBR = D.ITEMNMBR AND HH.BASEUOM = U2.BASEUOFM AND HH.LEFFECTDATE >= H.DOCDATE ORDER BY HH.LEFFECTDATE DESC),
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)) as 'BASE PRICE – LIST PRICE PER BUOM',
isnull(p.psitmval*d.qtyfulfi*u.equomqty,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM ), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
* d.qtyfulfi*u.equomqty
)
AS 'EXTND PRICE by BASE PRICE-BUOM',
p1.psitmval as 'MAKRO PRICE – LIST PRICE',
p1.psitmval*d.qtyfulfi as 'EXTND PRICE BY MAKRO',
d.qtyfulfi*u.equomqty AS 'QTYBASEUOM',
ISNULL(
(SELECT TOP 1 LISTPRICE FROM tbl_disco_historical_baseuom HH WHERE HH.ITEMNMBR = D.ITEMNMBR AND HH.BASEUOM = U2.BASEUOFM AND HH.LEFFECTDATE >= H.DOCDATE ORDER BY HH.LEFFECTDATE DESC),
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
))
as 'BaseUomListPrice',
(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)
as 'BaseUomTotalListPrice',
(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
) - D.XTNDPRCE AS 'TOTAL DISCOUNT',
a.IVBRAND as 'BRAND', b.IVSUBCAT as 'SubCat',
dbo.ACEfunc_CUSTOTYPE2(c.CUSTCLAS,c.SALSTERR) AS 'CUSTTYPE', D.MRKDNAMT*d.qtyfulfi AS 'MARKDOWN',
cast(i.uscatvls_6 as float) as 'ITEM REPORT SEQUENCE',
rtrim(ltrim(cast(uscatvls_1 as varchar(4)))) + '-' + bh.image_url as 'BRAND HANDLER',
I.ITMCLSCD 'ITEM CLASS', d.qtyfulfi*u.equomqty/uu.equomqty AS 'CASEQTY',
ii.inet1 as 'ABC CAT',
ISNULL(APL.PCPRICE,isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)) as 'APL(SAG) Piece Price', h.docid as 'SOP TYPE ID'
from sop30200 h
inner join sop30300 d on (h.sopnumbe = d.sopnumbe)
inner join IV00101 i on (d.itemnmbr = i.itemnmbr)
inner join RM00101 c on (h.custnmbr = c.custnmbr)
left join RM00102 c2 on (c.custnmbr = c2.custnmbr and c2.adrscode = 'MAIN')
left join RM00301 sp on (sp.slprsnid = c.slprsnid)
left join RM00303 st on (st.salsterr = c.salsterr)
LEFT JOIN iv40202 u on (i.uomschdl = u.uomschdl and d.uofm = u.uofm and u.qtybsuom <> 0)
LEFT JOIN iv40201 u2 on (i.uomschdl = u2.uomschdl)
left join iv10402 p1 on (d.itemnmbr = p1.itemnmbr and p1.PRCSHID = 'PLMAKRO'
and d.uofm = p1.uofm)
left join iv10402 p on (d.itemnmbr = p.itemnmbr and p.PRCSHID = 'BASE PRICE'
and U.EQUIVUOM = p.uofm)
left join #brand a on (i.uscatvls_2 = a.code)
left join #SubCat b on (i.uscatvls_4 = b.code)
left join iv40600 bh on (i.uscatvls_1 = bh.uscatval AND bh.uscatnum = 1)
left join iv40202 uu on (i.uomschdl = uu.uomschdl and i.selnguom = uu.uofm and uu.qtybsuom <> 0)
LEFT join sy01200 as ii on (ii.master_id = d.itemnmbr and ii.master_type = 'ITM')
LEFT JOIN T0_APL APL ON (D.ITEMNMBR=APL.ITEMNMBR AND APL.MNTH = @MNTH AND APL.YR = @YR)
UNION ALL
select 'unposted' as Trx, d.quantity, d.qtytoinv, d.qtycance, (d.qtytoinv - d.qtyfulfi) 'qtyfulfivar',
(u.equomqty * d.qtyfulfi) 'qtybaseuom',
(d.XTNDPRCE -
(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)) 'variance',
CASE(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)
WHEN 0 THEN 0
ELSE
(d.XTNDPRCE /
((d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)))-1
end as 'percentvar',
p.PRCSHID, h.docdate, h.bachnumb, h.ReqShipDate,
d.lnitmseq, d.locncode, d.UNITPRCE,
p.psitmval as [LIST PRICE], i.stndcost, d.qtybsuom, c.slprsnid, sp.slprsnfn,
sp.sprsnsln, c.salsterr, st.slterdsc, h.custnmbr, h.custname,
c.shrtname as 'CUSTOGROUP', c.custclas as 'CUSTOCLAS', c2.ZIP as 'CHANSEG',
h.sopnumbe, d.itemnmbr, d.itemdesc,
case i.itemtype
WHEN 1 THEN 'SALES INVENTORY'
WHEN 2 THEN 'DISCONTINUED'
WHEN 3 THEN 'KITS'
end as 'ITEMTYPE',
d.qtyfulfi, d.uofm, d.XTNDPRCE, u.equivuom 'BASEUOM', u.equomqty 'PACKING',
d.qtyfulfi*u.equomqty 'bqty',
isnull( (SELECT TOP 1 LISTPRICE FROM tbl_disco_historical_baseuom HH WHERE HH.ITEMNMBR = D.ITEMNMBR AND HH.BASEUOM = U2.BASEUOFM AND HH.LEFFECTDATE >= H.DOCDATE ORDER BY HH.LEFFECTDATE DESC),
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)) as 'BASE PRICE – LIST PRICE PER BUOM',
isnull(p.psitmval*d.qtyfulfi*u.equomqty,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM ), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
* d.qtyfulfi*u.equomqty
)
AS 'EXTND PRICE by BASE PRICE-BUOM',
p1.psitmval as 'MAKRO PRICE – LIST PRICE',
p1.psitmval*d.qtyfulfi as 'EXTND PRICE BY MAKRO',
d.qtyfulfi*u.equomqty AS 'QTYBASEUOM',
ISNULL(
(SELECT TOP 1 LISTPRICE FROM tbl_disco_historical_baseuom HH WHERE HH.ITEMNMBR = D.ITEMNMBR AND HH.BASEUOM = U2.BASEUOFM AND HH.LEFFECTDATE >= H.DOCDATE ORDER BY HH.LEFFECTDATE DESC),
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
))
as 'BaseUomListPrice',
(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)
as 'BaseUomTotalListPrice',
(d.qtyfulfi*u.equomqty) *
isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
) - D.XTNDPRCE AS 'TOTAL DISCOUNT',
a.IVBRAND as 'BRAND', b.IVSUBCAT as 'SubCat',
dbo.ACEfunc_CUSTOTYPE2(c.CUSTCLAS,c.SALSTERR) AS 'CUSTTYPE', D.MRKDNAMT*d.qtyfulfi AS 'MARKDOWN',
cast(i.uscatvls_6 as float) as 'ITEM REPORT SEQUENCE',
rtrim(ltrim(cast(uscatvls_1 as varchar(4)))) + '-' + bh.image_url as 'BRAND HANDLER',
I.ITMCLSCD 'ITEM CLASS', d.qtyfulfi*u.equomqty/uu.equomqty AS 'CASEQTY',
ii.inet1 as 'ABC CAT',
ISNULL(APL.PCPRICE,isnull(p.psitmval,
ISNULL((SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = U.EQUIVUOM), (SELECT psitmval from iv10402
WHERE prcShid = 'BASE PRICE'
AND itemnmbr = d.itemnmbr
AND UOFM = D.UOFM))
)) as 'APL(SAG) Piece Price', h.docid as 'SOP TYPE ID'
from sop10100 h
inner join sop10200 d on (h.sopnumbe = d.sopnumbe)
inner join IV00101 i on (d.itemnmbr = i.itemnmbr)
inner join RM00101 c on (h.custnmbr = c.custnmbr)
left join RM00102 c2 on (c.custnmbr = c2.custnmbr and c2.adrscode = 'MAIN')
left join RM00301 sp on (sp.slprsnid = c.slprsnid)
left join RM00303 st on (st.salsterr = c.salsterr)
LEFT JOIN iv40202 u on (i.uomschdl = u.uomschdl and d.uofm = u.uofm and u.qtybsuom <> 0)
LEFT JOIN iv40201 u2 on (i.uomschdl = u2.uomschdl)
left join iv10402 p1 on (d.itemnmbr = p1.itemnmbr and p1.PRCSHID = 'PLMAKRO'
and d.uofm = p1.uofm)
left join iv10402 p on (d.itemnmbr = p.itemnmbr and p.PRCSHID = 'BASE PRICE'
and U.EQUIVUOM = p.uofm)
left join #brand a on (i.uscatvls_2 = a.code)
left join #SubCat b on (i.uscatvls_4 = b.code)
left join iv40600 bh on (i.uscatvls_1 = bh.uscatval AND bh.uscatnum = 1)
left join iv40202 uu on (i.uomschdl = uu.uomschdl and i.selnguom = uu.uofm and uu.qtybsuom <> 0)
LEFT join sy01200 as ii on (ii.master_id = d.itemnmbr and ii.master_type = 'ITM')
LEFT JOIN T0_APL APL ON (D.ITEMNMBR=APL.ITEMNMBR AND APL.MNTH = @MNTH AND APL.YR = @YR)
where
h.soptype = 3 and d.CMPNTSEQ = 0 and h.voidstts = 0 and h.pstgstus = 2
and h.docdate >= @DTFROM and h.docdate <= @DTTO
--order by percentvar, h.custname, d.sopnumbe, d.itemnmbr
END
【问题讨论】:
嗨!我已经注释掉了订单。该错误未显示,但在执行时,查询似乎挂起。在没有 UNION ALL 的情况下分离 2 个选择语句后,查询工作正常。您能帮我如何使用 UNION ALL 组合两个选择语句吗?谢谢 这听起来像是一个不同的问题。如果这两个查询独立运行正常,并且它们返回相同的列,则 UNION ALL 应该可以工作。肯定是 Select 挂了?如果你独立运行它会发生什么? 独立运行时运行良好。您认为错误出在我使用的临时表上吗?谢谢。 谢谢你的帮助,我已经修好了。 【参考方案1】:您正在选择 h.sopnumbe,但按 d.sopnumbe 排序
尝试更改顺序以使用 h.sopnumbe
【讨论】:
嗨!我已经注释掉了订单。该错误未显示,但在执行时,查询似乎挂起。在没有 UNION ALL 的情况下分离 2 个选择语句后,查询工作正常。您能帮我如何使用 UNION ALL 组合两个选择语句吗?谢谢【参考方案2】:如果您在查询中使用ORDER BY
语句,则必须在查询的选择列表中提及。例如,我给了你链接。如果你通过它,你会想到在 SQL 中使用ORDER BY
子句。
http://msdn.microsoft.com/en-us/library/ms188385.aspx
【讨论】:
嗨!我已经注释掉了订单。该错误未显示,但在执行时,查询似乎挂起。在没有 UNION ALL 的情况下分离 2 个选择语句后,查询工作正常。您能帮我如何使用 UNION ALL 组合两个选择语句吗?谢谢 -1 不,你没有。例如,SELECT 1 FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME
工作正常。
@ta.speot.is c 我已经为您提供了学习 order by 子句的链接。在某些情况下,要考虑限制。对于您上面给出的简单查询,它可以正常工作.正确阅读网站并理解它。
该链接不支持您声称您必须在查询的选择列表中提及这一点。您不必必须这样做。链接中的措辞是 ORDER BY 子句中引用的列名必须对应于选择列表中的列或对应于 FROM 子句中指定的表中定义的列模棱两可。以上是关于如果语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中的主要内容,如果未能解决你的问题,请参考以下文章
SQL语句的MINUS,INTERSECT和UNION ALL
[讲解]sql except和intersect运算符(比拟两个或多个select语句的结果并前去非重复值)
LINQ体验——LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods