1

我有一个 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
4

1 回答 1

-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行一次。

for如果您在某处有客户列表 - 另一个 SQLCMD 查询的输出,或者只是一个文本文件,那么您可以使用循环来代替复制+粘贴。我把它留给你弄清楚。

于 2012-12-12T01:14:49.390 回答