2012-12-11 58 views
1

我有一個SQL查詢是一個價格表,我通常運行18個不同的時間18個不同的客戶端。 我有一個@varchar作爲聲明,我改變了customerID。然後這導出一個txt平面文件文件,然後我可以轉換爲Excel並通過feebootimail發送。SQL例程導出單獨的txt文件

是否有一個例程,我可以在SQL 2005中運行,這樣我可以提供一個客戶列表並獲得18個不同的txt文件?例如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 

回答

-1

您可以用SQLCMD和一個批處理文件做到這一點。

做一個script.sql文件中包含的內容:

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

然後做一個批處理文件,它看起來像這樣:

@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 

複製併爲每個18粘貼一旦set/call線顧客。另一SQLCMD查詢的輸出,或只是一個文本文件 -

,而不是複製粘貼+,你可以,如果你有客戶的地方名單做一個for循環的東西。我把這些留給你弄清楚。

+0

呃,downvoter謹慎解釋? – Blorgbeard