sql例程导出单独的txt文件

时间:2012-12-11 23:58:23

标签: sql flat-file routines

我有一个sql查询,这是一个价格表,我通常为18个不同的客户端运行18次。 我有@varchar作为声明,我更改了customerID。然后导出一个txt平面文件,然后我可以转换为Excel并通过feebootimail发送。

我是否可以在sql 2005中运行例程,以便我可以将客户列表输入并获取18个不同的txt文件?示例Pricelist_8343.txt,Pricelist_8363.txt等


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 个答案:

答案 0 :(得分:-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循环代替复制+粘贴。我把它留给你弄清楚。