添加行'将SQL Server 2008查询拆分为批次'

时间:2013-03-26 22:06:00

标签: sql export batch-processing bcp

我在Break up a SQL Server 2008 query into batches的帮助下使用bcp成功导出到平面文件。

现在我想为每个批次添加一行(或两行)。这需要“抵消”总金额,因此每个批次将平衡为零。例如,我在表中有2,501条记录。 2,500金额为100美元,全部预订到#70000帐户,总计250,000美元。最后一条记录#2,501被预订到帐号#80000,其价值为($ 250,000);因此总表余额。

由于我们的新系统只允许最多950行的批次,我必须使用上面的链接代码拆分输出。工作得很好,BTW。但由于每个批次必须“平衡”为零,我需要在每个批次中添加一行,并将其设置为偏移帐户(例如#80000)。

我希望这对某人有意义! ; - ]关于何时/何时插入记录的建议,我可以在创建每个批次之后或之前插入它吗?

谢谢

2 个答案:

答案 0 :(得分:1)

看起来您应该在创建每个批次后插入新记录。还有一个建议,你链接的帖子使用'insert into table ... select ...',它比'select ... into table'慢。 'select into table'将自动为您创建表格。每次批处理bcp成功后,您可以删除自动创建的表。您还可以使用历史记录表来记住您所做的事情。

答案 1 :(得分: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

当然希望这可以帮助其他人一路走来! 肖恩