Can you change the column order of a SQL statement being returned in Excel? -
we using stored procedure run sales tax report in excel. data pulls no problem, there easy way reorder columns? right excel pulls month, date, applied month... , need pull month, date, customer, contract...
i tried using "order by", doesn't change column order, how data sorted.
use [viewpoint];
go set ansi_nulls on; go set quoted_identifier on; go /** object: stored procedure dbo.brptarsalestax script date: 8/28/99 9:32:28 **/ --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 caller 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 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 , newbaserate info */ insert #baserate (taxgroup, taxcode, oldbaserate, newbaserate,effectivedate,description,glacct,localtaxcode) select b.taxgroup,b.taxcode, oldbaserate=sum(case when b.multilevel='y' x.oldrate else b.oldrate end), newbaserate=sum(case when b.multilevel='y' x.newrate else b.newrate end), effectivedate=(case when b.multilevel='y' x.effectivedate else b.effectivedate end), description=(case when b.multilevel='y' x.description else b.description end), x.glacct,localtaxcode = x.taxcode hqtx b (nolock) left join hqtl (nolock) on a.taxgroup=b.taxgroup , a.taxcode=b.taxcode left join hqtx x (nolock) on x.taxgroup=a.taxgroup , x.taxcode=a.taxlink group b.taxgroup, b.taxcode,b.multilevel,b.effectivedate, x.effectivedate, b.description,x.description,x.glacct,x.taxcode /* insert multilevel code info */ insert #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' #baserate.localtaxcode else #baserate.taxcode end, localtaxdesc=case base.multilevel when 'y' #baserate.description end, glacct=case base.multilevel when 'y' #baserate.glacct else base.glacct end, artl.taxbasis, artl.taxamount, /* taxrate=case base.multilevel when 'y' */ taxrate= case when arth.transdate < isnull(#baserate.effectivedate,'12/31/2070') (#baserate.oldbaserate) when arth.transdate >= isnull(#baserate.effectivedate,'12/31/2070') (#baserate.newbaserate) end, artl.discoffered, artl.taxdisc, artl.amount --begintaxcode=@begintaxcode, endtaxcode=@endtaxcode, beginmth=@beginmth, endmth=@endmth artl (nolock) inner join arth (nolock) on arth.arco=artl.arco , arth.mth=artl.mth , arth.artrans=artl.artrans inner join hqco (nolock) on artl.arco=hqco.hqco inner join hqtx base (nolock) on base.taxgroup=artl.taxgroup , base.taxcode=artl.taxcode inner join #baserate (nolock) on #baserate.taxgroup=artl.taxgroup , #baserate.taxcode=artl.taxcode /*full outer join hqtl on hqtl.taxgroup = artl.taxgroup , hqtl.taxcode = artl.taxcode full outer join hqtx local on local.taxgroup = hqtl.taxgroup , local.taxcode = hqtl.taxlink*/ artl.arco=@arco , artl.taxcode>=@begintaxcode , artl.taxcode<=@endtaxcode , artl.mth>=@beginmth , artl.mth<=@endmth , arth.artranstype not in ('p','m') /* insert total #multilevel */ insert #multilevel (arco,name,basetaxcode, localtaxcode,taxrate, artrans,mth, contract, taxlocalbasistotal,taxlocalamounttotal, taxlocaldiscofftotal, taxlocaltaxdisctotal) /*select distinct arco,name, basetaxcode, localtaxcode, taxrate, artrans,mth, taxbasis, taxamount #multilevel*/ select arco, name, basetaxcode, localtaxcode, taxrate, artrans, mth, contract, sum(taxbasis), sum(taxamount), sum(discoffered), sum(taxdisc) #multilevel group arco, name, basetaxcode, localtaxcode, mth, taxrate, artrans, contract insert #multilevel (arco, name, basetaxcode, artrans, mth, taxbasebasistotal, taxbaseamounttotal, taxbasediscofftotal, taxbasetaxdisctotal, totalamount) /*select distinct arco, name, basetaxcode, artrans, mth, taxbasis, taxamount #multilevel*/ select artl.arco, hqco.name, taxcode, artl.artrans, artl.mth, sum(taxbasis), sum(taxamount), sum(discoffered), sum(taxdisc), sum(amount) artl (nolock) join arth (nolock) on arth.arco=artl.arco , arth.mth=artl.mth , arth.artrans=artl.artrans join hqco (nolock) on hqco.hqco=artl.arco hqco.hqco=@arco , artl.arco=@arco , artl.taxcode between @begintaxcode , @endtaxcode , artl.mth between @beginmth , @endmth , arth.arco=@arco , arth.mth between @beginmth , @endmth , arth.artranstype not in ('p','m') group artl.arco, name, taxcode, artl.artrans, artl.mth insert #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 artl (nolock) inner join arth (nolock) on arth.arco=artl.arco , arth.mth=artl.mth , arth.artrans=artl.artrans inner join hqco (nolock) on hqco.hqco=artl.arco @notaxlines ='y' , artl.arco=@arco , artl.taxcode null , artl.mth between @beginmth , @endmth , arth.arco=@arco , arth.mth between @beginmth , @endmth /*and arth.artranstype not in ('p','m')*/ /* removed above pull negative/credit amounts*/ /* inserting final sales tax data multilevel auxiliary table */ insert #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 #multilevel arco = 1 , customer not null , invoice not null /* setting ansi_warning off in order prevent sql server throw exception excel causes excel not fetch data */ set ansi_warnings off; /* updating multilevel auxiliary table customer name */ update #multilevel_aux set customername = arcm.name arcm arcm.customer = #multilevel_aux.customer
/* updating multilevel auxiliary table contract name */ update #multilevel_aux set contractname = jccm.description jccm jccm.contract = #multilevel_aux.contract
/* selecting data excel report */ select mth, transdate, customer, customername, contract, contractname, invoice, description, basetaxcode "taxcode", taxbasis "gross", taxamount, amount "total", appliedmth, artrans, arline #multilevel_aux
go
i set simple stored procedure select statement, , ran inside excel. changed order of columns in select statement , ran again. did columns re-order, way delete original results in excel, , re-run fresh (empty) spreadsheet. try these steps:
- re-save stored procedure new select statement column order.
- delete old results spreadsheet (or go new sheet).
- check setup: data tab -> connections -> highlight connection -> properties -> definition tab -> command type should "sql" , command text should "exec [dbo].[udbrptarsalestax]"
- click ok.
- click in cell a1.
- data -> external data -> highlight connection -> "open" -> in import data window, view data table, existing worksheet, click ok.
the data should show in spreadsheet in order in listed in saved stored procedure udbrptarsalestax. able change around columns in stored procedure select statement, re-generate procedure, delete old data in spreadsheet, external data again empty sheet, , columns in new order.
Comments
Post a Comment