Read contents of a text file into a varchar WITHOUT using BULK

為{幸葍}努か 提交于 2019-12-22 08:59:25

问题


I've got a problem whereby I need to read data from a .txt file into a variable in SQL Server. The read needs to be performed programmatically, as it's going to form part of a stored procedure, and it needs not to utilise the BULK method, as I don't have permissions to use the BULK method on the database in question. Is this possible?

Thanks in advance :)


回答1:


Can you get them to allow Ad Hoc Distributed Queries? Then you can use OpenRowset or OpenDatasource.

SELECT * 
FROM   OPENROWSET('MSDASQL',  
'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=c:\users\graham\desktop;', 
'SELECT * FROM [data.txt];'

Here's the recofiguring code, if you need it:

EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
go

This is a laborious technique, though -- you sure you can't use client code? Even, I dunno, VBA in Excel or something?

g.




回答2:


There is no other way to read the contents of a file without these permissions in SQL Server, or without setting up a link to the file. Otherwise, you have to do this through SSIS or using programming.

The solution is to get the permissions to solve your issue.



来源:https://stackoverflow.com/questions/19229625/read-contents-of-a-text-file-into-a-varchar-without-using-bulk

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