sql安装弹出sqlcmd_SQL Server中SQLCMD实用工具概述

article/2025/11/10 7:39:52

sql安装弹出sqlcmd

This article is aimed at helping you understand the sqlcmd utility. Of course, this is a tool most of us have used it at several occasions given that the tool has been around for a decade now. However, to be a solid starting point, this article contains some examples that help you understand the tool from different viewpoints.

本文旨在帮助您了解sqlcmd实用程序。 当然,考虑到该工具已经使用了十年,我们大多数人都多次使用它。 但是,作为一个坚实的起点,本文包含一些示例,可以帮助您从不同的角度理解该工具。

This article discusses the following:

本文讨论以下内容:

  1. How to enable SQLCMD

    如何启用SQLCMD
  2. A few important points to remember

    需要记住的一些要点
  3. SQLCMD with scripting variables in SSMS

    在SSMS中具有脚本变量SQLCMD
  4. Passing variables (or argument) as T-SQL and or a SQL Script file

    将变量(或参数)作为T-SQL和/或SQL脚本文件传递
  5. SQLCMD with scripting variables and Windows scripting in SSMS

    在SSMS中具有脚本变量和Windows脚本SQLCMD

什么是SQLCMD? (What is SQLCMD?)

sqlcmd is a command line utility that is part of the standard installation of SQL Server 2005 or higher, which enables interactive interface with any SQL Server instance to help perform the following tasks:

sqlcmd是一个命令行实用程序,是SQL Server 2005或更高版本的标准安装的一部分,它启用与任何SQL Server实例的交互界面以帮助执行以下任务:

  1. Execute Transact-SQL (T-SQL) statements in SSMS

    在SSMS中执行Transact-SQL(T-SQL)语句
  2. Call a T-SQL script file

    调用T-SQL脚本文件
  3. Use of environment variables in command mode

    在命令模式下使用环境变量
  4. Store the output results of executed queries in a specified text file

    将执行的查询的输出结果存储在指定的文本文件中

为什么选择SQLCMD? (Why SQLCMD?)

sqlcmd is a simple, yet powerful scripting environment that helps with the automation of several tasks related to SQL Server. For example, you can write and execute a script that logs you into a specific instance of SQL Server, executes a script from a specified path, and redirects the output to a certain file.

sqlcmd是一个简单但功能强大的脚本环境,可帮助自动化与SQL Server相关的多个任务。 例如,您可以编写和执行脚本,以将您登录到SQL Server的特定实例,从指定路径执行脚本,然后将输出重定向到特定文件。

Most of us SQL database administrators are already using PowerShell. The Invoke-SqlCmd cmdlet, from the sqlserver module that’s part of every SQL Server distribution since 2008, packs most of the capabilities of sqlcmd. Also, in SQL Server Management Studio, we have a SQLCMD mode, that simulates the capabilities of sqlcmd, and accepts some of the commands that are not part of the T-SQL language.

我们大多数SQL数据库管理员已经在使用PowerShell。 自2008年以来,每个SQL Server发行版中都包含sqlserver模块的Invoke-SqlCmd cmdlet包含了sqlcmd的大多数功能。 另外,在SQL Server Management Studio中,我们有一个SQLCMD模式,该模式模拟sqlcmd的功能,并接受不属于T-SQL语言的某些命令。

sqlcmd was an extension of the osql and isql command line utilities, that contains a rich set of parameters and options to enable automation of various administrative tasks.

sqlcmd是osql和isql命令行实用程序的扩展,其中包含一组丰富的参数和选项,可实现各种管理任务的自动化。

  1. It’s simple to bind SQL in a .bat file.

    将SQL绑定到.bat文件很简单。
  2. It runs interactively across various OS platforms.

    它可以跨各种OS平台交互运行。
  3. It’s easy to pass command line arguments to a SQL file using sqlcmd.

    使用sqlcmd将命令行参数传递给SQL文件很容易。

如何启用SQLCMD (How to enable SQLCMD)

OK, so sqlcmd is a great tool; of that, we’re all convinced. Let’s now talk about how the query editor in SSMS can be used to write statements as sqlcmd scripts. The following example talks about the integration of Windows system commands and T-SQL statements into a single script.

好的,因此sqlcmd是一个很好的工具; 对此,我们都深信不疑。 现在让我们讨论一下如何使用SSMS中的查询编辑器将语句编写为sqlcmd脚本。 以下示例讨论将Windows系统命令和T-SQL语句集成到单个脚本中的情况。

It’s importatnt to remember that sqlcmd mode is not enabled by default. To enter into the sqlcmd mode in the SSMS query editor, follow the steps below:

请记住,默认情况下未启用sqlcmd模式,这一点很重要。 要在SSMS查询编辑器中进入sqlcmd模式,请执行以下步骤:


  1. OR
    要么
    Open a new Database Engine Query Editor window.
    打开一个新的数据库引擎查询编辑器窗口。
  2. SQLCMD Mode. SQLCMD模式

To enable SQLCMD scripting mode:

要启用SQLCMD脚本模式:

  1. Tools menu, select 工具”菜单,选择“ Options. 选项”
  2. Query Execution tab, and select 查询执行”选项卡,然后选择“ SQL Server, click the SQL Server” ,然后单击“ General page. 常规”页面。
  3. By default open new queries in SQLCMD Mode. 默认情况下,以SQLCMD模式打开新查询 ”复选框。

Some important points to remember 要记住的一些重要点
  1. SQLCMD commands must be prefixed with a colon. This makes the difference between SQLCMD commands and Transact-SQL clear.

    SQLCMD命令必须以冒号作为前缀。 这使SQLCMD命令和Transact-SQL之间的区别清晰可见。
  2. same session context. 相同的会话上下文连接到其他服务器。


  3. The variables that are used as part of the SQLCMD script are case-sensitive. For example, if we query the environment variables, we can see that COMPUTERNAME and computername are two different variables in the output. In the first, the environment variable is queried to return the name of the computer, where as in the second, SSMS isn’t able to resolve the variable and reports an error.

    用作SQLCMD脚本一部分的变量区分大小写。 例如,如果查询环境变量,我们可以看到COMPUTERNAME和computername是输出中的两个不同变量。 在第一个环境变量中,查询环境变量以返回计算机的名称,在第二个环境中,SSMS无法解析该变量并报告错误。
:connect hqdbsp18
PRINT '$(COMPUTERNAME)'  
GO
:connect hqdbsp17
PRINT '$(computername)'

SQLCMD的示例 (Examples of SQLCMD)

The following example defines an integration of operating system commands and SQL statements together.

以下示例定义了操作系统命令和SQL语句的集成。

Variable are defined using SETVAR, connection is built using CONNECT the keyword, operating system commands are defined using !!, the output file is declared using the OUT keyword, and SQL Statements are placed to fetch the results based on the connection string.

使用SETVAR定义变量,使用关键字CONNECT建立连接,使用!!定义操作系统命令,使用OUT关键字声明输出文件,并放置SQL语句以基于连接字符串获取结果。

--variable declartion
:setvar subscriber1 HQMESRP01   
:setvar subscriber2 HQMESRP02
:setvar file1 c:\Data1.txt
:setvar file2 c:\Data2.txt--check for the file and delete if exists
!!if exist $(file1) del $(file1)
!!if exist $(file2) del $(file2)--connect to to the subscription 1  
:Connect $(subscriber1)
--redirect the SQL output to file
:out $(file1)
select * from [MES_REPL_Rpt_AP]..tb_F4801_woheader where wadoco=2520079
select * from [MES_REPL_Rpt_AP]..tb_F4801_woheader_MES where WorkOrderNumber=2520079
GO--connect to to the subscription 2  
:Connect $(subscriber2)--redirect the SQL output to file
:out $(file2)
select * from [MES_HIST_AP]..tb_F4801_woheader where wadoco=2520079
select * from [MES_HIST_AP]..tb_F4801_woheader_MES where WorkOrderNumber=2520079

SQLCMD with scripting variables, and Windows scripting in SSMS

具有脚本变量SQLCMD和SSMS中的Windows脚本

In this example, login is created across multiple servers. T-SQL, sqlcmd and Windows scripting are all integrated, and run using the SSMS interface to create logins.

在此示例中,登录是在多个服务器上创建的。 T-SQL,sqlcmd和Windows脚本均已集成,并使用SSMS界面运行以创建登录名。

  1. Enable XP_CMDSHELL

    启用XP_CMDSHELL
  2. Define the T-SQL in a SQL file

    在SQL文件中定义T-SQL
  3. Build the dynamic SQL

    建立动态SQL
  4. Verify the output

    验证输出

STEP 1: In this step, xp_cmdshell is enabled. It is not a recommended option, though. This has only been enabled for the purpose of the demo.

步骤1:在此步骤中,启用了xp_cmdshell。 但是,不建议使用此选项。 仅在演示中启用了此功能。

sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
sp_configure 'xp_cmdShell', 1;  
GO  
RECONFIGURE;  
GO

STEP 2: A SQL file, SysAdminLoginCreation.sql is created with the create login SQL statement.

步骤2:使用create login SQL语句创建一个SQL文件SysAdminLoginCreation.sql。

USE [master]
GO
CREATE LOGIN [SQLShackTest] WITH PASSWORD=N'SQLShackDemo123', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO

The file looks like below

该文件如下所示

We create a for loop to loop through each SQL instances.

我们创建一个for循环来遍历每个SQL实例。

  1. Iterate through the input file.

    遍历输入文件。
  2. FOR Parameter %j defines the name of the SQL instance.

    FOR参数%j定义SQL实例的名称。
  3. Call sqlcmd using the parameters as part of the command.

    使用参数作为命令的一部分来调用sqlcmd。
  4. Repeat for each data item.

    对每个数据项重复上述步骤。
MASTER..XP_CMDSHELL 'for /f %j in (\\networkshare\IsDba\server.txt) do sqlcmd -S %j -i \\networkshare \IsDba\SysAdminLoginCreation.sql -E'

sqlcmd executed successfully!

sqlcmd执行成功!

Let’s check whether the logins are created on the those servers

让我们检查登录名是否在这些服务器上创建

:connect hqdbsp18
select * from sys.syslogins where name='SQLShackTest'
GO
:connect hqdbsp17
select * from sys.syslogins where name='SQLShackTest'

The logins have also been created for the listed servers.

还为列出的服务器创建了登录名。

Passing variables (or argument) as T-SQL and SQL Script file

将变量(或参数)作为T-SQL和SQL脚本文件传递

We can pass the variables as an input to the T-SQL or the SQL script file in the sqlcmd command console. The scripting variables are declared using :SETVAR keyword. The corresponding parameter values passed to T-SQL or SQL script file. The values are enclosed in $(variable name) are fed to the SQLs during the run time. Using -v (version) switch, the parameters are declared and fed to the script. We can list any number of variables with no delimiter in between the parameters. Lets follow the example to understand the mechanism better:

我们可以将变量作为输入传递给T-SQL或sqlcmd命令控制台中SQL脚本文件。 脚本变量使用:SETVAR关键字声明。 相应的参数值传递给T-SQL或SQL脚本文件。 $(变量名)中包含的值在运行时被馈送到SQL。 使用-v(版本)开关,可以声明参数并将其输入脚本。 我们可以列出任意数量的变量,并且在参数之间没有定界符。 让我们按照示例更好地了解该机制:

  1. salesorderdetails is the name of table assigned to salesorderdetails是分配给tablename variable tablename变量的表的名称
  2. The select statement is prepared along with the tablename variable

    select语句与tablename变量一起准备
C:\Users\ccov648>sqlcmd -S hqdbt01\SQL2017 -E
1> use Python2017
2> :SETVAR tablename salesorderdetail
2> select top(10) * from $(tablename);
3> go

We can assume that database name will be provided as a SQLCMD variable during the deployment process; we can have exactly the same file deployed to all environments. The following example has two parameters, db and tablename. The input parameters are separated by a space in between.

我们可以假设数据库名称将在部署过程中作为SQLCMD变量提供; 我们可以将完全相同的文件部署到所有环境。 以下示例具有两个参数,即db和tablename。 输入参数之间用空格隔开。

C:\Users\ccov648>sqlcmd -S hqdbt01\SQL2017 -E -i n:\IsDba\SalesOrderDetails.sql -v db=Python2017  tablename=salesorderdetail

摘要 (Summary)

SQLCMD mode in SSMS (SQL Server Management Studio) provides an interface to run SQL statements across multiple servers and across platforms. This is really helpful in many cases. In this article, we discussed the creation of logon across different SQL Server instance versions. Similarly, we performed data validation between the two subscribers. We also saw that, if there is a complex SQL query which requires multiple inputs, the values can be passed as parameters to the T-SQL or the SQL Script file.

SSMS(SQL Server Management Studio)中SQLCMD模式提供了一个界面,可在多个服务器和平台之间运行SQL语句。 在许多情况下,这确实很有帮助。 在本文中,我们讨论了跨不同SQL Server实例版本的登录的创建。 同样,我们在两个订户之间执行了数据验证。 我们还看到,如果有一个复杂SQL查询需要多个输入,则可以将这些值作为参数传递给T-SQL或SQL Script文件。

Having said all that, we need to remember that SQLCMD mode only allows the use of sqlcmd environment commands within SSMS, however, without the typical IntelliSense support, or support for debugging. Maintenance of scripts that mix pure T-SQL and SQLCMD can require effort. Use it wisely!

说了这么多,我们需要记住,SQLCMD模式仅允许在SSMS中使用sqlcmd环境命令,但是没有典型的IntelliSense支持或调试支持。 混合纯T-SQL和SQLCMD的脚本的维护可能会很费力。 明智地使用它!

目录 (Table of contents)

Getting started building applications using SQL Server DevOps Tools
Overview of SQLCMD utility in SQL Server
The BCP (Bulk Copy Program) command in action
Continuous Database Delivery (CD) using SQL Server Tools SqlPackage.exe
All about MSSQL-Scripter, the SQL Server cross-platform scripting Tool
Getting started with Azure Data Studio (ADS); initial installation and configuration
开始使用SQL Server DevOps工具构建应用程序
SQL Server中SQLCMD实用工具概述
运行中的BCP(大容量复制程序)命令
使用SQL Server工具SqlPackage.exe的连续数据库传递(CD)
有关MSSQL-Scripter(SQL Server跨平台脚本工具)的全部信息
Azure Data Studio(ADS)入门; 初始安装和配置

翻译自: https://www.sqlshack.com/overview-of-the-sqlcmd-utility-in-sql-server/

sql安装弹出sqlcmd


http://chatgpt.dhexx.cn/article/GYvINwwy.shtml

相关文章

SQLServer知识:sqlcmd用法笔记

今天给大家介绍sqlcmd用法笔记,希望对大家能有所帮助! 1、介绍 sqlcmd是一个 Microsoft Win32 命令提示实用工具,可以通过该命令工具实现SQL语句、脚本的执行,并且可以实现脚本任务的自动化。 2、使用场景 2.1 针对大文件脚本的执…

Sqlcmd使用详解

Sqlcmd实用工具,可以输入 TRANSACT-SQL 语句、 系统过程和脚本文件,通过各种可用模式: 通过命令提示符。在中查询编辑器在 SQLCMD 模式下。在 Windows 脚本文件。在 SQL Server 代理作业的操作系统 (Cmd.exe) 作业步骤。 该实用工具使用 OD…

如何在CMD下写SQL语句

1、WinX->搜索环境变量 2、选择环境变量 3、在系统变量中找到Path,并双击进入 4、选择新建 5、选择安装MySQL的盘,找到bin,并复制路径到刚刚新建的Path中,例如笔者安装的盘是D盘 6、把复制好的路径放到Path中 7、WinR->直…

jquery设置cookie、删除cookie、获取cookie

1.引入两个js 去bootcdn搜索就行。 jquery.js <script src"https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>jquery cookie <script src"https://cdn.bootcdn.net/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"…

关闭浏览器 清除cookie

根据客户要求使用dedecms v5.6版本&#xff0c;发现了一个会员登录的bug&#xff01;看来dedecms也不过如此&#xff01; 首先说说bug所在&#xff1a;在会员登录页面 /member/index.php 登录的时候 有效期如果选择‘即时’&#xff0c;那么就永远也登不上去了&#xff01; 原因…

删除cookie删不掉的问题

文章转自&#xff1a; https://blog.csdn.net/swimming_in_it_/article/details/80290547 https://blog.csdn.net/huanshiwushuang/article/details/70172536 http://php.js.cn/blog/delete-cookie/ 不知道能不能解决你的问题,但是我来说说我的经历&#xff0c;昨天在用Yii…

Javascript清除所有的cookie

只需一个js函数即可&#xff1a; //清除所有cookie函数function clearAllCookie() {var keys document.cookie.match(/[^ ;](?\)/g);if(keys) {for(var i keys.length; i--;)document.cookie keys[i] 0;expires new Date(0).toUTCString()}} 测试的所有代码如下&#xf…

JS创建、获取、删除 cookie 方法

js 创建 cookie 方法 // 没有设置 cookie 过期时间的话&#xff0c;默认是 会话cookie(浏览器关闭时&#xff0c;该cookie失效&#xff0c;被删除) document.cookie cname "" value; // 设置了过期时间的话&#xff0c;在设置的时间段内 cookie 一直存在有效&…

浏览器cookie怎么清除,彻底清除上网痕迹只需这几步

很多用户对cookie不是很了解&#xff0c;我们使用windows系统电脑浏览各个网站的时候都会产生cookie&#xff0c;cookie记录用户名、密码等信息。显而易见的是&#xff0c;cookie和网络隐私安全息息相关&#xff0c;有些恶意网站会利用cookie来侵犯用户隐私安全。因此清理cooki…

vcruntime140.dll发生访问冲突

0x6EF538DC (vcruntime140.dll)处引发的异常: 0xC0000005: 写入位置0x00000000 时发生访问冲突 发生以上问题&#xff0c;有可能为内存不足引起&#xff0c;可尝试分配更大内存。 在本次报错中&#xff0c;我们将1024*1024*8的BUFFER_SIZE扩大为1024*1024*20&#xff0c;成功解…

电脑提示vcruntime140_1.dll丢失怎么安装?

许多用户在使用电脑的时候&#xff0c;在安装运行某些程序的时候&#xff0c;电脑突然提示由于找不到vcruntime140_1.dll&#xff0c;无法继续执行代码&#xff0c;遇到这种情况我们应该怎么解决呢&#xff1f;下面小编就带着大家一起看看吧&#xff01; 首先是打开电脑浏览器后…

电脑提示vcruntime140_1.dll丢失的正确修复方法

vcruntime140_1.dll是vs2010编译的程序默认的库文件它的丢失易导致游戏、应用软件等程序运行出现错误&#xff0c;致使程序无法正常运行&#xff0c;下面小编教大家电脑提示vcruntime140_1.dll丢失的正确修复教程 首先打开一个360&#xff0c;搜狗等等电脑浏览器&#xff0c;输…

vcruntime140_1.dll缺失

问题&#xff1a;安装mysql-8.0.29执行mysql -install命令出现由于找不到VCRUNTIME140_1.dll&#xff0c;无法继续执行代码。 解决方法&#xff1a;下载VCRUNTIME140_1.dll 把文件放到C:\Windows\System32目录下

由于找不到vcruntime140_1.dll,无法继续执行代码重新安装程序可能会解决此问题

问题场景&#xff1a; 提示&#xff1a;由于找不到VCRUNTIME140_1.dll,无法继续执行代码&#xff0c;重新安装程序可能会解决此问题。 问题描述 提示&#xff1a;一般是重置完电脑打开软件时出现&#xff0c;或者是在安装软件的过程中会出现以上问题 先查看是否已经安装有 Vi…

【vcruntime140.dll文件下载】vcruntime140.dll丢失的解决方法

vcruntime140.dll文件对一些电脑软件、电脑游戏等程序的正常运行起到关键性作用。对于弹出缺少此类文件的弹窗&#xff0c;用户们很多时候也摸不着头脑&#xff0c;程序明明上次都能正常运行&#xff0c;突然就弹出缺少vcruntime140.dll文件的提醒窗口。通过小编此次编辑的文章…

unity报错,提示找不到vcruntime140

重装系统后&#xff0c;发现打开unity提示:unity找不到vcruntime140,等好几个.dll格式的文件 于是就想到了系统环境缺少dll&#xff0c;于是打开这个DirectX修复工具4.1&#xff0c;点击检测并修复&#xff0c;提示修复完成&#xff0c;最好重启一下&#xff0c;ok啦 这里我已…

计算机提示“找不到vcruntime140.dll,无法继续执行代码可”以这样子修复

首先&#xff0c;对于那些不熟悉的人来说&#xff0c;vcruntime140.dll是一个关键文件&#xff0c;用于在Windows操作系统上运行使用C语言编写的大型应用程序。如果你正在运行或安装这样的应用程序&#xff0c;但找不到vcruntime140.dll文件&#xff0c;那么你的应用程序可能无…

vcruntime140.dll文件丢失的解决方法

打开电脑浏览器顶部栏目输入【dll修复程序.site】然后点击进入下载vcruntime140.dll文件 下载完成vcruntime140.dll文件安装包。右键进行解压&#xff0c;然后再右键点击以管理员身份运行打开 打开后点击开始安装&#xff0c;电脑开始自动检测丢失的vcruntime140.dll文件等待自…

由于找不到vcruntime140_1.dll,无法继续执行代码。重新安装程序可能会解决此问题。

目录 情景再现 解决办法 情景再现 由于找不到vcruntime140_1.dll,无法继续执行代码。重新安装程序可能会解决此问题。 在安装并启动pgsql时报了该错&#xff1a; 解决办法 这时候其实很简单&#xff0c;无需去下载什么&#xff0c;只需要到本地c盘全局搜索cruntime140_1.d…

vcruntime140.dll丢失的解决方法,vcruntime140.dll下载安装教程

文件vcruntime140.dll需要在电脑打开一个浏览器才能下载顶部输入 dll修复程序.site 【按下键盘的回车键打开】然后点击进入下载dll文件安装包 dll文件安装包下载需要点击解压&#xff0c;然后把解压好的文件点击打开 开始安装丢失的vcruntime140.dll文件系统盘文件等待电脑…