我们正在使用存储过程在Excel中运行销售税报表。数据没有问题,但有没有 easy 方法对列进行重新排序?现在excel拉月,日期,应用月......我们需要它来拉月,日,客户,合同......
我尝试使用" ORDER BY",但这实际上并没有改变列顺序,只是如何对数据进行排序。
USE [Viewpoint];
GO SET ANSI_NULLS ON; 走 SET QUOTED_IDENTIFIER ON; 走 / ** 对象:存储过程dbo.brptARSalesTax脚本日期:8/28/99 9:32:28 AM ** / --Drop proc brptARSalesTax ALTER PROCEDURE [dbo]。[udbrptARSalesTax] @ARCo [dbo]。[bCompany] = 3,@ BeginTaxCode [dbo]。[bTaxCode] ='',@ EndTaxCode [dbo]。[bTaxCode] =' zzzzzzzzz', @BeginMth [dbo]。[bMonth] =' 01/01 / 1950',@ EndMth [dbo]。[bMonth] =' 12/1 / 2049',@ NoTaxLines [dbo ] [bYN] =' N' 与EXEC作为调用者 AS
create table #Multilevel
(Name varchar (60) NULL,
ARCo tinyint NULL,
ARTrans int NULL,
ARLine int NULL,
Mth smalldatetime NULL,
TransDate Smalldatetime NULL,
AppliedMth Smalldatetime NULL,
Customer int NULL,
CustomerName varchar(30) NULL,
Invoice varchar(10) NULL,
CheckNo varchar(10) NULL,
Description varchar (30) NULL,
BaseTaxCode varchar (10) NULL,
BaseTaxDesc varchar (30) NULL,
MultiLevel varchar (10) NULL,
LocalTaxCode varchar (10) NULL,
Contract varchar (10) NULL,
ContractName varchar (60) NULL,
LocalTaxDesc varchar (30) NULL,
GLAcct varchar (20) NULL,
TaxBasis decimal (12,2) NULL,
TaxAmount decimal (12,2) NULL,
TaxRate decimal (8,6) NULL,
TaxLocalBasisTotal decimal (12,2) NULL,
TaxLocalAmountTotal decimal (12,2) NULL,
TaxBaseAmountTotal decimal(12,2) NULL,
TaxBaseBasisTotal decimal (12,2) NULL,
DiscOffered decimal (12,2) NULL,
TaxDisc decimal (12,2) NULL,
TaxLocalDiscOffTotal decimal (12,2) NULL,
TaxLocalTaxDiscTotal decimal (12,2) NULL,
TaxBaseDiscOffTotal decimal (12,2) NULL,
TaxBaseTaxDiscTotal decimal (12,2) NULL,
Amount decimal (12,2) NULL,
TotalAmount decimal (12,2) NULL)
/* Creating Multilevel auxiliary table in order to select Customer Name */
create table #Multilevel_Aux
(Name varchar (60) NULL,
ARCo tinyint NULL,
ARTrans int NULL,
ARLine int NULL,
Mth smalldatetime NULL,
TransDate Smalldatetime NULL,
AppliedMth Smalldatetime NULL,
Customer int NULL,
CustomerName varchar(30) NULL,
Invoice varchar(10) NULL,
CheckNo varchar(10) NULL,
Description varchar (30) NULL,
BaseTaxCode varchar (10) NULL,
BaseTaxDesc varchar (30) NULL,
MultiLevel varchar (10) NULL,
LocalTaxCode varchar (10) NULL,
Contract varchar (10) NULL,
ContractName varchar (60) NULL,
LocalTaxDesc varchar (30) NULL,
GLAcct varchar (20) NULL,
TaxBasis decimal (12,2) NULL,
TaxAmount decimal (12,2) NULL,
TaxRate decimal (8,6) NULL,
TaxLocalBasisTotal decimal (12,2) NULL,
TaxLocalAmountTotal decimal (12,2) NULL,
TaxBaseAmountTotal decimal(12,2) NULL,
TaxBaseBasisTotal decimal (12,2) NULL,
DiscOffered decimal (12,2) NULL,
TaxDisc decimal (12,2) NULL,
TaxLocalDiscOffTotal decimal (12,2) NULL,
TaxLocalTaxDiscTotal decimal (12,2) NULL,
TaxBaseDiscOffTotal decimal (12,2) NULL,
TaxBaseTaxDiscTotal decimal (12,2) NULL,
Amount decimal (12,2) NULL,
TotalAmount decimal (12,2) NULL)
create table #BaseRate
(TaxGroup tinyint NULL,
TaxCode varchar (10) NULL,
OldBaseRate Decimal(8,6) NULL,
NewBaseRate Decimal(8,6) NULL,
EffectiveDate smalldatetime NULL,
Description varchar (30) NULL,
GLAcct varchar(20) NULL,
LocalTaxCode varchar (10) NULL)
/* insert OldBaseRate and NewBaseRate Info */
insert into #BaseRate
(TaxGroup, TaxCode, OldBaseRate, NewBaseRate,EffectiveDate,Description,GLAcct,LocalTaxCode)
SELECT b.TaxGroup,b.TaxCode,
OldBaseRate=sum(case when b.MultiLevel='Y' then x.OldRate else b.OldRate end),
NewBaseRate=sum(case when b.MultiLevel='Y' then x.NewRate else b.NewRate end),
EffectiveDate=(case when b.MultiLevel='Y' then x.EffectiveDate else b.EffectiveDate end),
Description=(case when b.MultiLevel='Y' then x.Description else b.Description end),
x.GLAcct,LocalTaxCode = x.TaxCode
FROM HQTX b with (nolock)
Left Join HQTL a with (nolock) on a.TaxGroup=b.TaxGroup and a.TaxCode=b.TaxCode
Left Join HQTX x with (nolock) on x.TaxGroup=a.TaxGroup and x.TaxCode=a.TaxLink
GROUP BY
b.TaxGroup, b.TaxCode,b.MultiLevel,b.EffectiveDate, x.EffectiveDate, b.Description,x.Description,x.GLAcct,x.TaxCode
/* insert Multilevel Code Info */
insert into #Multilevel
(Name, ARCo, ARTrans, ARLine, Mth,TransDate, AppliedMth, Customer, Invoice, CheckNo, Description, Contract, BaseTaxCode, BaseTaxDesc,
MultiLevel, LocalTaxCode, LocalTaxDesc, GLAcct, TaxBasis, TaxAmount,TaxRate,DiscOffered,TaxDisc,Amount)
SELECT HQCO.Name,ARTL.ARCo, ARTL.ARTrans, ARTL.ARLine, ARTL.Mth,
ARTH.TransDate, ARTH.AppliedMth, ARTH.Customer,
ARTH.Invoice, ARTH.CheckNo, ARTH.Description, ARTH.Contract,
BaseTaxCode=base.TaxCode,
BaseTaxDesc=base.Description,
MultiLevel=base.MultiLevel,
LocalTaxCode=case base.MultiLevel when 'Y' then #BaseRate.LocalTaxCode else #BaseRate.TaxCode end,
LocalTaxDesc=case base.MultiLevel when 'Y' then #BaseRate.Description end,
GLAcct=case base.MultiLevel when 'Y' then #BaseRate.GLAcct else base.GLAcct end,
ARTL.TaxBasis,
ARTL.TaxAmount,
/*
TaxRate=case base.MultiLevel when 'Y'
then
*/
TaxRate= case when ARTH.TransDate < isnull(#BaseRate.EffectiveDate,'12/31/2070') then (#BaseRate.OldBaseRate)
when ARTH.TransDate >= isnull(#BaseRate.EffectiveDate,'12/31/2070') then (#BaseRate.NewBaseRate)
end,
ARTL.DiscOffered,
ARTL.TaxDisc, ARTL.Amount
--BeginTaxCode=@BeginTaxCode, EndTaxCode=@EndTaxCode, BeginMth=@BeginMth, EndMth=@EndMth
FROM ARTL with (nolock)
Inner Join ARTH with (nolock) on ARTH.ARCo=ARTL.ARCo and ARTH.Mth=ARTL.Mth and ARTH.ARTrans=ARTL.ARTrans
Inner Join HQCO with (nolock) on ARTL.ARCo=HQCO.HQCo
Inner Join HQTX base with (nolock) on base.TaxGroup=ARTL.TaxGroup and base.TaxCode=ARTL.TaxCode
Inner Join #BaseRate with (nolock) on #BaseRate.TaxGroup=ARTL.TaxGroup and #BaseRate.TaxCode=ARTL.TaxCode
/*Full outer Join HQTL on HQTL.TaxGroup = ARTL.TaxGroup and HQTL.TaxCode = ARTL.TaxCode
Full outer Join HQTX local on local.TaxGroup = HQTL.TaxGroup and local.TaxCode = HQTL.TaxLink*/
WHERE ARTL.ARCo=@ARCo and ARTL.TaxCode>=@BeginTaxCode and ARTL.TaxCode<=@EndTaxCode
and ARTL.Mth>=@BeginMth and ARTL.Mth<=@EndMth and ARTH.ARTransType Not In ('P','M')
/* insert Total into #Multilevel */
insert into #Multilevel
(ARCo,Name,BaseTaxCode, LocalTaxCode,TaxRate, ARTrans,Mth, Contract, TaxLocalBasisTotal,TaxLocalAmountTotal,
TaxLocalDiscOffTotal, TaxLocalTaxDiscTotal)
/*SELECT DISTINCT ARCo,Name, BaseTaxCode, LocalTaxCode, TaxRate, ARTrans,Mth, TaxBasis, TaxAmount
FROM #Multilevel*/
Select ARCo, Name, BaseTaxCode, LocalTaxCode, TaxRate, ARTrans, Mth, Contract, sum(TaxBasis), sum(TaxAmount),
sum(DiscOffered), sum(TaxDisc)
From #Multilevel Group By ARCo, Name, BaseTaxCode, LocalTaxCode, Mth, TaxRate, ARTrans, Contract
insert into #Multilevel
(ARCo, Name, BaseTaxCode, ARTrans, Mth, TaxBaseBasisTotal, TaxBaseAmountTotal,
TaxBaseDiscOffTotal, TaxBaseTaxDiscTotal, TotalAmount)
/*Select Distinct ARCo, Name, BaseTaxCode, ARTrans, Mth, TaxBasis, TaxAmount
From #Multilevel*/
Select ARTL.ARCo, HQCO.Name, TaxCode, ARTL.ARTrans, ARTL.Mth, sum(TaxBasis), sum(TaxAmount),
sum(DiscOffered), sum(TaxDisc), sum(Amount)
From ARTL with (nolock)
Join ARTH with (nolock) on ARTH.ARCo=ARTL.ARCo and ARTH.Mth=ARTL.Mth and ARTH.ARTrans=ARTL.ARTrans
Join HQCO with (nolock) on HQCO.HQCo=ARTL.ARCo
WHERE HQCO.HQCo=@ARCo
and ARTL.ARCo=@ARCo and ARTL.TaxCode between @BeginTaxCode and @EndTaxCode
and ARTL.Mth between @BeginMth and @EndMth
and ARTH.ARCo=@ARCo and ARTH.Mth between @BeginMth and @EndMth
and ARTH.ARTransType Not in ('P','M')
Group By ARTL.ARCo, Name, TaxCode, ARTL.ARTrans, ARTL.Mth
insert into #Multilevel
(ARCo, ARTrans, ARLine, Mth,TransDate, AppliedMth, Customer, Invoice, CheckNo,
Description, Contract, GLAcct,DiscOffered,TotalAmount,BaseTaxCode, Name)
Select ARTL.ARCo, ARTL.ARTrans, ARTL.ARLine, ARTL.Mth,ARTH.TransDate, ARTH.AppliedMth, ARTH.Customer, ARTH.Invoice, ARTH.CheckNo,
ARTL.Description, ARTH.Contract, ARTL.GLAcct,ARTL.DiscOffered,ARTL.Amount,ARTL.TaxCode, HQCO.Name
from ARTL with (nolock)
Inner Join ARTH with (nolock) on ARTH.ARCo=ARTL.ARCo and ARTH.Mth=ARTL.Mth and ARTH.ARTrans=ARTL.ARTrans
Inner Join HQCO with (nolock) on HQCO.HQCo=ARTL.ARCo
where @NoTaxLines ='Y' and ARTL.ARCo=@ARCo and ARTL.TaxCode is null
and ARTL.Mth between @BeginMth and @EndMth
and ARTH.ARCo=@ARCo and ARTH.Mth between @BeginMth and @EndMth
/*and ARTH.ARTransType Not in ('P','M')*/
/* REMOVED THE ABOVE SO IT WOULD PULL NEGATIVE/CREDIT AMOUNTS*/
/* Inserting final sales tax data into Multilevel auxiliary table */
insert into #Multilevel_Aux
(Mth, TransDate, AppliedMth, ARTrans, ARLine, Customer, Invoice, Contract, Description, BaseTaxCode, Amount, TaxBasis, TaxAmount)
Select Distinct Mth, TransDate, AppliedMth, ARTrans, ARLine, Customer, Invoice, Contract, Description, BaseTaxCode, Amount, TaxBasis, TaxAmount
from #Multilevel
where ARCo = 1
and Customer is not null and Invoice is not null
/* Setting ANSI_WARNING to OFF in order to prevent SQL Server to throw an exception to Excel which causes Excel not to fetch the data successfully */
SET ANSI_WARNINGS OFF;
/* Updating Multilevel auxiliary table with customer name */
update #Multilevel_Aux
set CustomerName = ARCM.Name
from ARCM
where ARCM.Customer = #Multilevel_Aux.Customer
/ *使用合约名称更新多级辅助表* / 更新#Multilevel_Aux 设置ContractName = JCCM.Description 来自JCCM 其中JCCM.Contract =#Multilevel_Aux.Contract
/* Selecting data for the Excel report */
select Mth, TransDate, Customer, CustomerName, Contract, ContractName, Invoice, Description, BaseTaxCode "TaxCode", TaxBasis "Gross", TaxAmount, Amount "Total", AppliedMth, ARTrans, ARLine
from #Multilevel_Aux
GO
答案 0 :(得分:0)
我使用SELECT语句设置了一个简单的存储过程,并在Excel中运行它。然后我在SELECT语句中更改了列的顺序并再次运行它。我确实让列重新排序,但我能做到的唯一方法是删除Excel中的原始结果,然后使用新的(空)电子表格重新运行。请尝试以下步骤:
然后,数据应按照您在保存的存储过程udbrptARSalesTax中列出的顺序显示在电子表格中。我能够更改存储过程SELECT语句中的列,重新生成过程,删除电子表格中的旧数据,然后从空表中再次获取外部数据,并且列按新顺序排列。 / p>