1

I have an sql query which is a pricelist that I normally run 18 different times for 18 different clients. I have a @varchar as as declaration and I change the customerID. This then exports a txt flat-file file so then I can convert to Excel and send via feebootimail.

Is there a routine that I can run in sql 2005 so I can feed a list of customers into and get 18 different txt files? example Pricelist_8343.txt, Pricelist_8363.txt ect


DECLARE @CustNum varchar(20)
SELECT @CustNum = '7509'

SELECT
it.ITEMID 'item id'
,it.ItemGroupID
,it.ITEMNAME
,Convert(Decimal(9,0),itm.QUANTITY) 'Sales Multiple'
,Convert(Decimal(9,0),it.TAXPACKAGINGQTY) 'Price Break Qty'
,Convert(Decimal(9,0),it.OUTERQUANTITY) 'Carton Qty'
,Convert(Decimal(9,2),itm.PRICE) 'Part Pack'
,Convert(Decimal(9,2),itm.PRICE2) 'Full Pack'
,Convert(Decimal(9,2),round( CASE 
WHEN pdt.AMOUNT >0 then pdt.amount
else CASE
 when pdt.discpct is null then CASE
    WHEN pdt.PRICELEVELTOUSE = 0 then Price
    WHEN pdt.PRICELEVELTOUSE = 1 then Price2
    WHEN pdt.PRICELEVELTOUSE = 2 then Price3
    WHEN pdt.PRICELEVELTOUSE = 3 then Price4
    WHEN pdt.PRICELEVELTOUSE = 4 then Price5 
    WHEN pdt.PRICELEVELTOUSE = 5 then Price6 END
 when pdt.discpct = 0 then CASE
    WHEN pdt.PRICELEVELTOUSE = 0 then Price
    WHEN pdt.PRICELEVELTOUSE = 1 then Price2
    WHEN pdt.PRICELEVELTOUSE = 2 then Price3
    WHEN pdt.PRICELEVELTOUSE = 3 then Price4
    WHEN pdt.PRICELEVELTOUSE = 4 then Price5 
    WHEN pdt.PRICELEVELTOUSE = 5 then Price6 END
 when pdt.discpct > 0 then CASE
    WHEN pdt.PRICELEVELTOUSE = 0 then round(itm.price - (itm.price*pdt.discpct/100),2)
    WHEN pdt.PRICELEVELTOUSE = 1 then round(itm.price2 - (itm.price2*pdt.discpct/100),2)
    WHEN pdt.PRICELEVELTOUSE = 2 then round(itm.price3 - (itm.price3*pdt.discpct/100),2)
    WHEN pdt.PRICELEVELTOUSE = 3 then round(itm.price4 - (itm.price4*pdt.discpct/100),2)
    WHEN pdt.PRICELEVELTOUSE = 4 then round(itm.price5 - (itm.price5*pdt.discpct/100),2)
    WHEN pdt.PRICELEVELTOUSE = 5 then round(itm.price6 - (itm.price6*pdt.discpct/100),2) END END END ,2)) as 'TAPrice'
,upper(itm.unitid) 'UOM'
,     Case
            When itm.PRICECHANGESTATUS=3 then 'Increase'
            When itm.PRICECHANGESTATUS=2 then 'Decrease'
            When itm.PRICECHANGESTATUS=1 then 'New Item'
            When itm.PRICECHANGESTATUS=0 then '-'
      END 'Price Indicator'
from  INVENTTABLE it
join  INVENTTABLEMODULE itm   on it.ItemId = itm.ItemID and itm.ModuleType = 2
join  CUSTTABLE cust on LTRIM(cust.AccountNum) = @CustNum
left outer join   PRICEDISCTABLE pdt on (ltrim(pdt.accountrelation) = 
    case
    when pdt.accountcode = 0 then ltrim(cust.accountnum) 
    when pdt.accountcode = 1 then ltrim(cust.pricegroup) end)and
      (ltrim(pdt.ItemRelation)+ltrim(pdt.UnitID) = case
        when pdt.itemrelation = it.itemid then ltrim(it.ItemID)+ltrim(itm.Unitid)
        when pdt.itemrelation = it.itempricegroup then  ltrim(it.ItemPriceGroup)+ltrim(itm.Unitid)end
        ) and pdt.fromdate <= getdate() and (getdate()<= pdt.todate or pdt.todate = ' ')
join  PriceLevelListReportLine sorter on it.ItemGroupID  = sorter.ItemGroupID
and (it.ItemType = (case when sorter.linetype = 0 then '0'
      when sorter.linetype = 1 then '0'
      when sorter.linetype = 2 then '1'end)

or it.ItemType = (case when sorter.linetype = 1 then '1' end))
WHERE it.PRICELISTFlag=1
ORDER BY sorter.SortOrder, it.ItemID
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Makdaddy
  • 39
  • 1
  • 1
  • 6

1 Answers1

-1

You could do this with SQLCMD and a batch file.

Make a script.sql file with contents like:

declare @CustNum varchar(20) = '$(custnum)'
select
   -- your giant query here

Then make a batch file that looks like this:

@echo off

set custnum=1234
call :export

set custnum=5678
call :export

set custnum=9012
call :export

goto :eof
:export
sqlcmd -E  -i script.sql -o %custnum%.txt -v CustNum=%custnum%
goto :eof

Copy and paste the set/call lines once for each of your 18 customers.

Instead of copy+paste, you could do something with a for loop if you have a list of the customers somewhere - output of another SQLCMD query, or just a text file. I leave that to you to figure out.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272