[英]Run sql script from another sql script
I want to run all scripts from specified folder. 我要运行指定文件夹中的所有脚本。 I created sql-cursor which contains script's paths. 我创建了包含脚本路径的sql-cursor。
I want to execute script and log usage of them, but how can I run sql script inside other sql script. 我想执行脚本并记录它们的使用情况,但是如何在其他sql脚本中运行sql脚本。 There is ':r' statement used in sqlproj in Visual Studio, but it doesn't work in my case, so how can I resolve it? 在Visual Studio的sqlproj中使用了':r'语句,但是在我的情况下它不起作用,那么如何解决呢? I use SQL Server 2016. 我使用SQL Server 2016。
FETCH NEXT FROM ScriptsFromFolderCursor INTO @scriptName
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @fullScriptPath nvarchar(1024) = @MyPath + @scriptName
-- WHAT SHOULD BE THERE ??
:r @fullScriptPath
-- log usage of script
INSERT INTO DeployedSqlScripts([ScriptName], [Version],[DateUtc])
VALUES (@fullScriptPath, @DbVersion, GetUtcDate())
FETCH NEXT FROM ScriptsFromFolderCursor INTO @scriptName
END
I got error: 我收到错误消息:
Error SQL72001: The included file @fullScriptPath (C:\\GIT\\myPath\\@FULLSCRIPTPATH) does not exist. 错误SQL72001:包含的文件@fullScriptPath(C:\\ GIT \\ myPath \\ @FULLSCRIPTPATH)不存在。 (54, 8) (54、8)
Not sure what error you get, but by the looks of it you are trying to use a SQLCMD statement ( :r
) 不知道会收到什么错误,但是从外观上看,您正在尝试使用SQLCMD语句( :r
)
Have you executed this script in SQLCMD Mode? 您是否已在SQLCMD模式下执行此脚本? Documentation - https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility 文档-https://docs.microsoft.com/zh-cn/sql/tools/sqlcmd-utility
If you're willing to use a scripting environment other than T-SQL, you could do this with execsql.py ( http://pypi.python.org/pypi/execsql ). 如果您愿意使用T-SQL以外的脚本环境,则可以通过execsql.py( http://pypi.python.org/pypi/execsql )执行此操作。 The INCLUDE metacommand ( http://pythonhosted.org/execsql/#include ) will allow you to insert another SQL script within the running script, and the example of importing all CSV files in a directory ( http://pythonhosted.org/execsql/#example13 ) could be easily adapted to use to include all SQL scripts in a directory. INCLUDE元命令( http://pythonhosted.org/execsql/#include )将允许您在运行的脚本中插入另一个SQL脚本,以及将所有CSV文件导入目录中的示例( http://pythonhosted.org/ execsql /#example13 )可以很容易地修改为用于在目录中包含所有SQL脚本。
Disclaimer: I wrote execsql.py. 免责声明:我写了execsql.py。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.