だから、これが私が以前にリンクされた投稿から「持ち上げた」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
これが途中で他の誰かを助けることを願っています!ショーン