USE [msdb]
GO

-- =============================================
-- Job: Backup DIFERENCIAL - a cada 6 horas
-- =============================================
EXEC sp_add_job @job_name = N'Backup DIFERENCIAL - Levelcode-LevelOS';

EXEC sp_add_jobstep
    @job_name      = N'Backup DIFERENCIAL - Levelcode-LevelOS',
    @step_name     = N'Executar Backup DIFERENCIAL',
    @command       = N'EXEC [Levelcode-LevelOS].[dbo].[sp_BackupDiferencial]',
    @database_name = N'Levelcode-LevelOS';

EXEC sp_add_schedule
    @schedule_name      = N'A cada 6 horas',
    @freq_type          = 4,        -- diário
    @freq_interval      = 1,
    @freq_subday_type   = 8,        -- a cada X horas
    @freq_subday_interval = 6,      -- 6 horas
    @active_start_time  = 080000;   -- começa às 08:00

EXEC sp_attach_schedule
    @job_name      = N'Backup DIFERENCIAL - Levelcode-LevelOS',
    @schedule_name = N'A cada 6 horas';

EXEC sp_add_jobserver
    @job_name = N'Backup DIFERENCIAL - Levelcode-LevelOS';
GO

-- Confirma os jobs criados
SELECT 
    j.name        AS Job,
    s.name        AS Schedule,
    s.active_start_time,
    CASE s.freq_subday_type
        WHEN 8 THEN CAST(s.freq_subday_interval AS VARCHAR) + 'h em ' + CAST(s.freq_subday_interval AS VARCHAR) + 'h'
        ELSE 'Diário fixo'
    END AS Frequencia
FROM msdb.dbo.sysjobs j
JOIN msdb.dbo.sysjobschedules js ON j.job_id = js.job_id
JOIN msdb.dbo.sysschedules s     ON js.schedule_id = s.schedule_id
WHERE j.name LIKE '%Levelcode%'
ORDER BY j.name;