Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将行添加到“将SQL Server 2008查询分解为批”_Sql_Export_Batch Processing_Bcp - Fatal编程技术网

将行添加到“将SQL Server 2008查询分解为批”

将行添加到“将SQL Server 2008查询分解为批”,sql,export,batch-processing,bcp,Sql,Export,Batch Processing,Bcp,在的帮助下,我成功地使用bcp导出到平面文件 现在,我想为每个批次添加一到两行。这需要抵消$amount总额,以便每个批次的余额为零。例如,我在一个表中有2501条记录。2500美元的金额为100美元,全部记入账户70000,总计250000美元。最后一条记录2501记入账户80000,价值250000美元;因此,表中的总余额 因为我们的新系统只允许最多950行的批处理,所以我必须使用上面链接的代码分割输出。顺便说一句,效果很好。但是由于每个批次都必须平衡到零,因此我需要在每个批次中添加一行,并

在的帮助下,我成功地使用bcp导出到平面文件

现在,我想为每个批次添加一到两行。这需要抵消$amount总额,以便每个批次的余额为零。例如,我在一个表中有2501条记录。2500美元的金额为100美元,全部记入账户70000,总计250000美元。最后一条记录2501记入账户80000,价值250000美元;因此,表中的总余额

因为我们的新系统只允许最多950行的批处理,所以我必须使用上面链接的代码分割输出。顺便说一句,效果很好。但是由于每个批次都必须平衡到零,因此我需要在每个批次中添加一行,并将其设置为抵销科目80000,例如

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


谢谢

看起来您应该在创建每个批次后插入新记录。还有一个建议,你链接的帖子使用的是“插入表格…选择…”,比“选择…”慢。。。“进桌子”。“选择进入表格”将自动为您创建表格。在每个批次bcp输出成功后,您可以删除自动创建的表。您还可以使用历史记录表来记住您所做的事情

因此,以下是我从之前链接的帖子中“摘取”的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
当然希望这能帮助其他人走出困境! 肖恩