0

我想使用 while 循环并从其他表中逐一迭代值,例如“server1name”、“server2name”、“server3name”,下面是与从 sp 生成 sql 作业相关的代码,这是在 4 个不同的环境中一次创建它我想在 sp 下面使用 while 循环

--use msdb;
--IF  EXISTS (SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'TORODO1')
--EXEC sp_delete_job @job_name = N'TORODO1' , @delete_unused_schedule=1
--DROP PROCEDURE dbo.JobCreation
--sp_helptext 'dbo.JobCreation';
CREATE PROCEDURE dbo.JobCreation
AS
BEGIN
DECLARE @JobName VARCHAR(30)= 'TORODO1';

EXEC    msdb.dbo.sp_add_job @job_name= @jobName, 
        @enabled=0,                                             
        @notify_level_eventlog=0, 
        @notify_level_email=0, 
        @notify_level_netsend=0, 
        @notify_level_page=0, 
        @delete_level=0, 
        @description=N'No description available.', 
        @category_name=N'[Uncategorized (Local)]', 
        @owner_login_name=N'CORP\StaSQLExec';

EXEC    msdb.dbo.sp_add_jobstep @job_name= @jobName, @step_name=N'DUMMY_1', 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @on_success_action=3, 
        @on_success_step_id=0, 
        @on_fail_action=2, 
        @on_fail_step_id=0, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'SSIS', 
        --@database_name=N'NPKSTADAT11',  
        @database_name=@server1name, --server2name, server3name-- here want to apply while loop
        @command=N'/SQL "\Dummy" /SERVER NPKSTADAT11 /CONFIGFILE "E:\SSIS_Package_Repository\Dummy\PackageDependencies_20120801\Dummy.dtsconfig" /CHECKPOINTING OFF /REPORTING E', 
        @flags=0;

EXEC    msdb.dbo.sp_add_jobstep @job_name=@jobName, @step_name=N'DUMMY_2', 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @on_success_action=3, 
        @on_success_step_id=0, 
        @on_fail_action=2, 
        @on_fail_step_id=0, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'SSIS', 
        --@database_name=N'NPKSTADAT11',  
        @database_name=N'master',
        @command=N'/SQL "\Dummy" /SERVER NPKSTADAT11 /CONFIGFILE "E:\SSIS_Package_Repository\Dummy\PackageDependencies_20120801\Dummy.dtsconfig" /CHECKPOINTING OFF /REPORTING E',
        @flags=0;

EXEC    msdb.dbo.sp_add_jobschedule @job_name=@jobName, @name=N'Dummy_schedule_2', 
        @enabled=1, 
        @freq_type=8, 
        @freq_interval=63, 
        @freq_subday_type=1, 
        @freq_subday_interval=0, 
        @freq_relative_interval=0, 
        @freq_recurrence_factor=1, 
        @active_start_date=20120831, 
        @active_end_date=99991231, 
        @active_start_time=0, 
        @active_end_time=235959 
        --@schedule_uid=N'08468e56-ffbc-45b0-a3ac-a812bcf8401c'

EXEC    msdb.dbo.sp_add_jobschedule @job_name=@jobName, @name=N'DUMMY_step_1_schedule', 
        @enabled=1, 
        @freq_type=4, 
        @freq_interval=1, 
        @freq_subday_type=1, 
        @freq_subday_interval=0, 
        @freq_relative_interval=0, 
        @freq_recurrence_factor=0, 
        @active_start_date=20120831, 
        @active_end_date=99991231, 
        @active_start_time=230000, 
        @active_end_time=235959 
        --@schedule_uid=N'ef28ae48-cf83-46c7-b83e-973df6adc62d'
EXEC    msdb.dbo.sp_add_jobserver @job_name=@jobName, @server_name = N'(local)'
    END 

        Go
4

2 回答 2

1

您将需要研究Cursors来驱动您的 while 循环。就像是

declare myCursor cursor for
  select servername from ServerTable

open myCursor
fetch next from myCursor into @serverName

while @@FETCH_STATUS = 0
begin
   ... do the work ...
   fetch next from myCursor into @serverName
end

close myCursor
deallocate myCursor
于 2012-08-31T15:43:59.207 回答
0

以下是如何使用 while 循环。

SELECT  Servername ,
    0 AS Status 
 INTO    #temp1
 FROM    ServerTable
 DECLARE @variable VARCHAR(50)

WHILE EXISTS ( SELECT TOP 1
                    NULL
           FROM     #temp1 AS T
           WHERE    T.Status = 0 ) 

BEGIN

    UPDATE TOP ( 1 )
            #temp1
    SET     Status = 1
    WHERE   #temp1.Status = 0

    SELECT TOP 1
            @variable = Servername
    FROM    #temp1 AS T
    WHERE   T.Status = 0

...Do your work...

    DELETE  FROM #temp1
    WHERE   #temp1.Status = 1


END

`

于 2012-08-31T15:54:12.977 回答