什么是从脚本中解析对象名称的最有效方法?

时间:2013-05-14 09:26:14

标签: c# parsing

我有一些输入

CREATE PROCEDURE dbo._ws_CallLogs_DeleteAll
(
    @UserId uniqueidentifier 
)
AS

OR

/*========== Script Analyzed by Sql Eye  on 11/30/2012 2:55:12 PM =======

  *====================== Total warnings : 0  =================================== */ 






SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_CachedPlan_cached_plan_Job_id]') 
                AND parent_object_id = OBJECT_ID(N'[dbo].[CachedPlan]'))
ALTER TABLE [dbo].[CachedPlan] DROP CONSTRAINT [FK_CachedPlan_cached_plan_Job_id]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CachedPlan]') AND TYPE IN (N'U'))
DROP TABLE [dbo].[CachedPlan]
GO
CREATE TABLE [dbo].[CachedPlan](
    [cached_plan_id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [cached_plan_Job_id] [int] NOT NULL,
    [dbId] [int] NOT NULL,
    [dbname] [varchar](100) NOT NULL,
    [plan_type] [varchar](50) NOT NULL,
    [objId] [int] NOT NULL,
    [objname] [varchar](100) NOT NULL,
    [sql_batch] [varchar](max) NOT NULL,

我需要挑选

PROCEDURE dbo._ws_CallLogs_DeleteAll

OR

TABLE [dbo].[CachedPlan]

最有效的方法是什么?

1 个答案:

答案 0 :(得分:2)

使用正则表达式:

Match m = Regex.Match(inputString, @"CREATE\s+(?<obj>.+?)\s*\(", RegexOptions.Singleline);
string objectName = m.Groups["obj"].Value;