openrowset - How to select from a filename with white spaces?

早过忘川 提交于 2020-01-04 07:09:23

问题


This is working :

SELECT * 
FROM OPENROWSET('MSDASQL','Driver={Microsoft Access Text Driver (*.txt, *.csv)}; Extended Properties="text; HDR=YES; FMT=Delimited"','SELECT * FROM E:\folder\subfolder\myfile.txt')

This is not working because of the white spaces:

SELECT * 
FROM OPENROWSET('MSDASQL','Driver={Microsoft Access Text Driver (*.txt, *.csv)}; Extended Properties="text; HDR=YES; FMT=Delimited"','SELECT * FROM E:\folder\sub folder\my file.txt')

I tried with doubles quotes ("...") and with [...]

Thank you in advance for your tips


回答1:


From the documentation on OPENROWSET specifically on the query (emphasis mine):

'query'

Is a string constant sent to and executed by the provider. The local instance of SQL Server does not process this query, but processes query results returned by the provider, a pass-through query. [...]

In other words, it's not due to SQL Server that this pass-through query is not working.

The following two examples use the DefaultDir property in your provider string and should get your statement to work:

SELECT * FROM OPENROWSET('MSDASQL','Driver={Microsoft Access Text Driver (*.txt, *.csv)}; Extended Properties="text; HDR=YES; FMT=Delimited"; DefaultDir=E:\folder\sub folder;','SELECT * FROM [my file#txt]');

Or

SELECT * FROM OPENROWSET('MSDASQL','Driver={Microsoft Access Text Driver (*.txt, *.csv)}; Extended Properties="text; HDR=YES; FMT=Delimited"; DefaultDir=E:\folder\sub folder;','SELECT * FROM "my file.txt"');


来源:https://stackoverflow.com/questions/34696374/openrowset-how-to-select-from-a-filename-with-white-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!