我可以
PROCEDURE dbo._ws_CallLogs_DeleteAll
OR
TABLE [dbo].[CachedPlan]
OR
VIEW [test].[MyView]
OR
FUNCTION [xyz].[ParseStringList]
作为输入
输出应为
案例1
string Type = PROCEDURE
string DBOName = dbo
string obejctName = _ws_CallLogs_DeleteAll
案例2
string Type = TABLE
string DBOName = dbo
string obejctName = CachedPlan
案例3
string Type = VIEW
string DBOName = test
string obejctName = MyView
案例4
string Type = FUNCTION
string DBOName = xyz
string obejctName = ParseStringList
怎么做?
答案 0 :(得分:2)
这是解析它的另一种方法:
static void Main(string[] args)
{
var parser = new TSql100Parser(true);
IList<ParseError> errors = new List<ParseError>();
using (TextReader r = new StreamReader("sql.txt"))
{
var result = parser.GetTokenStream(r, errors);
var results = result
.Select((i, index) => (i.TokenType == TSqlTokenType.Create) ? index : -1)
.Where(i => i >= 0)
.Select(i => result.Skip(i + 1).TakeWhile(j =>
(j.TokenType == TSqlTokenType.WhiteSpace
|| j.TokenType == TSqlTokenType.QuotedIdentifier
|| j.TokenType == TSqlTokenType.Identifier
|| j.TokenType == TSqlTokenType.Dot
|| j.TokenType == TSqlTokenType.Table
|| j.TokenType == TSqlTokenType.Procedure
|| j.TokenType == TSqlTokenType.View
|| j.TokenType == TSqlTokenType.Function
))
.Where(j => (j.TokenType != TSqlTokenType.WhiteSpace
&& j.TokenType != TSqlTokenType.Dot))
);
foreach (var item in results)
{
var type = item.Where(i => (i.TokenType != TSqlTokenType.QuotedIdentifier
|| i.TokenType != TSqlTokenType.Identifier));
Console.WriteLine("Type: {0}\tDbo: {1}\tObject: {2}",
type.First().Text,
type.Skip(1)
.Take(type.Count() - 2)
.Aggregate<TSqlParserToken, string>("",
(a, b) => a + ((a == "") ? "" : ".") + b.Text),
type.Last().Text);
}
}
}
您需要对以下命名空间的必需引用:
using Microsoft.Data.Schema.ScriptDom.Sql;
using Microsoft.Data.Schema.ScriptDom;
答案 1 :(得分:0)
如果输入的格式相同,则可以使用此正则表达式。
(?<type>PROCEDURE|TABLE|VIEW|FUNCTION)\s+\[?(?<name>\w+)\]?[.]\[?(?<object>\w+)\]?
现在你可以得到像这样的匹配
Match m=Regex.Match(input,regex,RegexOptions.IgnoreCase);
m.Groups["type"].Value;//type
m.Groups["name"].Value;//name
m.Groups["object"].Value;//object