我有一个SQL作业列表,我想创建一个单元测试,用于评估作业中是否存在给定参数(作业名称)。
我试过了:
-- Test function
DECLARE @FinalResult NVARCHAR(30)
DECLARE @ResultTable TABLE (name VARCHAR(30))
INSERT INTO @ResultTable
SELECT [name]
FROM msdb.dbo.sysjobs
WHERE [name] = @Parameter_Name
-- Evaluation
SET @FinalResult = SELECT TOP 1 FROM @ResultTable
SELECT @TestResult = IIF(FinalResult IS NULL,0,1)
提前致谢。
答案 0 :(得分:3)
您可以使用EXISTS()
:
//substr(string,start,length)
substr("Hello Word", 0, 5);
substr($text, 0, 5);
substr($row['style-info'], 0, 5);
你也可以添加一个IF EXISTS(SELECT 1 FROM SomeWhere WHERE SomeCondition) --The '1' is of no importance... Just checking for any result...
BEGIN
Do what ever you need
END
块来对反面做出反应。
提示:您可以使用ELSE
...
答案 1 :(得分:0)
一种简单的方法就像是
Declare @JobExists INT
, @Parameter_Name SYSNAME = 'Backup Job';
IF EXISTS ( SELECT 1 FROM msdb.dbo.sysjobs WHERE [name] = @Parameter_Name)
BEGIN
SET @JobExists =1;
END
ELSE
BEGIN
SET @JobExists =0;
END
答案 2 :(得分:0)
可能是这样的:
DECLARE @Parameter_Name Varchar(MAX) = '' -- Job name
DECLARE @ResultTable TABLE (name VARCHAR(30),JobExists bit )
IF EXISTS( SELECT [name] FROM msdb.dbo.sysjobs WHERE [name] = @Parameter_Name)
BEGIN
INSERT INTO @ResultTable (name, JobExists )
Values (@Parameter_Name,1)
END
ELSE
BEGIN
INSERT INTO @ResultTable (name, JobExists )
Values (@Parameter_Name,0)
END