Bir text dosyada bulunan kayitlari T-SQL ile okumak için OpenRowSet fonksiyonunu kullanabilirsiniz.
select * from OpenRowset(‘MSDASQL’, ‘Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=C:\;’,‘select * from textDosya1.txt’) |
Yukaridaki sql scripti text dosya içindeki bilgileri satir satir ekrana getirir. Eger siz tüm veriyi bir bütün halinde almak istiyorsaniz BULk özelligini kullanmalisiniz.
SELECT * FROM OPENROWSET(BULK N’C:\textDosya1.txt’, SINGLE_CLOB)AS Document |
Yukariki scripte SINGLE_CLOB parametresi dosya içindeki bilgileri ASCII olarak getirir. Eger tüm veriyi byte olarak olumak isterseniz SINGLE_CLOB yerine SINGLE_BLOB kullanmaniz gerekiyor.
OpenRowSet kullanmak istediginiz asagidaki gibi bir hata alirsaniz yapmaniz gereken tek sey Ad Hoc Distributed Queries islemini aktif hale getirmeniz.
Hata Mesaji:
Msg 15281, Level 16, State 1, Line 1 SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more information about enabling ‘Ad Hoc Distributed Queries’, see "Surface Area Configuration" in SQL Server Books Online. |
Çözüm:
sp_configure ‘show advanced options’,1 reconfigure with override go sp_configure ‘Ad Hoc Distributed Queries’,1 reconfigure with override go |