【问题标题】:Retrieve specific number of rows in a SQL Server query检索 SQL Server 查询中的特定行数
【发布时间】: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】:

    试试这个。

    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
    

    【讨论】:

    • 这似乎是正确的。如果我想要每次选择 100 个。我应该把 2 改成 100 吗?
    • 是的,您可以这样做,因为您必须更改条件。我正在更新我的答案以满足您的要求。
    • @Coder : 满足你的要求吗?
    【解决方案2】:

    我建议您之前准备结果集,识别对(两行),然后迭代此结果集并从变量创建 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
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 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 的更多详细信息

      希望对你有帮助

      【讨论】:

      • 我收到错误消息:消息 102,级别 15,状态 1,第 11 行“偏移”附近的语法不正确。消息 153,级别 15,状态 2,第 12 行 FETCH 语句中选项 NEXT 的使用无效。
      • 您使用的是哪个版本的 SQL Server?正如我在原始帖子中所写的,此代码适用于 SQL2012 及更高版本
      【解决方案4】:

      使用 LIMIT 从查询中获取正确的记录子集:

      SELECT * FROM Orders LIMIT 10, 2
      

      将从记录 11 开始的 Orders 中检索两条记录

      【讨论】:

      • 你是对的,TOP 也会这样做。我将 mysql 误读为 db。唯一的问题是您必须运行两次才能选择一个范围:首先选择前 12 个,然后选择该子集的前 2 个。
      • 感谢您的回答。但我不确定这是正确的做法。
      • 抱歉,SELECT TOP 2 * FROM (SELECT TOP 12 * FROM Orders) 有什么问题??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多