How to pass in parameters to a SQL Server script called with sqlcmd?(如何将参数传递给使用 sqlcmd 调用的 SQL Server 脚本?)
问题描述
是否可以将参数传递给 SQL Server 脚本?我有一个创建数据库的脚本.它是使用 sqlcmd 从批处理文件中调用的.该 SQL 脚本的一部分如下:
Is it possible to pass parameters to a SQL Server script? I have a script that creates a database. It is called from a batch file using sqlcmd. Part of that SQL script is as follows:
CREATE DATABASE [SAMPLE] ON PRIMARY
( NAME = N'SAMPLE', FILENAME = N'c:devSAMPLE.mdf' , SIZE = 23552KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'SAMPLE_log', FILENAME = N'c:devSAMPLE_log.ldf' , SIZE = 29504KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
我希望能够传入数据库和日志的文件名,这样我就不必硬编码C:devSAMPLE.mdf"和C:devSAMPLE_log.ldf".
I want to be able to pass in the filenames for the database and the log so that I don't have to hardcode 'C:devSAMPLE.mdf' and 'C:devSAMPLE_log.ldf'.
有没有办法做到这一点?我正在运行 Microsoft SQL Server 2008 Express.如果您需要更多信息,请告诉我.
Is there a way to do this? I am running Microsoft SQL Server 2008 Express. Let me know if you need any more information.
推荐答案
使用 -v 开关传入变量.
Use the -v switch to pass in variables.
sqlcmd -v varMDF="C:devSAMPLE.mdf" varLDF="C:devSAMPLE_log.ldf"
然后在你的脚本文件中
CREATE DATABASE [SAMPLE] ON PRIMARY
( NAME = N'SAMPLE', FILENAME = N'$(varMDF)' , SIZE = 23552KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'SAMPLE_log', FILENAME = N'$(varLDF)' , SIZE = 29504KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
这篇关于如何将参数传递给使用 sqlcmd 调用的 SQL Server 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将参数传递给使用 sqlcmd 调用的 SQL Server 脚本?


基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01