May 25, 2012

sp_MSreset_attach_state (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_MSreset_attach_state(nvarchar @publisher
, nvarchar @publisher_db
, nvarchar @publication
, int @subscription_type)

MetaData:

 create procedure sys.sp_MSreset_attach_state   
(
@publisher sysname, -- publishing server name
@publisher_db sysname, -- publishing database name. If NULL then same as current db
@publication sysname, -- publication name,
@subscription_type int
)
AS
BEGIN
set nocount on
declare @retcode int
,@agent_id int

--
-- Security Check
--
EXEC @retcode = sys.sp_MSreplcheck_subscribe
IF @@ERROR <> 0 or @retcode <> 0
RETURN(1)
--
-- For non independent agent publications
--
if @publication is null or @publication = ''
select @publication = 'ALL'
--
-- complete the operation in a transaction
--
begin transaction
--
-- Processing specific to Queued updating subscriptions
--
select @agent_id = id
from dbo.MSsubscription_agents
where UPPER(publisher) = UPPER(@publisher)
and publisher_db = @publisher_db
and publication = @publication
and subscription_type = @subscription_type
and update_mode in (2,3,4,5)
if (@agent_id is not null)
begin
--
-- reinitialize the queue for each article in this subscription
--
declare @artid int
declare #hc_article CURSOR LOCAL FORWARD_ONLY for
select artid
from dbo.MSsubscription_articles
where agent_id = @agent_id
open #hc_article
fetch #hc_article into @artid
while (@@fetch_status != -1)
begin
--
-- we cannot have an NULL article id
--
if (@artid IS NULL)
begin
raiserror(20046, 16, 1)
goto failure
end
--
-- reset the queue
--
exec @retcode = sys.sp_MSreset_queue @publisher, @publisher_db, @publication, @artid
IF (@retcode != 0 or @@ERROR != 0)
begin
raiserror(21465, 16, 1, 'sp_MSreset_queue')
goto failure
end
--
-- fetch next article
--
fetch #hc_article into @artid
end -- while cursor
close #hc_article
deallocate #hc_article
end -- end processing queued subscription
--
-- Update the attach state
--
update MSsubscription_agents
set attach_state = 0
where UPPER(publisher) = UPPER(@publisher)
and publisher_db = @publisher_db
and publication = @publication
and subscription_type = @subscription_type
if (@@error != 0)
goto failure
--
-- All done
-- commit the transaction
--
commit transaction
return (0)
failure:
--
-- Rollback transaction
--
if (@@trancount > 0)
rollback transaction
return (1)
END

No comments:

Post a Comment

Total Pageviews