我有一个作为字符串的 SQL 查询,我正在尝试使用正则表达式对其进行查找和替换。
var s = "SELECT ([Document].[ArtifactID]) FROM [Document]
(NOLOCK) WHERE [Document].[AccessControlListID_D]
IN (1) AND ((NOT EXISTS(SELECT CodeArtifactID
FROM CodeArtifact (NOLOCK) WHERE AssociatedArtifactID =
[Document].[ArtifactID] AND CodeArtifact.CodeTypeID = 1000112))) ";
//line breaks added for readability
为了使查询工作,我需要指定 [Document] 和 CodeArtifact 的架构。我有:
var pattern = "FROM\\s(?<table>\\S+)";
var replacePattern = "FROM EDDSDBO.${table}";
var v = Regex.Replace(s, pattern, replacePattern);
System.Console.WriteLine(v);
但我回来的字符串看起来像:
SELECT ([Document].[ArtifactID])
FROM [Document] (NOLOCK)
WHERE [Document].[AccessControlListID_D] IN (1) AND
((NOT EXISTS(SELECT CodeArtifactID
FROM EDDSDBO.CodeArtifact (NOLOCK)
WHERE AssociatedArtifactID = [Document].[ArtifactID] AND CodeArtifact.CodeTypeID = 1000112)))
CodeArtifact 已被替换,但 [Document] 未被替换。我肯定错过了什么。有什么想法吗?