2013-03-26 58 views
0

我在Break up a SQL Server 2008 query into batches的幫助下成功導出到使用bcp的平面文件。將行分解爲'將SQL Server 2008查詢分解爲批'

現在我想爲每批添加一行(或兩行)。這是需要「抵消」總金額,因此每批將平衡爲零。例如,我在表中有2,501條記錄。 2500美元的金額爲100美元,全部預訂到賬戶#70000,總計$ 250,000。最後一條記錄#2,501被記錄到賬戶#80000,其價值爲($ 250,000);因此總表的餘額。

由於我們的新系統只允許批次最多950行,所以我必須使用上面的鏈接代碼來分割輸出。很好,順便說一句。但是由於每個批次必須將「餘額」設置爲零,因此我需要在每批中添加一行,並將其設置爲抵消賬戶(例如#80000)。

我希望對某人有意義! ; - ]關於何時/何時插入記錄的建議,並且可以在創建每個批次後還是之前插入它?

感謝

回答

1

貌似是創建每個批次後,你應該插入新記錄。而且,還有一個建議,您鏈接的帖子使用'insert into table ... select ...',它比'select ... into table'慢。而'選擇進入桌子'會自動爲你創建桌子。在每批bcp成功完成後,您可以刪除自動創建的表。你也可以有一個歷史表來記住你所做的事情。

0

所以,這裏是我想出了以下IJH的建議和代碼以前鏈接後我「解禁」:

-- Set up some variables 
declare 
@batchsize  int = 900, 
@bcpTargetDir varchar(10) = 'c:\tempFolder\', 
@csvQueryServer varchar(15) = 'SQLserverName', 
@rowcount  integer, 
@nowstring  varchar(25), 
@group   varchar(25), 
@batch_id  int, 
@startID  int, 
@endID   int, 
@oidCSV   varchar(max), 
@csvQuery  varchar(max), 
@bcpFilename varchar(25), 
@bcpQuery  varchar(1000) 

-- create the Batch Range temp table 
declare @tblBatchRanges table (
batch_id  integer NOT NULL IDENTITY(1,1) PRIMARY KEY, 
oid_start  integer NOT NULL, 
oid_end   integer NOT NULL, 
csvQuery  varchar(max) 
) 

-- Create a unique datestamp-based string, which will be used to name the exported files. 
select @nowstring = REPLACE(CONVERT(char(8),GETDATE(),1),'/','-') 

-- Set the value of @startid 
select top(1) @startID = jeDataID 
    from DBname.dbo.tableName 
    where groupReference = @group 
    order by jeDataID 

-- Set the value of @endid 
select top(@batchsize) @endID = jeDataID 
    from DBname.dbo.tableName 
    where groupReference = @group 
    order by jeDataID 

select @rowcount = @@ROWCOUNT 

-- create temp table to hold each batch -------------- 
CREATE TABLE ##jeDataTemp 
(
jeDataID int, 
docDate date, 
GLacct varchar(17), 
amount decimal(13, 2), 
groupReference varchar(25) 
) 

-- =================================================== 
-- Loop through the data with a WHILE loop 

WHILE (@rowcount > 0) begin 

-- since I'm using a temp table to hold the data, 
-- I need to clear it out each Loop through 
truncate table ##jeDataTemp 

-- insert the data into the temp table using the start and end ID values 
insert into ##jeDataTemp 
(jeDataID, docDate, GLacct, amount, groupReference) 

select jeDataID, docDate, GLacct, amount, groupReference 
from tableName 
where jeDataID between @startID and @endID 
order by jeDataID 

-- insert the General Ledger offset to a different GL acct # 
-- by getting the SUM of the [amount] field * -1 
insert into ##jeDataTemp 
(docDate, GLacct, amount, groupReference) 

Select max(docDate), 
     '8000000' as GLacct, 
     SUM(##jeDataTemp.amount)*-1 as amount, 
     @group 

from ##jeDataTemp 

-- create the select statement with the ID parameters for each file to be exported 
select @csvQuery = 'select * from ##jeDataTemp order by jeDataID' 

-- Log the info and get the batch ID. 
insert into @tblBatchRanges (oid_start, oid_end, csvQuery) 
    values (@startID, @endID, @csvQuery) 

select @batch_id = @@IDENTITY 

-- Advance @startid and @endid so that they point to the next batch 
-- and filter for the selected Group Reference 
select top(1) @startID = jeDataID 
    from tableName 
    where jeDataID > @endID 
    AND groupReference = @group 
    order by jeDataID 

select top(@batchsize) @endID = jeDataID 
    from tableName 
    where jeDataID > @endID 
    AND groupReference = @group 
    order by jeDataID 

-- set the row count variable value 
select @rowcount = @@ROWCOUNT 

-- Export the current batch to a file. --------------- 
-- Set the file name with the current Date and Batch ID # 
select @bcpFilename = 'JE_' + @nowstring + '-' + cast(@batch_id as varchar) + '.txt' 

select @bcpQuery = 'bcp "' + @csvQuery + '" QUERYOUT "' + 
      @bcpTargetDir + @bcpFilename + '" -S ' + @csvQueryServer + ' -T -c ' 
exec master..xp_cmdshell @bcpQuery 

-- end the WHILE loop 
END  

-- drop the temp table 
drop table ##jeDataTemp 

當然希望這可以幫助別人了前進的道路上! 肖恩