如何在SSMS中以SQLCMD模式获取文件的相对路径?

发布于 2024-12-25 02:39:29 字数 875 浏览 1 评论 0原文

我有下面的主脚本,它创建表,插入一些数据,然后创建存储过程。

--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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半衬遮猫 2025-01-01 02:39:29

您可以通过使用 sqlcmd setvar 选项将路径分配给变量来解决此问题。然后在您的 :r 调用中使用该变量,如下所示:

:setvar path "c:\some path"
:r $(path)\myfile.sql

此链接有一个更深入的示例:
http://www.simple-talk.com/sql/ sql-tools/the-sqlcmd-workbench/

就位后,您可以删除 setvar 行并将其从命令行传递:

Sqlcmd /Sserver /E -ddatabase -iInputfilename -oOutputfilename -v path=c:\somepath

这将解决脚本未从所在目录运行的问题第一个 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:

:setvar path "c:\some path"
:r $(path)\myfile.sql

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:

Sqlcmd /Sserver /E -ddatabase -iInputfilename -oOutputfilename -v path=c:\somepath

This would work around the issue of the script not running from the directory where the first SQL script was called from.

萌能量女王 2025-01-01 02:39:29

我意识到这已经很旧了,但我注意到您编辑的代码中有一个错误:您需要在路径和脚本名称之间包含反斜杠。

:r $(path)\$(ddl)
:r $(path)\$(dml)
:r $(path)\$(sprocs)

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.

:r $(path)\$(ddl)
:r $(path)\$(dml)
:r $(path)\$(sprocs)
你对谁都笑 2025-01-01 02:39:29

在 SSMS 中获取相对路径并不是那么简单,因为您没有执行脚本; SSMS 已将脚本加载到内存中并正在执行其文本。所以当前目录/文件夹是默认的进程启动文件夹。您可以通过在 SSMS 中的 SQLCMD 模式下运行以下命令来看到这一点:

!! PWD

但是,我确实找到了一种方法来执行此操作。我承认这不是最理想的方法,但是,它目前似乎是获得 true 相对路径的唯一方法(假设在变量中设置路径不是'本身并不是真正的“相对”)。

所以你可以做的是:

  1. 执行 DOS 命令来查找 todo_master.sql 并将该文件的路径存储在可以从 SSMS 获取的文件夹中的文本文件中,因为它是一个硬文件-coded 路径或因为它使用可用于 DOS 命令和 SSMS 中的 SQLCMD 模式的环境变量
  2. 将路径存储在该文件中时,将其存储为将变量设置为该路径的 SQLCMD 模式命令
  3. 导入该文本文件进入SSMS 使用 :r 并将该变量设置为所需路径
!! CD C:\ & FOR /F %B IN ('DIR /B /A -HS /S todo_master.sql') DO ECHO :setvar mypath "%~dpB" > %TEMP%\relative_path.txt

:r $(TEMP)\relative_path.txt

:r $(mypath)\todo_create_ddl.sql
GO
:r $(mypath)\todo_create_dml.sql
GO
:r $(mypath)\todo_create_sprocs.sql
GO

注意:

  • 上述方法假设系统上只有 1 个文件名为 todo_master.sql。如果多个文件具有该名称,则找到的最后一个文件将是 relative_path.txt 文件中设置的路径

  • 执行 CD C:\ 将从以下位置开始C: 驱动器的根目录。这可能不是最有效的起点。如果您的 SQL 文件通常位于 C:\Users{YourLogin}\Documents\Visual Studio 2013\Projects 等区域,则只需更改 CD 命令即可获取离目的地较近,如:

    <代码>!! CD C:\Users\{YourLogin}\Documents\Visual Studio 2013 &为了 ...
    

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:

!! PWD

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:

  1. Execute a DOS command to find todo_master.sql and store the path to that file in a text file in a folder that you can get from SSMS, either because it is a hard-coded path or because it uses an environment variable that is available to both the DOS command and to SQLCMD mode in SSMS
  2. When storing the path in that file, store it as a SQLCMD mode command that sets a variable to that path
  3. Import that text file into SSMS using :r and it will set that variable to the desired path
!! CD C:\ & FOR /F %B IN ('DIR /B /A -HS /S todo_master.sql') DO ECHO :setvar mypath "%~dpB" > %TEMP%\relative_path.txt

:r $(TEMP)\relative_path.txt

:r $(mypath)\todo_create_ddl.sql
GO
:r $(mypath)\todo_create_dml.sql
GO
:r $(mypath)\todo_create_sprocs.sql
GO

Notes:

  • 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 the CD command to get closer to the destination, such as:

    !! CD C:\Users\{YourLogin}\Documents\Visual Studio 2013 & FOR ...
    
聚集的泪 2025-01-01 02:39:29

我发现,这将是最好的:

:setvar path C:\"some path"

:r $(path)\myfile.sql

您必须将带有空格的语句放在引号中,而不是整个语句。这就是为什么你可以做 C:\"some path"

I have found, that this would be the best:

:setvar path C:\"some path"

:r $(path)\myfile.sql

You have to put statements with a space in quotes, but not the entire statement. That is why you can do C:\"some path"

晨曦慕雪 2025-01-01 02:39:29

我自己也遇到了同样的问题,我希望我没有说明显而易见的问题 - 为什么不打开 Dos/Cmd 或 PowerShell 实例,cd 到包含脚本的目录,然后从那里加载 Management Studio?

我的 PowerShell 设置中有此别名(您的路径可能不同):

Set-Alias -Name 'Ssms' -Value "${env:ProgramFiles(x86)}\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\Ssms.exe" 

然后我 cd 到包含解决方案文件的文件夹,然后我执行

Ssms mysolution.ssmssln

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):

Set-Alias -Name 'Ssms' -Value "${env:ProgramFiles(x86)}\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\Ssms.exe" 

I then cd to the folder containing the solution file, then I do

Ssms mysolution.ssmssln
活泼老夫 2025-01-01 02:39:29

使用批处理文件作为帮助程序。

修改 todo_master.sql 以使用名为 mypath 的环境变量:

use master
go 
:r $(mypath)\todo_create_ddl.sql
:r $(mypath)\todo_create_dml.sql
:r $(mypath)\todo_create_sprocs.sql
go

并在批处理文件 todo_master.bat

/* set the environment variable to the current directory */
SET mypath=%cd%
/* run your sql command */
sqlcmd -i todo_master.sql

Use a batchfile as a helper.

Modify todo_master.sql to use an environment variable called mypath:

use master
go 
:r $(mypath)\todo_create_ddl.sql
:r $(mypath)\todo_create_dml.sql
:r $(mypath)\todo_create_sprocs.sql
go

And in your batch file todo_master.bat

/* set the environment variable to the current directory */
SET mypath=%cd%
/* run your sql command */
sqlcmd -i todo_master.sql
z祗昰~ 2025-01-01 02:39:29

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文