存储过程以根据服务器名称运行作业

时间:2014-07-16 19:59:31

标签: sql sql-server powershell stored-procedures jobs

我有一个4节点的全局集群。在每个节点上,我有一个从SQL作业调用的存储过程,该作业使用PowerShell获取服务器名称。 (PowerShell脚本不能在作业中工作,但可以作为sql脚本工作。)根据名称,我调用一对备份作业,一个用于备份数据库,另一个用于事务日志。其中两台服务器位于北方,两台服务器位于东方。两个数据库备份作业似乎都运行了两次。请让我知道我需要更改什么才能让这个存储过程只调用一次作业。非常感谢提前!!!

这是我得到的错误:

Message
Executed as user: Domain\User1. SQLServerAgent Error: Request to run job Backup_DB - FULL - North (from User Domain\User1) refused because the job is already running from a request by User Domain\User1. [SQLSTATE 42000] (Error 22022).  The step failed.

这是给我错误的存储过程:

USE [msdb]
GO
/****** Object:  StoredProcedure [dbo].[sp_Server_BACKUP]    Script Date: 07/16/2014 15:44:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[sp_Server_BACKUP]
AS 
    SET NOCOUNT ON ;


Declare @spconfigValues table
(
[name] varchar(1500),
minimum int,
maximum int,
config_value int,
run_value int
)
insert into @spconfigValues
execute sp_configure
------------------------------------------------------
Declare @rv int
set @rv = (select run_value from @spconfigValues where [name] = 'xp_cmdshell')

If @rv is null
Begin 
set @rv = (select convert(int,value_in_use) from sys.configurations where [name] = 'xp_cmdshell') -- gets the server name
End

if @rv = 0
Begin 
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
End
-----------------------------------------------------------------------------------------------------------------
Declare @Server_Name_tbl table
(
[server_name] varchar(50)
)

insert into @Server_Name_tbl
execute xp_cmdshell 'powershell.exe "$(Get-WmiObject Win32_Computersystem).name"'


if @rv = 0
Begin 
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_cmdshell', 0
RECONFIGURE WITH OVERRIDE
End

--select * from @server_name_tbl

Declare @servernm varchar(50)

SET @servernm = (select lower(server_name) from @Server_Name_tbl where server_name is not null) --sets the server name

--select @servernm
-------------------------------------------------------------------------------------------------------------------
--Based on the server name call the correct set of jobs

IF @servernm = 'server_1234N1'
    Begin
        EXEC msdb.dbo.sp_start_job N'Backup_T_LOGS - North';        
        WAITFOR DELAY '00:02:00';
        EXEC msdb.dbo.sp_start_job N'Backup_DB - FULL - North';
    End

IF @servernm = 'server_1234N2'
    Begin
        EXEC msdb.dbo.sp_start_job N'Backup_T_LOGS - North';        
        WAITFOR DELAY '00:02:00';
        EXEC msdb.dbo.sp_start_job N'Backup_DB - FULL - North';
    End

IF @servernm = 'server_2345N1'
    Begin   
        EXEC msdb.dbo.sp_start_job N'Backup_T_LOGS - East';
        WAITFOR DELAY '00:02:00';
        EXEC msdb.dbo.sp_start_job N'Backup_DB - FULL - East';
    End

IF @servernm = 'server_2345N2'  
    Begin   
        EXEC msdb.dbo.sp_start_job N'Backup_T_LOGS - East';
        WAITFOR DELAY '00:02:00';
        EXEC msdb.dbo.sp_start_job N'Backup_DB - FULL - East';
    End

1 个答案:

答案 0 :(得分:1)

我遇到的问题是,在每个NetBackup作业中,我创建了相同的.bch文件。因此,当t-logs备份作业启动时,它将创建一个名为Backup123.bch的文件。然后,当完整备份作业运行时,它将尝试创建相同的.bch文件并提供错误"the job is already running."

最重要的是,一旦我更改了Veritas要创建的每个作业的.bch文件名,该过程就会按预期工作。