我正在使用SMO为SQL Server编写代理作业脚本,并且生成的脚本字符串具有我要从我正在存储的最终版本中删除的参数和值。我想要查看的脚本部分是添加到作业的计划,其中包含@schedule_uid参数以及与之关联的GUID。我想完全从脚本中删除它。
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name='Job Name',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=4,
@freq_subday_interval=10,
@freq_relative_interval=1,
@freq_recurrence_factor=0,
@active_start_date=20150119,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2'
我要替换的部分如下:
, \r\n\t\t@schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2'
这样最终的字符串是:
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name='Job Name',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=4,
@freq_subday_interval=10,
@freq_relative_interval=1,
@freq_recurrence_factor=0,
@active_start_date=20150119,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959
我尝试了各种我在网上阅读的东西组合,但似乎无法让它取代甚至匹配。我知道guid匹配的正则表达式是:
\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b'
我试图将其添加到许多内容中,并认为以下正则表达式可以正常工作,但无法弄清楚我做错了什么或丢失了
@", \r\n\t\t@schedule_uid=N'\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b'"
@", \r\n\t\t@schedule_uid=N'[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}'"
@", \r\n\t\t\b@schedule_uid=N'[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}'\b"
我不是在寻找解决方案,因为我想知道自己错过了什么。我一直在阅读regular-expressions.info网站一段时间,而且我通常能够找到正确的正则表达式,但这已经让我难以忍受了几天。
修改
它并不总是最后一项,并且不能保证只在脚本中出现一次,因为一个作业可以有多个具有不同@ schedule_uid的日程表,并且我想在不循环的情况下摆脱所有这些日程。这就是我选择Regex进行操作的原因。它还需要删除前一个参数行末尾的逗号,以使代码保持语法正确。
答案 0 :(得分:2)
答案 1 :(得分:0)
你去了:
@ schedule_uid = N' [\ W] {8} - [\ W] {4} - [\ W] {4} - [\ W] {4} - [\ W] {12}& #39;
创建和测试答案 2 :(得分:0)
有点复杂,但有效。
string test = "EXEC...";
var lines = test.Split(new char [] { ',' }).ToList();
lines = lines.Select((line, index) =>
{
var indexof = line.IndexOf("@schedule_uid");
if (indexof > -1)
{
if (index == 0)
{
return line.Substring(0, indexof);
}
else
{
return null;
}
}
return line + ",";
})
.Where(line => line != null)
.ToList();
test = string.Join(string.Empty, lines);
答案 3 :(得分:0)
假设尽可能少,只使用基本的字符串操作。
string exec = ...
int i = exec.IndexOf("@schedule_uid");
while (i > -1)
{
int j = i;
//Find the previous comma
while (exec[i] != ',')
i--;
//Find the end, next line, or next comma
while (j < exec.Length && exec[j] != '\r' && exec[j] != ',')
j++;
exec = exec.Remove(i, j - i);
i = exec.IndexOf("@schedule_uid");
}
我故意忽略无循环要求,支持简单的代码。测试与此... ...
string exec = @"
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule, @schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2', @job_id=@jobId, @name='Job Name',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=4,
@freq_subday_interval=10,
@freq_relative_interval=1,
@freq_recurrence_factor=0,
@schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2',
@active_start_date=20150119,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2'";