自动在不同的文件组中创建索引,编辑发布配置文件脚本
Automatically Create Indexes in Different Filegroup, edit Publish Profile Script
我需要一种方法来自动将聚集索引移动到一个文件组:ClusteredFilegroup,并将所有非聚集索引在创建 DDL 时移动到不同的文件组 NonClusteredFilegroup。我们有 sql 发布配置文件,它在每周部署下面创建类似的脚本。我如何利用 powershell 来执行此操作?
我想让powershell加词
ON [ClusteredFilegroup]
在每次 table 创建后
或 ON [NonClusteredFilegroup]
每个非聚集索引。
Powershell 应该能够读取原始脚本 (testscript.sql),并且 运行 对其进行文本编辑。
原文:
GO
CREATE TABLE [dbo].[Dim_Product] (
[DimProductId] INT IDENTITY (1, 1) NOT NULL,
[ProductName] VARCHAR(64) NOT NULL,
[ProductDescription] VARCHAR(64) NOT NULL,
[BeginDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
CONSTRAINT [PK_DimProductId] PRIMARY KEY CLUSTERED ([DimProductId] ASC)
);
GO
CREATE NONCLUSTERED INDEX [NCX_Product_ProductName]
ON [dbo].[Dim_Product]([ProductName] ASC);
GO
CREATE NONCLUSTERED INDEX [NCX_Product_BeginDate]
ON [dbo].[Dim_Product]([BeginDate] ASC);
GO
CREATE TABLE [dbo].[Dim_Customer] (
[DimCustomertId] INT IDENTITY (1, 1) NOT NULL,
[CustomerName] VARCHAR(64) NOT NULL,
[CustomerDescription] VARCHAR(64) NOT NULL,
[BeginDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
CONSTRAINT [PK_DimCustomerId] PRIMARY KEY CLUSTERED ([DimCustomerId] ASC)
);
GO
CREATE NONCLUSTERED INDEX [NCX_Customer_CustomerName]
ON [dbo].[Dim_Customer]([CustomerName] ASC);
GO
CREATE NONCLUSTERED INDEX [NCX_Customer_BeginDate]
ON [dbo].[Dim_Customer]([BeginDate] ASC);
目标:
CREATE TABLE [dbo].[Dim_Product] (
[DimProductId] INT IDENTITY (1, 1) NOT NULL,
[ProductName] VARCHAR(64) NOT NULL,
[ProductDescription] VARCHAR(64) NOT NULL,
[BeginDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
CONSTRAINT [PK_DimProductId] PRIMARY KEY CLUSTERED ([DimProductId] ASC)
) ON [ClusteredFilegroup];
GO
CREATE NONCLUSTERED INDEX [NCX_Product_ProductName]
ON [dbo].[Dim_Product]([ProductName] ASC) ON [NonClusteredFilegroup];
我正在尝试研究这些脚本:
Add text to every line in text file using PowerShell
Search a text file for specific word if found copy the entire line to new file in powershell
这应该可以解决问题:
$sql = Get-Content .\org.sql -Raw
$sql = $sql -replace '(?smi)(CREATE TABLE (.*?))\);',' ) ON [ClusteredFilegroup];'
$sql = $sql -replace '(?smi)(CREATE NONCLUSTERED INDEX (.*?))\);',') ON [NonClusteredFilegroup];'
$sql | Set-Content -Path .\new.sql
(?smi)
告诉替换语句匹配多行 (m) 并包括新行 (s) 并忽略大小写 (i)。
(.*?)
包括换行符(因此 (?smi)
),但不贪心(?
)。
我需要一种方法来自动将聚集索引移动到一个文件组:ClusteredFilegroup,并将所有非聚集索引在创建 DDL 时移动到不同的文件组 NonClusteredFilegroup。我们有 sql 发布配置文件,它在每周部署下面创建类似的脚本。我如何利用 powershell 来执行此操作?
我想让powershell加词
ON [ClusteredFilegroup]
在每次 table 创建后
或 ON [NonClusteredFilegroup]
每个非聚集索引。
Powershell 应该能够读取原始脚本 (testscript.sql),并且 运行 对其进行文本编辑。
原文:
GO
CREATE TABLE [dbo].[Dim_Product] (
[DimProductId] INT IDENTITY (1, 1) NOT NULL,
[ProductName] VARCHAR(64) NOT NULL,
[ProductDescription] VARCHAR(64) NOT NULL,
[BeginDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
CONSTRAINT [PK_DimProductId] PRIMARY KEY CLUSTERED ([DimProductId] ASC)
);
GO
CREATE NONCLUSTERED INDEX [NCX_Product_ProductName]
ON [dbo].[Dim_Product]([ProductName] ASC);
GO
CREATE NONCLUSTERED INDEX [NCX_Product_BeginDate]
ON [dbo].[Dim_Product]([BeginDate] ASC);
GO
CREATE TABLE [dbo].[Dim_Customer] (
[DimCustomertId] INT IDENTITY (1, 1) NOT NULL,
[CustomerName] VARCHAR(64) NOT NULL,
[CustomerDescription] VARCHAR(64) NOT NULL,
[BeginDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
CONSTRAINT [PK_DimCustomerId] PRIMARY KEY CLUSTERED ([DimCustomerId] ASC)
);
GO
CREATE NONCLUSTERED INDEX [NCX_Customer_CustomerName]
ON [dbo].[Dim_Customer]([CustomerName] ASC);
GO
CREATE NONCLUSTERED INDEX [NCX_Customer_BeginDate]
ON [dbo].[Dim_Customer]([BeginDate] ASC);
目标:
CREATE TABLE [dbo].[Dim_Product] (
[DimProductId] INT IDENTITY (1, 1) NOT NULL,
[ProductName] VARCHAR(64) NOT NULL,
[ProductDescription] VARCHAR(64) NOT NULL,
[BeginDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
CONSTRAINT [PK_DimProductId] PRIMARY KEY CLUSTERED ([DimProductId] ASC)
) ON [ClusteredFilegroup];
GO
CREATE NONCLUSTERED INDEX [NCX_Product_ProductName]
ON [dbo].[Dim_Product]([ProductName] ASC) ON [NonClusteredFilegroup];
我正在尝试研究这些脚本:
Add text to every line in text file using PowerShell
Search a text file for specific word if found copy the entire line to new file in powershell
这应该可以解决问题:
$sql = Get-Content .\org.sql -Raw
$sql = $sql -replace '(?smi)(CREATE TABLE (.*?))\);',' ) ON [ClusteredFilegroup];'
$sql = $sql -replace '(?smi)(CREATE NONCLUSTERED INDEX (.*?))\);',') ON [NonClusteredFilegroup];'
$sql | Set-Content -Path .\new.sql
(?smi)
告诉替换语句匹配多行 (m) 并包括新行 (s) 并忽略大小写 (i)。
(.*?)
包括换行符(因此 (?smi)
),但不贪心(?
)。