0

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

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

由于我们的新系统只允许最多 950 行的批处理,因此我必须使用上述链接的代码拆分输出。效果很好,顺便说一句。但是由于每个批次都必须“平衡”为零,我需要在每个批次中添加一行,并将其设置为抵消帐户(例如#80000)。

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

谢谢

4

2 回答 2

1

看起来您应该在创建每个批次后插入新记录。而且,还有一个建议,您链接的帖子使用“插入表格...选择...”,它比“选择...进入表格”要慢。'select into table' 会自动为你创建表格。每次bcp out成功后,就可以drop自动创建的表了。您还可以使用历史记录表来记住您所做的事情。

于 2013-03-26T22:30:38.170 回答
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

当然希望这可以帮助其他人!肖恩

于 2013-03-28T21:31:37.943 回答