April 17, 2012

sp_cleanup_log_shipping_history (Transact-SQL MetaData) Definition

Please note: that the following source code is provided and copyrighted by Microsoft and is for educational purpose only.
The meta data is from an SQL 2012 Server.

I have posted alot more, find the whole list here.

Goto Definition or MetaData

Definition:

sys.sp_cleanup_log_shipping_history(uniqueidentifier @agent_id
, tinyint @agent_type)

MetaData:

 create procedure sys.sp_cleanup_log_shipping_history   
(
@agent_id uniqueidentifier -- primary/secondary ID
,@agent_type tinyint -- 0 = Backup, 1 = Copy, 2 = Restore
)
as
begin
set nocount on
declare @retcode int
,@agent_idstring sysname
,@history_retention_period int
,@curdate_utc datetime
,@monitor_server sysname
,@monitor_server_security_mode int
--
-- security check
--
exec @retcode = sys.sp_MSlogshippingsysadmincheck
if (@retcode != 0 or @@error != 0)
return 1
--
-- must be invoked from master db
--
if (db_name() != N'master')
begin
raiserror(5001, 16,-1)
return 1
end
--
-- validate agent_type
--
if (@agent_type not in (0,1,2))
begin
raiserror(21055, 16, -1, '@agent_type','sp_MSadd_log_shipping_error_detail')
return 1
end
--
-- validate agent
--
if (sys.fn_MSvalidatelogshipagentid(@agent_id, @agent_type) = 0)
begin
select @agent_idstring = cast(@agent_id as sysname)
raiserror(32016, 16, 1, @agent_idstring, @agent_type)
return 1
end
--
-- get monitor server information
--
if (@agent_type = 0)
begin
select @monitor_server = pd.monitor_server
,@monitor_server_security_mode = pd.monitor_server_security_mode
,@history_retention_period = mp.history_retention_period
from msdb.dbo.log_shipping_primary_databases as pd join msdb.dbo.log_shipping_monitor_primary as mp
on pd.primary_id = mp.primary_id
where pd.primary_id = @agent_id
end
else
begin
--
-- there can be multiple secondaries for a secondary primary
-- with different history retention periods - choose the highest
--
select @monitor_server = s.monitor_server
,@monitor_server_security_mode = s.monitor_server_security_mode
,@history_retention_period = max(ms.history_retention_period)
from msdb.dbo.log_shipping_secondary as s join msdb.dbo.log_shipping_monitor_secondary as ms
on s.secondary_id = ms.secondary_id
where s.secondary_id = @agent_id
group by s.monitor_server, s.monitor_server_security_mode
end
--
-- cleanup now
--
select @curdate_utc = getutcdate()
exec @retcode = sys.sp_MSprocesslogshippingretentioncleanup
@agent_id = @agent_id
,@agent_type = @agent_type
,@monitor_server = @monitor_server
,@monitor_server_security_mode = @monitor_server_security_mode
,@history_retention_period = @history_retention_period
,@curdate_utc = @curdate_utc
if (@retcode != 0 or @@error != 0)
goto UNDO
--
-- all done
--
return 0

UNDO:
return 1
end

No comments:

Post a Comment

Total Pageviews