如何将参数传递给使用sqlcmd调用的SQL Server脚本?

编程入门 行业动态 更新时间:2024-10-25 21:32:19
本文介绍了如何将参数传递给使用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:\dev\SAMPLE.mdf' , SIZE = 23552KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = N'SAMPLE_log', FILENAME = N'c:\dev\SAMPLE_log.ldf' , SIZE = 29504KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)

我希望能够传递数据库和日志的文件名,这样我就不必对"C:\ dev \ SAMPLE.mdf"和"C:\ dev \ SAMPLE_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:\dev\SAMPLE.mdf' and 'C:\dev\SAMPLE_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:\dev\SAMPLE.mdf" varLDF="C:\dev\SAMPLE_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脚本?

本文发布于:2023-06-04 19:07:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   脚本   参数   sqlcmd   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!