How to read a file's contents into an SQL variable

旧街凉风 提交于 2019-11-30 14:58:18

问题


Could someone tell me how to read a file's contents into an MS SQL variable using T-SQL?


回答1:


DECLARE @FileContents  VARCHAR(MAX)

SELECT @FileContents=BulkColumn
FROM   OPENROWSET(BULK'PathToYourFile.sql',SINGLE_BLOB) x; -- BINARY
--FROM OPENROWSET(BULK'PathToYourFile.sql',SINGLE_CLOB) x; -- CHAR

The SQL Server service account needs to have permissions to read the file obviously.




回答2:


Make use of SQLCMD to execute the .sql (either from command prompt or within SSMS). If you want to use it within SSMS then first turn the SQLCMD mode (Query >> SQLCMD Mode)

Check out http://msdn.microsoft.com/en-us/library/ms174187.aspx

:r yourFilename

something like:

:r d:\scripts\sample.sql


来源:https://stackoverflow.com/questions/11539424/how-to-read-a-files-contents-into-an-sql-variable

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