有没有一种简单的方法可以将数据库 SQL 脚本文件分解为每个对象一个文件?

发布于 2024-10-03 10:00:15 字数 156 浏览 2 评论 0原文

我有 SQL Servers Tasks -> 的输出为一个文件中数据库中的所有表生成脚本...。

有没有一种简单的方法可以将单个 SQL 脚本分解为每个表一个文件?

显然,如果我可以在选择“每个对象文件”设置的情况下运行“生成脚本”任务,我就不会问这个问题。

I have the output of SQL Servers Tasks -> Generate Scripts... for all the tables in my database on one file.

Is there an easy way to break up the single SQL script into one file per table?

Obviously, if I could run the Generate Scripts task with the File per object setting selected, I wouldn't be asking this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2024-10-10 10:00:15

运行任务->生成脚本任务。在最终向导页面中,选择“脚本到文件”,然后选择“每个对象文件”。您必须为输出指定现有文件夹。

Run Task-> Generate Scripts task. In the final wizard page select "script to file" and select 'File per object'. You must specify an existing folder for the output.

枕花眠 2024-10-10 10:00:15

使用 PowerShell。
如果不知道 SQL 文件的格式,这可能有效,也可能无效。它至少应该给你一个起点。它开始写入“table.sql”文件,但后来将该文件重命名为tablename.sql

1.   $lineNumber = 0
2.   $inputFile = "test.sql"
3.   foreach ($line in Get-Content $inputFile) {
4.     if ($line -match "create table") { 
5.        $w = [regex]::Replace($line, "^.+\.\[(\w+)\].+$", '$1')
6.        $outFile = "$w.sql"
7.        $lineNumber = 1
8.     }
9.     if (($line -match "use \[dbaInventory\]") -and ($lineNumber -gt 0)) {
10.        Move-Item -LiteralPath "table.sql" -Destination $outFile
11.     }
12.     $line | Out-file -FilePath table.sql -Append
13.  }
14.  Move-Item -LiteralPath "table.sql" -Destination $outFile

将第 2 行更改为您当前拥有的任何组合 sql 文件,并在第 9 行中查找适合的任何数据库名称你的脚本。

这是我用于测试的“test.sql”文件的示例。

USE [dbaInventory]
GO

/****** Object:  Table [dbo].[tableOne]    Script Date: 11/22/2010 12:28:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tableOne](
    [colA] [smallint] NULL,
    [colB] [char](1) NULL,
    [colC] [decimal](10, 2) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

USE [dbaInventory]
GO

/****** Object:  Table [dbo].[tableTwo]    Script Date: 11/22/2010 12:28:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tableTwo](
    [col_A] [char](1) NULL,
    [col_B] [decimal](10, 2) NULL,
    [col_C] [smallint] NULL,
 CONSTRAINT [Pk_tableTwo] PRIMARY KEY CLUSTERED 
(
    [col_A] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Using PowerShell.
Without knowing the format of your SQL file this may or may not work. It should at least give you a starting point. It starts writing to the "table.sql" file but later renames the file as tablename.sql

1.   $lineNumber = 0
2.   $inputFile = "test.sql"
3.   foreach ($line in Get-Content $inputFile) {
4.     if ($line -match "create table") { 
5.        $w = [regex]::Replace($line, "^.+\.\[(\w+)\].+$", '$1')
6.        $outFile = "$w.sql"
7.        $lineNumber = 1
8.     }
9.     if (($line -match "use \[dbaInventory\]") -and ($lineNumber -gt 0)) {
10.        Move-Item -LiteralPath "table.sql" -Destination $outFile
11.     }
12.     $line | Out-file -FilePath table.sql -Append
13.  }
14.  Move-Item -LiteralPath "table.sql" -Destination $outFile

Change line 2 for whatever combined sql file you currently have and line 9 to look for whatever database name is appropriate to your script.

Here is a sample of the "test.sql" file I used for testing.

USE [dbaInventory]
GO

/****** Object:  Table [dbo].[tableOne]    Script Date: 11/22/2010 12:28:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tableOne](
    [colA] [smallint] NULL,
    [colB] [char](1) NULL,
    [colC] [decimal](10, 2) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

USE [dbaInventory]
GO

/****** Object:  Table [dbo].[tableTwo]    Script Date: 11/22/2010 12:28:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tableTwo](
    [col_A] [char](1) NULL,
    [col_B] [decimal](10, 2) NULL,
    [col_C] [smallint] NULL,
 CONSTRAINT [Pk_tableTwo] PRIMARY KEY CLUSTERED 
(
    [col_A] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文