这一篇,我们来实现PowerShell 2.0在SQL Server中的一个应用,是批量执行SQL脚本。在接下来的内容,将使用到下面的命令或类库。
- Sort-Object
- out-null
- Write-Error
- $_
- System.IO.DirectoryInfo
- Microsoft.SqlServer.Management.Common.ServerConnection
创建测试环境
为了更能说明PowerShell脚本的应用,我们这里创建个测试环境,模拟一个要升级的SQL脚本文件;首先,要创建两个数据库『TestingDB01』和『TestingDB02』:
use master
Go
if db_id('TestingDB01') Is Not null
Drop Database TestingDB01
Go
Create Database TestingDB01
Go
if db_id('TestingDB02') Is Not null
Drop Database TestingDB02
Go
Create Database TestingDB02
Go
Go
if db_id('TestingDB01') Is Not null
Drop Database TestingDB01
Go
Create Database TestingDB01
Go
if db_id('TestingDB02') Is Not null
Drop Database TestingDB02
Go
Create Database TestingDB02
Go