【发布时间】:2015-02-12 11:38:04
【问题描述】:
行:
1 Test1
2 Test2
3 Test3
12 Test4
5 Test1
6 Test2
7 Test1
8 Test2
我正在尝试从 ssis 包中导出 xml 数据文件。
一个例子:我需要为每个文件创建 2 行。如果该查询在 SSIS 循环容器中,什么 sql 命令可以确保每次返回正确的 2 行。
【问题讨论】:
标签: c# sql sql-server-2008 ssis
行:
1 Test1
2 Test2
3 Test3
12 Test4
5 Test1
6 Test2
7 Test1
8 Test2
我正在尝试从 ssis 包中导出 xml 数据文件。
一个例子:我需要为每个文件创建 2 行。如果该查询在 SSIS 循环容器中,什么 sql 命令可以确保每次返回正确的 2 行。
【问题讨论】:
标签: c# sql sql-server-2008 ssis
试试这个。
Create table Temp (id smallint, Col1 varchar(25))
insert into Temp select 1, 'Test1'
insert into Temp select 2, 'Test2'
insert into Temp select 3, 'Test3'
insert into Temp select 12, 'Test4'
insert into Temp select 5, 'Test1'
insert into Temp select 6, 'Test2'
insert into Temp select 7, 'Test1'
insert into Temp select 8, 'Test2'
insert into Temp select 10, 'Test1'
declare @i int = (select Max(RowNum) from (select ROW_NUMBER() over(order by id) as RowNum,id,Col1 from Temp) as temp)
declare @countnum int = 1
declare @NoOfRows int = 100
while(@countnum <= @i)
begin
select * from (select ROW_NUMBER() over(order by (select 0)) as RowNum,id,Col1 from Temp) as temp where RowNum >= @countnum and RowNum < @countnum + @NoOfRows
set @countnum = @countnum + @NoOfRows
end
drop table Temp
【讨论】:
我建议您之前准备结果集,识别对(两行),然后迭代此结果集并从变量创建 XML 输出。我不知道您要准备的 XML 的复杂性,但至少您可以在下面找到的查询可以帮助您准备对标识符,如果您需要获取更复杂的数据,可以使用它。
根据您的数据:
0) 准备变量:data - object, id int, name varchar(20)
1) 将您的结果集作为对象返回 - 下面的查询准备具有相同 id 的对(row_group 列)
2) 用 Foreach Ado 网络枚举器迭代这个对象,将数据插入到变量中
3) 在 Foreach 内部根据 row_group 准备具有动态文件名的数据流任务
with
data
as
(
select *, row_number() over(order by id) as row
from test_data
)
,
pairs
as
(
select * ,
case when row%2=0 then row-1 else row end as row_group
from data
)
select id, name, row_group
from pairs
order by row_group
【讨论】:
如果您使用的是 SQL Server 2012 或更高版本,则可以使用 T-SQL 中的 SQL 分页增强功能
请检查以下查询是否有帮助 注意@n参数应该被认为是循环数
/*
create table tbl (id smallint, txt varchar(25))
insert into tbl select 1, 'Test1'
insert into tbl select 2, 'Test2'
insert into tbl select 3, 'Test3'
insert into tbl select 12, 'Test4'
insert into tbl select 5, 'Test1'
insert into tbl select 6, 'Test2'
insert into tbl select 7, 'Test1'
insert into tbl select 8, 'Test2'
insert into tbl select 9, 'Test1'
delete tbl where id = 9
*/
declare @rowsperpage int = 2
declare @x int = @rowsperpage
declare @n int = 1
while @x = @rowsperpage
begin
SELECT *
FROM tbl
ORDER BY id
OFFSET (@n-1)*@rowsperpage ROWS
FETCH NEXT @rowsperpage ROWS ONLY
select @x = @@ROWCOUNT
set @n = @n + 1
end
您可以在参考教程中找到有关SQL paging using offset and fetch next 的更多详细信息
希望对你有帮助
【讨论】:
使用 LIMIT 从查询中获取正确的记录子集:
SELECT * FROM Orders LIMIT 10, 2
将从记录 11 开始的 Orders 中检索两条记录
【讨论】: