打破SQL创建表脚本到块?

时间:2018-02-15 13:38:20

标签: c# regex

有没有办法使用RegEx将MS SQL数据库脚本的CREATE TABLE块拆分为数组或集合?

我知道如何使用RegEx,但我似乎无法找到与之匹配的正确模式。

例如:

CREATE TABLE [Company](
    [CompanyName] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Address1] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Address2] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [City] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [State] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Zip] [numeric](18, 0) NOT NULL
) ON [PRIMARY]

GO

GO
SET ANSI_PADDING ON

GO
CREATE NONCLUSTERED INDEX [IX_Company] ON [Company]
(
    [CompanyName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
GO

GO
ALTER TABLE [Company] ADD  CONSTRAINT [DF_Company_CompanyName]  DEFAULT ('') FOR [CompanyName]
GO
ALTER TABLE [Company] ADD  CONSTRAINT [DF_Company_Address1]  DEFAULT ('') FOR [Address1]
GO
ALTER TABLE [Company] ADD  CONSTRAINT [DF_Company_Address2]  DEFAULT ('') FOR [Address2]
GO
ALTER TABLE [Company] ADD  CONSTRAINT [DF_Company_City]  DEFAULT ('') FOR [City]
GO
ALTER TABLE [Company] ADD  CONSTRAINT [DF_Company_State]  DEFAULT ('') FOR [State]
GO
ALTER TABLE [Company] ADD  CONSTRAINT [DF_Company_Zip]  DEFAULT ((0)) FOR [Zip]
GO

GO

-- =============================================
-- Counties TABLE
-- =============================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Counties](
    [UID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [CountyName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [StateUID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [SurTax] [money] NOT NULL
) ON [PRIMARY]

我只想把所有的CREATE TABLE块放到一个数组中(在CREATE TABLE和GO之间),所以我会得到一个只包含的数组:

CREATE TABLE [Company](
    [CompanyName] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Address1] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Address2] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [City] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [State] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Zip] [numeric](18, 0) NOT NULL
) ON [PRIMARY]

CREATE TABLE [Counties](
    [UID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [CountyName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [StateUID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [SurTax] [money] NOT NULL
) ON [PRIMARY]

2 个答案:

答案 0 :(得分:1)

我认为这个想法可能有些脆弱。 无论如何,我会尝试这样的事情:

CREATE TABLE \[.*?\) ON \[PRIMARY\]

检查online demo,您也可以在那里找到您的示例。 从该链接,让我复制粘贴说明:

  • CREATE TABLE字面匹配字符CREATE TABLE(case 敏感的)
  • \ [匹配字符[字面意思(区分大小写)
  • 。*?匹配任何角色
  • *?量词 - 在零和无限次之间匹配,尽可能少,根据需要扩展(懒惰)
  • \)字面匹配(区分大小写)
  • ON在字面上匹配字符ON(区分大小写)
  • \ [匹配字符[字面意思(区分大小写)
  • PRIMARY字面匹配字符PRIMARY(区分大小写)
  • \]匹配字符]字面意思(区分大小写)

要使其正常工作,您必须在代码中使用以下选项:

var RegexOptions = RegexOptions.Singleline;

这是Regex的单行选项,因此Dot匹配    换行符(否则你会停在换行符)。

答案 1 :(得分:1)

嗯嗯

String.Split("go", StringSplitOptions.IgnoreCase);

并采取....或类似的东西(在哪里)是你需要的

更进一步,内联altera ......

别忘了赞成我的家伙。