介绍 (Introduction)
The xp_cmdshell is a very powerful extended procedure used to run the command line (cmd). This is very useful to run tasks in the operative system like copying files, create folders, share folders, etc. using T-SQL.
xp_cmdshell是一个非常强大的扩展过程,用于运行命令行(cmd)。 这对于使用T-SQL在操作系统中运行诸如复制文件,创建文件夹,共享文件夹等任务非常有用。
In this new article, we will show some useful examples about how to use it.
在这篇新文章中,我们将显示一些有关如何使用它的有用示例。
We will show how to do the following tasks:
我们将展示如何执行以下任务:
- How to enable xp_cmdshell 如何启用xp_cmdshell
- How to copy backups from one folder to another using xp_cmdshell 如何使用xp_cmdshell将备份从一个文件夹复制到另一个文件夹
- How to store full path of files of a folder in a SQL Server Table 如何在SQL Server表中存储文件夹文件的完整路径
- How to execute PowerShell in T-SQL using xp_cmdshell 如何使用xp_cmdshell在T-SQL中执行PowerShell
- How to connect to Azure in SSMS using xp_cmdshell and sqlcmd 如何使用xp_cmdshell和sqlcmd在SSMS中连接到Azure
要求 (Requirements)
- SQL Server Installed. In this example, we are using SQL Server 2016, but you can work with SQL Server 2005. 已安装SQL Server。 在此示例中,我们使用的是SQL Server 2016,但是您可以使用SQL Server 2005。
入门 (Getting started)
如何启用xp_cmdshell (How to enable xp_cmdshell)
First, we will enable the xp_cmdshell. We will need to verify if advanced options in SQL Server are enabled. To do that, run the following procedure in SQL Server Management Studio (SSMS):
首先,我们将启用xp_cmdshell。 我们将需要验证是否在SQL Server中启用了高级选项。 为此,请在SQL Server Management Studio(SSMS)中运行以下过程:
USE master;
GO
EXEC sp_configure 'show advanced option'
When you run the command, if the configuration value is 0, it means that Advanced Options are not enabled.
运行命令时,如果配置值为0,则表示未启用“高级选项”。
In order to enable the Advanced Options, set the advanced option to 1. The following example shows how to do it:
为了启用“高级选项”,请将高级选项设置为1。以下示例显示了如何执行此操作:
USE master;
GO
EXEC sp_configure 'show advanced option', '1';
RECONFIGURE WITH OVERRIDE;
Xp_cmdshell is one of the advanced options, now you can enable this extended procedure. The following example shows how to enable it:
Xp_cmdshell是高级选项之一,现在您可以启用此扩展过程。 以下示例显示了如何启用它:
EXEC sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
如何使用xp_cmdshell将备份从一个文件夹复制到另一个文件夹 (How to copy backups from one folder to another using xp_cmdshell)
The following example will show how to copy several backups from one folder to another. We have several backups in the c:\backup path:
以下示例将显示如何将多个备份从一个文件夹复制到另一个文件夹。 我们在c:\ backup路径中有几个备份:
The following T-SQL statements will copy the files from c:\Backup to c:\Shared folder:
以下T-SQL语句会将文件从c:\ Backup复制到c:\ Shared文件夹:
xp_cmdshell 'copy c:\backup c:\shared';
The output will be this one:
输出将是以下内容:
As you can see, the copy cmd command is copying the files to the shared folder:
如您所见,copy cmd命令将文件复制到共享文件夹:
如何在SQL Server表的文件夹中存储文件的完整路径 (How to store full path of files in a folder in a SQL Server table)
The next example, will store in a SQL Server table all the paths of images stored in a specified file.
下一个示例将在SQL Server表中存储存储在指定文件中的图像的所有路径。
For example, I have the following images in the c:\image folder:
例如,我在c:\ image文件夹中有以下图像:
What I want is to store in a table the full paths like this:
我想要的是将完整的路径存储在表中,如下所示:
C:\images\1.jpg
C:\images\2.jpg
…etc.
C:\ images \ 1.jpg
C:\ images \ 2.jpg
…等等。
We will first store all the paths of the folder in a txt file named path:
我们将首先将文件夹的所有路径存储在名为path的txt文件中:
xp_cmdshell 'dir "c:\images" /s/b>c:\sql\path.txt';
The path.txt will now include all the image full paths:
现在,path.txt将包含所有图像完整路径:
We need a table to store the paths:
我们需要一个表来存储路径:
create table images(path varchar(80))
To import the information of the txt file of figure 6 into the table just created use the following sentences:
要将图6的txt文件的信息导入到刚刚创建的表中,请使用以下语句:
xp_cmdshell 'bcp AdventureWorKs2016CTP3.dbo.images IN c:\sql\path.txt -c -T'
Bcp is the command used to import from the path.txt to the table dbo.images in the AdventureWorks2016CTP3 Database. –T is used to connect using a Trust connection (the current Windows Authentication) and –c means to perform character type operations.
Bcp是用于从path.txt导入AdventureWorks2016CTP3数据库中的表dbo.images的命令。 -T用于使用信任连接(当前的Windows身份验证)进行连接,而-c表示执行字符类型操作。
If you do a select in the images table, you can verify that we could successfully import the data to SQL Server by doing a select:
如果在图像表中进行选择,则可以通过执行选择来验证我们是否可以成功将数据导入SQL Server:
select * from images
The SQL Statement will show the following results:
SQL语句将显示以下结果:
We are going to create a table with ID and a column with the path and image name columns:
我们将创建一个具有ID的表以及一个包含path和image name列的列:
CREATE TABLE [dbo].[pictures](
[id] [smallint] IDENTITY(1,1) NOT NULL,
[path] [varchar](max) NULL,
[imagename] [nchar](40) NULL,
CONSTRAINT [PK_pictures] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
The last steps is to import the data from the table images to the table pictures:
最后一步是将数据从表格图像导入表格图像:
insert into pictures (imagename,path) select replace(path,’c:\images\’,”) as imagename,path from images
插入图片(图像名称,路径),选择替换(路径,'c:\ images \',”)作为图像名称,图像的路径
You will now have the path and image name in a table:
现在,您将在表中具有路径和图像名称:
select * from pictures 从图片中选择*如何使用xp_cmdshell在T-SQL中执行PowerShell (How to execute PowerShell in T-SQL using xp_cmdshell)
PowerShell is used to create scripts and automate tasks in SQL Server, SharePoint, Window Server, Azure and several other Microsoft technologies. PowerShell is now available in Linux also. You can use loops, operators to create powerful scripts to automate tasks using PowerShell.
PowerShell用于在SQL Server,SharePoint,Window Server,Azure和其他几种Microsoft技术中创建脚本并自动执行任务。 现在,PowerShell在Linux中也可用。 您可以使用循环,运算符来创建功能强大的脚本,以使用PowerShell自动执行任务。
For example, this cmdlets allow you to copy backups from the c:\sql\source into the destination folder in PowerShell:
例如,此cmdlet允许您将备份从c:\ sql \ source复制到PowerShell中的目标文件夹中:
copy-item “C:\sql\source” -Destination “C:\sql\destination” -Recurse
复制项目“ C:\ sql \ source”-目标“ C:\ sql \ destination”-递归
The Destination folder will have now the source folder with the backups:
现在,“目标”文件夹将具有包含备份的源文件夹:
To run the same PowerShell cmdlet using the xp_cmdshell in SSMS, run the following T-SQL statements:
要在SSMS中使用xp_cmdshell运行相同的PowerShell cmdlet,请运行以下T-SQL语句:
xp_cmdshell ‘powershell -command “copy-item “C:\sql\source” -Destination “C:\sql\destination” -Recurse’
xp_cmdshell'powershell-命令“复制项目” C:\ sql \ source”-目标“ C:\ sql \ destination”-递归'
You are calling PowerShell and executing the command to copy all the files and folders from source to destination.
您正在调用PowerShell并执行命令以将所有文件和文件夹从源复制到目标。
The next example is to run a PowerShell script using xp_cmdshell. First, we will create a script named script.ps1 with the following content:
下一个示例是使用xp_cmdshell运行PowerShell脚本。 首先,我们将创建一个名为script.ps1的脚本,其内容如下:
File_name: script.ps1
copy-item “C:\sql\source” -Destination “C:\sql\destination” –Recurse
文件名:script.ps1
复制项目“ C:\ sql \ source”-目的地“ C:\ sql \ destination” –递归
To call a PowerShell script in xp_cmdshell, use the following sentences:
要在xp_cmdshell中调用PowerShell脚本,请使用以下语句:
xp_cmdshell ‘powershell -command “C:\\sql\\script.ps1″‘
xp_cmdshell and SQL Server PowerShell
xp_cmdshell'powershell-命令“ C:\\ sql \\ script.ps1”'
xp_cmdshell和SQL Server PowerShell
Sometimes, we need to connect to SQL PowerShell (SQLPS). The following example will show how to show database information in an html file using xp_cmdshell. The following example shows how to do it.
有时,我们需要连接到SQL PowerShell(SQLPS)。 以下示例将显示如何使用xp_cmdshell在html文件中显示数据库信息。 以下示例显示了如何执行此操作。
First, create a script named scriptsql.ps1 with the following content:
首先,创建一个名为scriptsql.ps1的脚本,其内容如下:
sqlserver:
cd sql\InstanceName\default\databases
get-childitem | ConvertTo-HTML | Out-File C:\sql\databases.htm
sqlserver:
cd sql \ InstanceName \ default \ databases
获得子项| ConvertTo-HTML | 外档C:\ sql \ databases.htm
The script is going to SQL Server and to the Instance name (change to the name of your SQL Server instance), default and databases.
该脚本将转到SQL Server,然后转到实例名称(更改为SQL Server实例的名称),默认值和数据库。
Get-childitem will show all the databases and convert-html out-file will convert the results to HTML format. The result will be saved in a file named databases.htm:
Get-childitem将显示所有数据库,而convert-html外文件会将结果转换为HTML格式。 结果将保存在名为databases.htm的文件中:
如何使用xp_cmdshell和sqlcmd在SSMS中连接到Azure (How to connect to Azure in SSMS using xp_cmdshell and sqlcmd)
Sometimes we need to call Azure from a local Database. Usually to connect to Azure, you need another connection. Xp_cmdshell and sqlcmd is an alternative to create complex stored procedures and scripts connecting to Azure and local databases.
有时我们需要从本地数据库调用Azure。 通常,要连接到Azure,您需要另一个连接。 Xp_cmdshell和sqlcmd是创建连接到Azure和本地数据库的复杂存储过程和脚本的替代方法。
We will first create a SQL Server in Azure Portal. In Azure Portal, go to More Services>SQL Servers:
我们首先将在Azure门户中创建一个SQL Server。 在Azure门户中,转到“其他服务”>“ SQL Server”:
Press Add to add a new SQL Server:
按添加以添加新SQL Server:
Add a new, a login and a password, create a new resource group and select a location and press Create:
添加新的,登录名和密码,创建新的资源组并选择一个位置,然后按创建:
Once created, wait a few minutes and Refresh the list of SQL Servers. In this example, the SQL Server name is sqlshack:
创建完成后,请等待几分钟,然后刷新SQL Server列表。 在此示例中,SQL Server名称为sqlshack:
To enable the local machine with SSMS, in Azure Portal, click the SQL Server, go to Firewall and press Add client IP. This will add the local machine with SSMS. Once added, press Save:
若要使用SSMS启用本地计算机,请在Azure门户中单击SQL Server,转到“防火墙”,然后按“添加客户端IP”。 这将使用SSMS添加本地计算机。 添加后,按保存:
Select the SQL Server created in Azure Portal and select properties. The Azure SQL Server name will be displayed:
选择在Azure门户中创建SQL Server,然后选择属性。 将显示Azure SQL Server名称:
In your local SQL Server, create a sql script named sqlscript.sql with the following content:
在本地SQL Server中,创建一个名为sqlscript.sql的sql脚本,其内容如下:
CREATE DATABASE SQLSHACKDB
创建数据库SQLSHACKDB
Create database will be used to create a database named SQLSHACKDB in Azure.
创建数据库将用于在Azure中创建一个名为SQLSHACKDB的数据库。
The following T-SQL sentences will be used to run a script in Azure to create an Azure Database:
以下T-SQL语句将用于在Azure中运行脚本以创建Azure数据库:
xp_cmdshell 'sqlcmd -U daniel -S sqlshack.database.windows.net -P "myPWD" -d master -i "c:\sql\sqlscript.sql"'
Where daniel and myPWD are the login and password created in Figure 13. Sqlshack.database.windows.net is the Server name displayed in Figure 16. We are calling the sqlcmd, which is the command line. We are connecting to the master database in Azure and receiving as input the sqlscript.sql script.
其中daniel和myPWD是图13中创建的登录名和密码。Sqlshack.database.windows.net是图16中显示的服务器名称。我们正在调用sqlcmd,这是命令行。 我们正在连接到Azure中的master数据库,并接收sqlscript.sql脚本作为输入。
If everything is fine, in Azure Portal, in SQL Databases, you will be able to see the Database SQLSHACKDB created:
如果一切正常,在Azure门户SQL数据库中,您将能够看到创建的数据库SQLSHACKDB:
结论 (Conclusion)
In this article, we explained how to copy data from one folder to another, how to copy the full path of files to a SQL table. We also learned how to run PowerShell and how to connect to Azure using xp_cmdshell.
在本文中,我们解释了如何将数据从一个文件夹复制到另一个文件夹,以及如何将文件的完整路径复制到SQL表。 我们还学习了如何运行PowerShell以及如何使用xp_cmdshell连接到Azure。
翻译自: https://www.sqlshack.com/use-xp-cmdshell-extended-procedure/