openrowset for excel:我们可以跳过几行吗?

发布于 2024-10-16 12:04:44 字数 239 浏览 2 评论 0原文

我将使用以下sql从excel读取数据,但有时我需要跳过前几行。例如,真实数据从第 5 行开始,所以我需要跳过前 4 行,这可行吗?

 SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0;HDR=YES;Database=c:\daniel\test.xls',
    'SELECT * FROM [sheet1$]');

I will use the following sql to read data from excel, but sometimes I need to skip first several rows. e.g the real data begins from line 5, so I need to skip the first 4 rows, is that doable?

 SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0;HDR=YES;Database=c:\daniel\test.xls',
    'SELECT * FROM [sheet1$]');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-10-23 12:04:44

使用范围 [sheet1$A5:Z] 而不是整个工作表 [sheet1$]

SELECT *
FROM OPENROWSET(
    'Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0;HDR=YES;Database=c:\daniel\test.xls',
    'SELECT * FROM [sheet1$A5:Z]'
);

Use a range [sheet1$A5:Z] instead of the entire sheet [sheet1$]

SELECT *
FROM OPENROWSET(
    'Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0;HDR=YES;Database=c:\daniel\test.xls',
    'SELECT * FROM [sheet1$A5:Z]'
);
冷情 2024-10-23 12:04:44

这将对获取的行进行编号,没有特定的顺序(幸运的是):

SELECT *
FROM (
  SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS rownum
  FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0;HDR=YES;Database=c:\daniel\test.xls',
    'SELECT * FROM [sheet1$]')
) s
WHERE rownum > 4;

如果您认为合适,您可能希望通过更改 rownum 定义来指定某种顺序,如下所示:

ROW_NUMBER() OVER (ORDER BY specific_column_list) AS rownum

This will number the rows being obtained, with no specific order (as luck would have it):

SELECT *
FROM (
  SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS rownum
  FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0;HDR=YES;Database=c:\daniel\test.xls',
    'SELECT * FROM [sheet1$]')
) s
WHERE rownum > 4;

You may want to specify some order, if you see fit, by changing the rownum definition like this:

ROW_NUMBER() OVER (ORDER BY specific_column_list) AS rownum
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文