我有一个变量,其中包含一个表中的两个值(使用DECLARE @JobExecutionDate INT
--current date
SET @JobExecutionDate = CAST(CONVERT(VARCHAR(8),GETDATE(),112) AS INT)
--check if there were any errors first
IF EXISTS (SELECT h.instance_id
FROM MSDB.DBO.SYSJOBHISTORY h
INNER JOIN MSDB.DBO.SYSJOBS j ON h.job_id = j.job_id
INNER JOIN MSDB.DBO.SYSJOBSTEPS s ON j.job_id = s.job_id AND h.step_id = s.step_id
WHERE h.run_status = 0 AND h.run_date = @JobExecutionDate AND j.name = 'YourJobName')
BEGIN
DECLARE @Title VARCHAR(50)
SET @Title = CONCAT(CONVERT(VARCHAR(12), CAST(GETDATE() AS DATE), 109), ' Job Error Email')
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Your Database Mail Profile',
@recipients = 'ExampleEmail@Domain.com',
@query = 'SELECT j.[name] AS JobName,
s.step_name AS StepName,
h.run_date AS RunDate,
h.run_time AS RunTime,
h.sql_severity As ErrorSeverity,
h.message AS ErrorMessage
FROM MSDB.DBO.SYSJOBHISTORY h
INNER JOIN MSDB.DBO.SYSJOBS j ON h.job_id = j.job_id
INNER JOIN MSDB.DBO.SYSJOBSTEPS s ON j.job_id = s.job_id AND h.step_id = s.step_id
WHERE h.run_status = 0 AND h.run_date = CAST(CONVERT(VARCHAR(8),GETDATE(),112) AS INT) AND j.name = ''YourJobName''
',
@query_result_no_padding = 1,
@subject = @Title ;
END
)
-expandproperty
我进行检查以确保$variable = "value1,value2"
没有返回任何NULL /空值
$variable
但是由于某些原因,即使应该if(($variable.Split(",") | Where { $_ -match '^\S'}).count -lt 1)
{write "no value!"}
else
{write $variable}
仅检查空格的第一个字符/索引,它也会输出“
没有价值
”条件结果...应该输出
value1,value2
相反...
不仅如此,如果表列的值为'^\S'
,则它无法识别它,而是尝试输出第二个条件NULL
那是为什么?
答案 0 :(得分:1)
另一种检查“ null或empty”的方法是使用[string]IsNulOrEmpty()
静态方法。像这样的东西
$Var1 = "value1,value2"
$Var2 = ''
$Var3 = $Null
$Var1, $Var2, $Var3 |
ForEach-Object {
$_
[string]::IsNullOrEmpty($_)
}
输出...
value1,value2
False
True
True
请注意,最后一个$Null
不会产生新行,而第二项[空字符串]会产生新行。
答案 1 :(得分:0)
\S
是非空格。
\s
是空格。