通过 SQL 2008 R2 SSMS 64 位查询 csv 或 excel 文件的最佳方法?
大家好,我有一些 SQL 2008 证书,但绝不是大师。我很好奇,如果我的目标是在 SSMS 中查询桌面上的 CSV 或 Excel 文件,该怎么做?在有人提到 Openrowset 并通过 sp_configure 打开它之前,我已经尝试过,但在尝试“Microsoft.Jet.OLEDB.4.0”驱动程序、“MSADSAL”驱动程序或其其他迭代时,它似乎不起作用。它给出了一些关于服务器不允许为空或只能在单线程模式下运行的错误。我读过这篇文章,很多人声称你必须运行 32 位版本或类似版本才能获得它。让我困惑的是,我可以将“BULK”方法与 Openrowset 一起使用,然后选择“Single_Clob”,但是我得到一个逗号分隔的字符串,然后我必须解析该字符串。
我真正的问题是,是否有一种简单的方法可以在 64 位版本的 SSMS 2008 R2 中快速查询 CSV 或 Excel 文件?
Hello everybody I have some certs in SQL 2008 but am by no means a master. I was curious if my goal was to query a CSV or Excel file on my desktop within SSMS how to do this? BEFORE anyone mentions Openrowset and turning it on through sp_configure I have tried that and it seems to not working when trying 'Microsoft.Jet.OLEDB.4.0' driver, the 'MSADSAL' drivers or the other iterations of it. It gives some error about the server not being allowed to be null OR else that this is only able to run in Single threaded mode. I read up on this and a lot of people claim that you have to run a 32 bit version or similar to get it. What confuses me is I can use the 'BULK' method with Openrowset and then select the 'Single_Clob' however I get a comma seperated string which I would then have to parse out.
What my real question is, is there a simple way to just query a CSV or Excel file fast in a 64 bit version of SSMS 2008 R2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Jet 驱动程序仅是 32 位模式。请参阅此 MSDN 博客。您可以使用 SQL CLR 吗?
我们通常使用 SSIS 包将 CSV/XLS 数据导入到表中进行查询 - 类似于右键单击并选择导入时 SSMS 会自动为您执行的操作。
您遇到的问题是 Excel 和 CSV 不是数据库。要查询它们,您必须将它们放入 DBMS 中。
Jet driver is only 32-bit mode. See this MSDN blog. Can you use SQL CLR?
We typically use SSIS packages to import CSV/XLS data into a table for querying - similar to what SSMS would do for you automatically if you right-click and select import.
The issue you are running into is that Excel and CSV are not databases. To query them, you must get them into a DBMS.
想通了:
方法 A:bcp:
确保 xp_cmdshell 已打开。 (如果您处于企业公司的生产环境中,出于安全原因,这可能不是一个选择。您可以始终使用BETA或QA环境将数据放在第一位,并希望设置链接服务器)。
执行 sp_configure 'xp_cmdshell', 1
重新配置
创建 csv 文件以进行测试。我制作了一个三列 csv 平面文件,如下所示:
A,Brett,1
B,约翰,2
C,Brian,3
创建与此处显示的数据类型匹配的临时表或永久表:
create table Test ( value varchar(3), Name varchar(16), ID int)
从命令 shell 运行 bcp,确保没有打开该文件。
EXEC xp_cmdshell 'bcp Test..Test in "C:\test\Test.txt" -c -t , -r \n -T'
GO
对 bcp 开关感到好奇,请使用帮助文件:EXEC xp_cmdshell 'bcp /?'
!就我自己而言,我在 SQL 2005 中使用此方法,在 SQL 2008 和 2008 R2 中失败,但出现以下异常:Error = [Microsoft][SQL Native Client]BCP 数据文件中遇到意外的 EOF。我看了又看,但似乎我的文件采用标准 UTF 格式,带有标准逗号分隔符和标准 \n 换行 SQL bcp 不喜欢。然而我的下一个解决方案立即奏效了。
方法 B:批量插入:
按照上面的步骤 2 和 3 进行操作
这样运行批量插入:
批量插入 dbo.Test
来自“C:\test\Test.csv”
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
只要您的表数据类型与导入匹配,就应该没问题。最终,与必须不断运行向导或打开 Business Intelligence Development Studio 相比,对我来说,一个简单的表创建和一个三行语句相当快,此时我还不如编写一个自定义 C# 应用程序。
Figured it out:
Approach A: bcp:
Ensure that xp_cmdshell is on. (If you are on production environment for an enterprise company this MAY not be an option for security reasons. You may always use a BETA or QA environment to put the data to first and hopefully have linked servers set up).
exec sp_configure 'xp_cmdshell', 1
reconfigure
Create a csv file for testing purposes. I did a three column csv flat file as such:
A, Brett, 1
B, John, 2
C, Brian, 3
Create a temp or permanent table matching the data types shown here:
create table Test ( value varchar(3), Name varchar(16), ID int)
Run bcp from the command shell ensuring you do not have the file open.
EXEC xp_cmdshell 'bcp Test..Test in "C:\test\Test.txt" -c -t , -r \n -T'
GO
if you are curious on switches with bcp, use the help file: EXEC xp_cmdshell 'bcp /?'
! For myself I had this method work in SQL 2005 and FAIL in SQL 2008 and 2008 R2 with this exception: Error = [Microsoft][SQL Native Client]Unexpected EOF encountered in BCP data-file. I looked and looked and looked but it appears that my file in standard UTF format with the standard comma delimeter and the standard \n line feed SQL bcp does not like. However my next solution worked right away.
Approach B: Bulk Insert:
Follow steps 2 and 3 above the same
Run Bulk Insert as such:
BULK INSERT dbo.Test
FROM 'C:\test\Test.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
As long as your table data types match the import, you should be fine. Ulimtately a simple table creation and a three line statement to me is pretty quick compared to having to run a wizard constantly or open up Business Intelligence Development Studio at which point I might as well just a write a custom C# app.