问题
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