如何在SSMS中以SQLCMD模式获取文件的相对路径?
我有下面的主脚本,它创建表,插入一些数据,然后创建存储过程。
--todo_master.sql
use master
go
:r todo_create_ddl.sql
:r todo_create_dml.sql
:r todo_create_sprocs.sql
go
但是,即使 todo_master.sql 与其他三个脚本位于同一路径中,它也无法找到这三个脚本。
我收到以下错误:
A fatal scripting error occurred.
The file specified for :r command was not found.
如果我提供如下所示的完整路径,则会按预期找到并执行这些文件。
“C:\Docs and Settings\user\My Docs\SSMS\Projects\todo_create_ddl.sql”
我可能缺少什么?
编辑 按照 Jason 的建议,我尝试了这个,但仍然得到相同的错误:
use master
go
:setvar path "C:\Documents and Settings\user\My Documents\SQL Server Management Studio\Projects"
:setvar ddl "todo_create_ddl.sql"
:setvar dml "todo_create_dml.sql"
:setvar sprocs "todo_create_sprocs.sql"
:r $(path)$(ddl)
:r $(path)$(dml)
:r $(path)$(sprocs)
go
I have the below master script which creates tables, and inserts some data and then creates the stored procedures.
--todo_master.sql
use master
go
:r todo_create_ddl.sql
:r todo_create_dml.sql
:r todo_create_sprocs.sql
go
However, even though the todo_master.sql is in the same path as the other three scripts, it is unable to locate those three scripts.
I get the following error:
A fatal scripting error occurred.
The file specified for :r command was not found.
If I provide the complete path like below, these files are found and executed as intended.
"C:\Docs and Settings\user\My Docs\SSMS\Projects\todo_create_ddl.sql"
What might I be missing?
Edit
As suggested by Jason I tried this, but still get the same error:
use master
go
:setvar path "C:\Documents and Settings\user\My Documents\SQL Server Management Studio\Projects"
:setvar ddl "todo_create_ddl.sql"
:setvar dml "todo_create_dml.sql"
:setvar sprocs "todo_create_sprocs.sql"
:r $(path)$(ddl)
:r $(path)$(dml)
:r $(path)$(sprocs)
go
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过使用 sqlcmd
setvar
选项将路径分配给变量来解决此问题。然后在您的:r
调用中使用该变量,如下所示:此链接有一个更深入的示例:
http://www.simple-talk.com/sql/ sql-tools/the-sqlcmd-workbench/
就位后,您可以删除 setvar 行并将其从命令行传递:
这将解决脚本未从所在目录运行的问题第一个 SQL 脚本被调用。
You can work around this by using the sqlcmd
setvar
option to assign the path to a variable. Then use that variable in your:r
call like:This link has a more in depth example:
http://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/
With that in place you can remove the setvar line and pass that in from the command line with:
This would work around the issue of the script not running from the directory where the first SQL script was called from.
我意识到这已经很旧了,但我注意到您编辑的代码中有一个错误:您需要在路径和脚本名称之间包含反斜杠。
I realize this is pretty old, but I noticed an error in your Edited code: you need to include a backslash between the path and the script name.
在 SSMS 中获取相对路径并不是那么简单,因为您没有执行脚本; SSMS 已将脚本加载到内存中并正在执行其文本。所以当前目录/文件夹是默认的进程启动文件夹。您可以通过在 SSMS 中的 SQLCMD 模式下运行以下命令来看到这一点:
但是,我确实找到了一种方法来执行此操作。我承认这不是最理想的方法,但是,它目前似乎是获得 true 相对路径的唯一方法(假设在变量中设置路径不是'本身并不是真正的“相对”)。
所以你可以做的是:
:r
并将该变量设置为所需路径注意:
上述方法假设系统上只有 1 个文件名为 todo_master.sql。如果多个文件具有该名称,则找到的最后一个文件将是 relative_path.txt 文件中设置的路径
执行
CD C:\
将从以下位置开始C: 驱动器的根目录。这可能不是最有效的起点。如果您的 SQL 文件通常位于 C:\Users{YourLogin}\Documents\Visual Studio 2013\Projects 等区域,则只需更改CD
命令即可获取离目的地较近,如:Getting a relative path in SSMS is not that straight-forward since you are not executing the script; SSMS has loaded the script into memory and is executing its text. So the current directory/folder is the default process starting folder. You can see this by running the following in SQLCMD mode in SSMS:
However, I did find a kinda-sorta way to do this. I admit that this is not the super-most ideal way to do this, however, it currently seems to be the only way to get a true relative path (given that setting the path in a variable isn't really "relative" per se).
So what you can do is:
:r
and it will set that variable to the desired pathNotes:
The above method assumes only 1 file on the system is named todo_master.sql. If more than one file has that name, the last one found will be the path set in the relative_path.txt file
Doing the
CD C:\
will start at the root of the C: drive. This is probably not the most efficient place to start. If you generally have your SQL files in an area such as C:\Users{YourLogin}\Documents\Visual Studio 2013\Projects, then just change theCD
command to get closer to the destination, such as:我发现,这将是最好的:
您必须将带有空格的语句放在引号中,而不是整个语句。这就是为什么你可以做
C:\"some path"
I have found, that this would be the best:
You have to put statements with a space in quotes, but not the entire statement. That is why you can do
C:\"some path"
我自己也遇到了同样的问题,我希望我没有说明显而易见的问题 - 为什么不打开 Dos/Cmd 或 PowerShell 实例,
cd
到包含脚本的目录,然后从那里加载 Management Studio?我的 PowerShell 设置中有此别名(您的路径可能不同):
然后我
cd
到包含解决方案文件的文件夹,然后我执行I had this same issue myself and I hope I'm not stating the obvious - why not open a Dos/Cmd or PowerShell instance,
cd
to the directory containing the scripts, then load Management Studio from there?I have this alias in my PowerShell set-up (your path may differ):
I then
cd
to the folder containing the solution file, then I do使用批处理文件作为帮助程序。
修改 todo_master.sql 以使用名为
mypath
的环境变量:并在批处理文件
todo_master.bat
中Use a batchfile as a helper.
Modify todo_master.sql to use an environment variable called
mypath
:And in your batch file
todo_master.bat
1) 文件和文件夹中没有空格
2) 在部署后脚本中给出绝对路径。
3)删除部署后脚本中的双冒号
4)部署后脚本路径应该有 / 而不是 \ (使用 Github 操作的 CICD 管道部署)。
:r 主文件夹/子文件夹1/子文件夹2/file1.sql
GO
1)No spaces in files and folders
2)Give absolute path in post deployment script.
3)Remove the double colon in post deployment script
4)post deployment script path should have / not the \ (CICD pipeline deployment using Github actions).
:r mainfolder/subfolder1/subfilder2/file1.sql
GO