Powershell:删除 SQL 发布配置文件行索引

Powershell: Remove SQL Publish Profile Lines Indexes

我想使用 PowerShell 可能是 Regex 从脚本中删除非聚集索引。背景:我们在数据库中有大量数据,并且希望防止 1 小时的索引创建时间用于创建或更改非聚集索引。如何使用 PowerShell 执行此操作? PowerShell 将接受输入脚本并进行相应的编辑。

发布配置文件脚本:

Create table dbo.Test(TestId int, ColumnA int, ColumnB int)

GO
CREATE NONCLUSTERED INDEX [ncx_Test_ColumnA]
    ON [dbo].[Test]([ColumnA] ASC);


GO
PRINT N'Creating [ncx_Test_ColumnA]...';


GO
CREATE NONCLUSTERED INDEX [ncx_Test_ColumnB]
    ON [dbo].[Test]([ColumnB] ASC);


GO
PRINT N'Creating [ncx_Test_ColumnB]...';


GO
create procedure dbo.TestSelect1
as select 1

在上面的脚本中,我想删除'Create Nonclustered Index Lines, (and added bonus, not required) would be removed the corresponding Print statements, keep everything else, tables and stored procedures.

仅删除这些行,保留所有其他内容:

CREATE NONCLUSTERED INDEX [ncx_Test_ColumnA]
    ON [dbo].[Test]([ColumnA] ASC);


PRINT N'Creating [ncx_Test_ColumnA]...';


CREATE NONCLUSTERED INDEX [ncx_Test_ColumnB]
    ON [dbo].[Test]([ColumnB] ASC);


PRINT N'Creating [ncx_Test_ColumnB]...';

powershell 的好资源:试图弄清楚

有人可以对此进行代码审查吗?似乎有效,

$sql = Get-Content C:\Users\TestUser\Desktop\indextext\original.txt -Raw
$sql = $sql -replace '(?smi)(CREATE NONCLUSTERED INDEX (.*?))\);',''
$sql | Set-Content -Path C:\Users\TestUser\Desktop\indextext\new.txt