May 14, 2012

sp_MSforce_drop_distribution_jobs (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_MSforce_drop_distribution_jobs(nvarchar @publisher
, nvarchar @publisher_db
, nvarchar @type)

MetaData:

 CREATE PROCEDURE sys.sp_MSforce_drop_distribution_jobs  
(
@publisher sysname,
@publisher_db sysname
,@type nvarchar(5) = N'both' -- 'merge' or 'tran' or 'both' to cleanup.
)
AS
BEGIN
DECLARE @retcode int,
@agent_id int,
@publisher_id smallint,
@publication sysname,
@subscriber sysname,
@subscriber_id smallint,
@subscriber_db sysname,
@subscription_type int
,@drop_tran_jobs bit
,@drop_merge_jobs bit

-- Security Check
IF ISNULL(IS_SRVROLEMEMBER('sysadmin'),0) = 0
BEGIN
RAISERROR(21089, 16, -1)
RETURN 1
END

IF LOWER(@type collate SQL_Latin1_General_CP1_CS_AS) IN (N'tran', N'both')
AND DatabasePropertyEx(@publisher_db, 'IsPublished') = 1
BEGIN
set @drop_tran_jobs = 1
END
else
begin
set @drop_tran_jobs = 0
end

IF LOWER(@type collate SQL_Latin1_General_CP1_CS_AS) IN (N'merge', N'both')
AND DatabasePropertyEx(@publisher_db, 'IsMergePublished') = 1
BEGIN
set @drop_merge_jobs = 1
END
else
BEGIN
set @drop_merge_jobs = 0
END

-- retrieve the publisher_id and if we do not
-- find one then we can just exit silently...
SELECT @publisher_id = server_id
FROM sys.servers
WHERE UPPER(name) = UPPER(@publisher)
IF @publisher_id IS NULL
BEGIN
RETURN 0
END

BEGIN TRANSACTION tr_drop_distribution_jobs
SAVE TRANSACTION tr_drop_distribution_jobs

-- -- -- -- -- -- -- -- -- -- -- -- -
-- DROP QREADER AGENTS
-- -- -- -- -- -- -- -- -- -- -- -- -

-- NOTE:
-- We do not drop qreader agents in the force
-- drop case since we have no way of knowing
-- when we can drop the qreader agent (i.e.
-- when the publisher is using it still...)


-- -- -- -- -- -- -- -- -- -- -- -- -
-- DROP SNAPSHOT AGENTS
-- -- -- -- -- -- -- -- -- -- -- -- -
DECLARE #cursorSnapshot CURSOR LOCAL FAST_FORWARD FOR
SELECT publication
FROM MSsnapshot_agents
WHERE publisher_id = @publisher_id
AND publisher_db = @publisher_db
AND ((@drop_tran_jobs = 1 and publication_type = 1)
or (@drop_merge_jobs = 1 and publication_type = 2)
)
FOR READ ONLY

OPEN #cursorSnapshot

FETCH #cursorSnapshot INTO @publication
WHILE @@FETCH_STATUS != -1
BEGIN
EXEC @retcode = sys.sp_MSdrop_snapshot_agent @publisher = @publisher,
@publisher_db = @publisher_db,
@publication = @publication
IF @@ERROR != 0 OR @retcode != 0
GOTO UNDO

FETCH #cursorSnapshot INTO @publication
END

CLOSE #cursorSnapshot
DEALLOCATE #cursorSnapshot

if (@drop_tran_jobs = 1)
begin
-- -- -- -- -- -- -- -- -- -- -- -- -
-- DROP LOGREADER AGENTS
-- -- -- -- -- -- -- -- -- -- -- -- -
DECLARE #cursorLogreader CURSOR LOCAL FAST_FORWARD FOR
SELECT publication
FROM MSlogreader_agents
WHERE publisher_id = @publisher_id
AND publisher_db = @publisher_db
FOR READ ONLY

OPEN #cursorLogreader

FETCH #cursorLogreader INTO @publication
WHILE @@FETCH_STATUS != -1
BEGIN
EXEC @retcode = sys.sp_MSdrop_logreader_agent @publisher = @publisher,
@publisher_db = @publisher_db,
@publication = @publication
IF @@ERROR != 0 OR @retcode != 0
GOTO UNDO

FETCH #cursorLogreader INTO @publication
END

CLOSE #cursorLogreader
DEALLOCATE #cursorLogreader

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -
-- DROP DISTRIBUTION AGENTS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -
DECLARE #cursorDistrib CURSOR LOCAL FAST_FORWARD FOR
SELECT publication,
subscriber_id,
subscriber_db,
subscription_type
FROM MSdistribution_agents
WHERE publisher_id = @publisher_id
AND publisher_db = @publisher_db
FOR READ ONLY

OPEN #cursorDistrib

FETCH #cursorDistrib INTO @publication, @subscriber_id, @subscriber_db, @subscription_type
WHILE @@FETCH_STATUS != -1
BEGIN
EXEC @retcode = sys.sp_MSdrop_distribution_agent @publisher_id = @publisher_id,
@publisher_db = @publisher_db,
@publication = @publication,
@subscriber_id = @subscriber_id,
@subscriber_db = @subscriber_db,
@subscription_type = @subscription_type
IF @@ERROR != 0 OR @retcode != 0
GOTO UNDO

FETCH #cursorDistrib INTO @publication, @subscriber_id, @subscriber_db, @subscription_type
END

CLOSE #cursorDistrib
DEALLOCATE #cursorDistrib
end
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -
-- DROP MERGE AGENTS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -
DECLARE #cursorReplmerg CURSOR LOCAL FAST_FORWARD FOR
SELECT msma.publication,
msma.subscriber_name,
msma.subscriber_db
FROM MSmerge_agents msma
WHERE publisher_id = @publisher_id
AND publisher_db = @publisher_db
AND @drop_merge_jobs = 1
FOR READ ONLY

OPEN #cursorReplmerg

FETCH #cursorReplmerg INTO @publication, @subscriber, @subscriber_db
WHILE @@FETCH_STATUS != -1
BEGIN
EXEC @retcode = sys.sp_MSdrop_merge_agent @publisher = @publisher,
@publisher_db = @publisher_db,
@publication = @publication,
@subscriber = @subscriber,
@subscriber_db = @subscriber_db
IF @@ERROR != 0 OR @retcode != 0
GOTO UNDO

FETCH #cursorReplmerg INTO @publication, @subscriber, @subscriber_db
END

CLOSE #cursorReplmerg
DEALLOCATE #cursorReplmerg

COMMIT TRANSACTION tr_drop_distribution_jobs

RETURN 0
UNDO:
ROLLBACK TRANSACTION tr_drop_distribution_jobs
COMMIT TRANSACTION

RETURN 1
END

sp_MSfast_delete_trans (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_MSfast_delete_trans()

MetaData:

 CREATE PROCEDURE sys.sp_MSfast_delete_trans  
as
begin
declare @retcode int

--
-- security check is done in sp_MSdrop_snapshot_dirs
--
EXEC @retcode = sys.sp_MSdrop_snapshot_dirs
if( @retcode <> 0 or @@error <> 0 )
return 1

truncate table MSrepl_commands
truncate table MSrepl_transactions
end

sp_MSgenerateexpandproc (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_MSgenerateexpandproc(int @tablenick
, nvarchar @procname)

MetaData:

 create procedure sys.sp_MSgenerateexpandproc(  
@tablenick int, @procname sysname
)
AS
begin
set nocount on

declare @immediate_child_partition_viewname nvarchar(130), @immediate_child_nickname int,
@join_unique_key int, @child_expand_proc sysname, @child_rgcol nvarchar(270), @child_has_col_tracking int

declare @partition_view_id int, @partition_view_name nvarchar(270), @pubid uniqueidentifier, @artid uniqueidentifier,
@retcode int, @artidstr nvarchar(40), @rgcol nvarchar(270), @objid int, @publication_number smallint
declare @command1 nvarchar(max), @command2 nvarchar(max), @command3 nvarchar(max), @command4 nvarchar(max)
declare @table_name nvarchar(130), @owner_qualified_table_name nvarchar(270), @immediate_child_table_name nvarchar(130),
@owner_qualified_immediate_child_table_name nvarchar(270), @immediate_child_objid int,
@join_filterclause nvarchar(2000), @child_join_colname nvarchar(130), @child_cannot_pre_exist bit, @parent_columns_unique bit,
@join_filter_id int
declare @gen_change_threshold int
declare @replnick binary(6)


-- Security Check
exec @retcode= sys.sp_MSreplcheck_publish
if @@error <> 0 or @retcode <> 0 return (1)

select @parent_columns_unique = 0

create table #tmpproccmd (phase int NOT NULL, step int identity NOT NULL, cmdtext nvarchar(max) collate database_default null)

select top 1 @artid=artid, @objid=objid
from dbo.sysmergearticles
where nickname = @tablenick

select @rgcol = quotename(name) from sys.columns where object_id = @objid and is_rowguidcol = 1

select @table_name = quotename(object_name(@objid)),
@owner_qualified_table_name = quotename(SCHEMA_NAME(schema_id)) + '.' + quotename(object_name(@objid))
from sys.objects
where object_id = @objid

select @gen_change_threshold = min(isnull(generation_leveling_threshold,0)) from dbo.sysmergepublications
where pubid in (select pubid from dbo.sysmergearticles where nickname = @tablenick)
select @command1 = '
create procedure dbo.'
+ quotename(@procname) + ' (@marker uniqueidentifier, @inherit_pastchanges_generation bigint, @parent_being_updated bit = 0, @trigger_type int = 1) as '

insert into #tmpproccmd (phase, cmdtext) values (1, @command1)

declare immediate_children CURSOR LOCAL FAST_FORWARD FOR
select distinct quotename(object_name(sma.objid)), sma.objid, sma.nickname, 'dbo.' + quotename(object_name(sma.partition_view_id)),
ssf.join_filterid, ssf.join_filterclause, ssf.join_unique_key, sma.expand_proc, sma.pubid, sma.column_tracking
from dbo.sysmergesubsetfilters ssf, dbo.sysmergepartitioninfoview sma where ssf.join_nickname = @tablenick
and sma.nickname = ssf.art_nickname
and sma.pubid = ssf.pubid
and (ssf.filter_type & 1) = 1
and sma.partition_view_id is not null
for read only

open immediate_children

fetch next from immediate_children into @immediate_child_table_name, @immediate_child_objid, @immediate_child_nickname,
@immediate_child_partition_viewname, @join_filter_id, @join_filterclause, @join_unique_key, @child_expand_proc, @pubid, @child_has_col_tracking

if (@@fetch_status <> -1)
begin
exec @retcode= sys.sp_MSgetreplnick @replnick = @replnick out
if @retcode <> 0
return 1

select @command3 = '
declare @child_marker uniqueidentifier
declare @child_rowcount int, @child_newgen bigint, @child_oldmaxversion int, @child_metadatarows_updated int, @cv varbinary(1), @replnick binary(6)
declare @lineage varbinary(311)
declare @retcode int
declare @dt datetime
declare @nickbin varbinary(8)
declare @reason bit


select @replnick = '
+ sys.fn_varbintohexstr(@replnick) + '

select @nickbin = @replnick + 0xFF

set @child_marker = newid()

select @dt = getdate()
'

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)
end

while (@@fetch_status <> -1)
begin
-- exec @retcode = sys.sp_MSis_joinfilter_based_on_PK_UQ_constraints @pubid = @pubid,
-- @join_filter_id = @join_filter_id,
-- @dri_based = @child_cannot_pre_exist output,
-- @unique_constraint_based = @parent_columns_unique output

-- if @@error <> 0 or @retcode <> 0
-- goto FAILURE

-- if @child_cannot_pre_exist = 1
-- begin
-- select @command3 = '
-- if @parent_being_updated = 1
-- begin '

-- insert into #tmpproccmd (phase, cmdtext) values(14, @command3)
-- end

select @owner_qualified_immediate_child_table_name =
quotename(SCHEMA_NAME(schema_id)) + '.' + quotename(object_name(object_id))
from sys.objects with (nolock) where object_id = @immediate_child_objid

select top 1 @publication_number = publication_number from dbo.sysmergepublications where pubid = @pubid

select top 1 @child_join_colname = name from sys.columns where object_id = @immediate_child_objid
and sys.fn_MSisfilteredcolumn(@join_filterclause, name, @immediate_child_objid) = 1

select @child_rgcol = quotename(name) from sys.columns where object_id = @immediate_child_objid and is_rowguidcol = 1

if @join_unique_key = 0 or @parent_columns_unique = 0
begin
select @command3 = '
select @child_rowcount = count(*) from '
+
@owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock)
where '
+ @child_rgcol + ' in
(select '
+ @immediate_child_table_name + '.' + @child_rgcol + '
from
dbo.MSmerge_contents with (rowlock), '

+ @owner_qualified_table_name + ' ' + @table_name + ' with (rowlock), '
+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock)
where dbo.MSmerge_contents.marker = @marker
and dbo.MSmerge_contents.tablenick = '
+ convert(nvarchar, @tablenick) + '
and dbo.MSmerge_contents.rowguid = '
+ @table_name + '.' + @rgcol + '
and ('
+ @join_filterclause +'))'
end
else
begin
select @command3 = '
select @child_rowcount = count(*) from
dbo.MSmerge_contents with (rowlock), '

+ @owner_qualified_table_name + ' ' + @table_name + ' with (rowlock), '
+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock)
where dbo.MSmerge_contents.marker = @marker
and dbo.MSmerge_contents.tablenick = '
+ convert(nvarchar, @tablenick) + '
and dbo.MSmerge_contents.rowguid = '
+ @table_name + '.' + @rgcol + '
and ('
+ @join_filterclause + ') '
end

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

-- touch the immediate children of deleted rows
select @command3 = '
if @child_rowcount > 0
begin
select @child_oldmaxversion= maxversion_at_cleanup from dbo.sysmergearticles
where nickname = '
+ convert(nvarchar, @immediate_child_nickname) + '

-- the code below will get an open generation for the child article
select @child_newgen = NULL
select top 1 @child_newgen = generation from dbo.MSmerge_genhistory with (rowlock, updlock, readpast)
where art_nick = '
+ convert(nvarchar,@immediate_child_nickname) + '
and genstatus = 0'

if @gen_change_threshold > 0
select @command3 = @command3 + '
and changecount <= ('
+ convert(nvarchar, @gen_change_threshold) + '- isnull(@child_rowcount,0))'
select @command3 = @command3 + '
if @child_newgen is NULL
begin
insert into dbo.MSmerge_genhistory with (rowlock)
(guidsrc, genstatus, art_nick, nicknames, coldate, changecount)
values (newid(), 0, '
+ convert(nvarchar,@immediate_child_nickname) + ', @nickbin, @dt, @child_rowcount)
select @child_newgen = @@identity
end'

if @gen_change_threshold > 0
select @command3 = @command3 + '
else
begin
-- do the update right away to change the changecount to include the rows that we just put in the generation
update dbo.MSmerge_genhistory with (rowlock)
set changecount = changecount + @child_rowcount
where generation = @child_newgen
end
'

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

select @command3 = '
if @retcode <> 0
return @retcode '


insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

if @join_unique_key = 0 or @parent_columns_unique = 0
begin
select @command3 = '
update MSmerge_contents1 with (rowlock) set generation = @child_newgen, partchangegen = @child_newgen, marker = @child_marker,
lineage = case when @trigger_type = 0 then lineage else { fn UPDATELINEAGE(MSmerge_contents1.lineage, @replnick, @child_oldmaxversion+1) } end
from dbo.MSmerge_contents MSmerge_contents1 with (rowlock)
where MSmerge_contents1.tablenick = '
+ convert (nvarchar, @immediate_child_nickname) + '
and MSmerge_contents1.rowguid in
(select '
+ @immediate_child_table_name + '.' + @child_rgcol + '
from
dbo.MSmerge_contents MSmerge_contents2 with (rowlock), '

+ @owner_qualified_table_name + ' ' + @table_name + ' with (rowlock), '
+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock)
where MSmerge_contents2.marker = @marker
and MSmerge_contents2.tablenick = '
+ convert(nvarchar, @tablenick) + '
and MSmerge_contents2.rowguid = '
+ @table_name + '.' + @rgcol + '
and ('
+ @join_filterclause + '))

select @child_metadatarows_updated = @@rowcount'

end
else
begin
select @command3 = '
update MSmerge_contents1 set generation = @child_newgen, partchangegen = @child_newgen, marker = @child_marker,
lineage = case when @trigger_type = 0 then lineage else { fn UPDATELINEAGE(MSmerge_contents1.lineage, @replnick, @child_oldmaxversion+1) } end
from
dbo.MSmerge_contents MSmerge_contents2 with (rowlock), '

+ @owner_qualified_table_name + ' ' + @table_name + ' with (rowlock), '
+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock),
dbo.MSmerge_contents MSmerge_contents1 with (rowlock)
where MSmerge_contents1.tablenick = '
+ convert (nvarchar, @immediate_child_nickname) + '
and MSmerge_contents1.rowguid = '
+ @immediate_child_table_name + '.' + @child_rgcol + '
and MSmerge_contents2.marker = @marker
and MSmerge_contents2.tablenick = '
+ convert(nvarchar, @tablenick) + '
and MSmerge_contents2.rowguid = '
+ @table_name + '.' + @rgcol + '
and ('
+ @join_filterclause + ')

select @child_metadatarows_updated = @@rowcount'

end

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

select @command3 = '
if @child_metadatarows_updated < @child_rowcount '

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

set @command3 = '
begin
set @lineage = case when @trigger_type = 0 then 0x0 else { fn UPDATELINEAGE(0x0, @replnick, @child_oldmaxversion+1) } end '


insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

if @child_has_col_tracking = 1
set @command3 = '
set @cv = 0xFF '

else
set @command3 = '
set @cv = NULL '


set @command3 = @command3 + '
insert into dbo.MSmerge_contents with (rowlock)
(tablenick, rowguid, generation, partchangegen, lineage, colv1, marker)
'

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

if @join_unique_key = 0 or @parent_columns_unique = 0
begin
set @command3 = '
select '
+ convert(nvarchar, @immediate_child_nickname) + ', ' + @immediate_child_table_name + '.' + @child_rgcol + ', @child_newgen, @child_newgen, @lineage, @cv, @child_marker
from '

+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock)
where '
+ @child_rgcol + ' in
(
select '
+ @immediate_child_table_name + '.' + @child_rgcol + '
from dbo.MSmerge_contents MSmerge_contents2 with (rowlock),
'
+ @owner_qualified_table_name + ' ' + @table_name + ' with (rowlock),
'
+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock)
where MSmerge_contents2.marker = @marker
and MSmerge_contents2.tablenick = '
+ convert(nvarchar, @tablenick) + '
and MSmerge_contents2.rowguid = '
+ @table_name + '.' + @rgcol + '
and ('
+ @join_filterclause + ')
)
and not exists
(select * from dbo.MSmerge_contents with (rowlock)
where tablenick = '
+ convert(nvarchar, @immediate_child_nickname) +'
and rowguid = '
+ @immediate_child_table_name + '.' + @child_rgcol + '
) '

end
else
begin
set @command3 = '
select '
+ convert(nvarchar, @immediate_child_nickname) + ', ' + @immediate_child_table_name +'.' + @child_rgcol + ', @child_newgen, @child_newgen, @lineage, @cv, @child_marker
from
dbo.MSmerge_contents MSmerge_contents2 with (rowlock), '

+ @owner_qualified_table_name + ' ' + @table_name + ' with (rowlock), '
+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name + ' with (rowlock)
where MSmerge_contents2.marker = @marker
and MSmerge_contents2.tablenick = '
+ convert(nvarchar, @tablenick) + '
and MSmerge_contents2.rowguid = '
+ @table_name + '.' + @rgcol + '
and ('
+ @join_filterclause + ')
and not exists (select * from dbo.MSmerge_contents with (rowlock)
where tablenick = '
+ convert(nvarchar, @immediate_child_nickname) + '
and rowguid = '
+ @immediate_child_table_name + '.' + @child_rgcol + ') '
end

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

select @command3 = '
end '

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

select @command3 = '
if @trigger_type <> 0
begin
delete dbo.MSmerge_current_partition_mappings with (rowlock) from
dbo.MSmerge_contents mc with (rowlock)
join dbo.MSmerge_current_partition_mappings with (rowlock)
on dbo.MSmerge_current_partition_mappings.tablenick = mc.tablenick
and mc.tablenick = '
+ convert(nvarchar(11), @immediate_child_nickname) + '
and dbo.MSmerge_current_partition_mappings.tablenick = '
+ convert(nvarchar(11), @immediate_child_nickname) + '
and dbo.MSmerge_current_partition_mappings.rowguid = mc.rowguid
and dbo.MSmerge_current_partition_mappings.publication_number = '
+ convert(nvarchar, @publication_number) + '
and mc.marker = @child_marker
end '

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

select @command3 = '
if @trigger_type <> 0
begin
insert into dbo.MSmerge_current_partition_mappings with (rowlock) (publication_number, tablenick, rowguid, partition_id)
select distinct '
+ convert(nvarchar, @publication_number) + ', mc.tablenick, mc.rowguid, v.partition_id
from dbo.MSmerge_contents mc with (rowlock)
JOIN '
+ @immediate_child_partition_viewname + ' v with (rowlock)
ON mc.tablenick = '
+ convert(nvarchar(11), @immediate_child_nickname) + '
and mc.rowguid = v.'
+ @child_rgcol + '
and mc.marker = @child_marker
end
else
begin
insert into dbo.MSmerge_current_partition_mappings with (rowlock) (publication_number, tablenick, rowguid, partition_id)
select distinct '
+ convert(nvarchar, @publication_number) + ', mc.tablenick, mc.rowguid, v.partition_id
from dbo.MSmerge_contents mc with (rowlock)
JOIN '
+ @immediate_child_partition_viewname + ' v with (rowlock)
ON mc.tablenick = '
+ convert(nvarchar(11), @immediate_child_nickname) + '
and mc.rowguid = v.'
+ @child_rgcol + '
and mc.marker = @child_marker
and v.partition_id in (select partition_id from dbo.MSmerge_current_partition_mappings cpm with (rowlock) JOIN
dbo.MSmerge_contents mc2 with (rowlock)
ON cpm.rowguid = mc2.rowguid
and mc2.marker = @marker)
where not exists (select * from MSmerge_current_partition_mappings with (readcommitted, rowlock, readpast) where
publication_number = '
+ convert(nvarchar, @publication_number) + ' and
tablenick = '
+ convert(nvarchar(11), @immediate_child_nickname) + ' and
rowguid = v.'
+ @child_rgcol + ' and
partition_id = v.partition_id)
end '


insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

select @command3 = '
if @inherit_pastchanges_generation <> -1
begin
if @parent_being_updated = 1
set @reason = 0
else
set @reason = 1 -- expanding for delete. --

insert into dbo.MSmerge_past_partition_mappings with (rowlock) (publication_number, tablenick, rowguid, partition_id, generation,reason)
select distinct '
+ convert(nvarchar, @publication_number) + ', ' + convert (nvarchar, @immediate_child_nickname) + ', ' + @immediate_child_table_name + '.' + @child_rgcol + ',
mpcpm.partition_id, @child_newgen, @reason
from
dbo.MSmerge_contents mc with (rowlock) ,
dbo.MSmerge_past_partition_mappings mpcpm with (rowlock) , '

+ @owner_qualified_table_name + ' ' + @table_name + ' with (rowlock) , '
+ @owner_qualified_immediate_child_table_name + ' ' + @immediate_child_table_name +' with (rowlock)
where mc.marker = @marker
and mc.tablenick = '
+ convert(nvarchar, @tablenick) + '
and mc.rowguid = '
+ @table_name + '.' + @rgcol + '
and mpcpm.publication_number = '
+ convert(nvarchar, @publication_number) + '
and mpcpm.generation = @inherit_pastchanges_generation
and mc.tablenick = mpcpm.tablenick
and mpcpm.tablenick = '
+ convert(nvarchar, @tablenick) + '
and mc.rowguid = mpcpm.rowguid
and ('
+ @join_filterclause + ')
end '


insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

if @child_expand_proc is not null and @child_expand_proc != ' '
begin
set @command3 = '
exec dbo.'
+ quotename(@child_expand_proc) + ' @marker = @child_marker, @inherit_pastchanges_generation = @child_newgen, @parent_being_updated = 1, @trigger_type = @trigger_type '
-- use parent_being_updated = 1 even if the originating parent was being inserted or deleted. this value
-- simply indicates that the current parent row was found, and we are updating its metadata.
insert into #tmpproccmd (phase, cmdtext) values(14, @command3)
end

select @command3 = '
end '

insert into #tmpproccmd (phase, cmdtext) values(14, @command3)

-- if @child_cannot_pre_exist = 1
-- begin
-- select @command3 = '
-- end '

-- insert into #tmpproccmd (phase, cmdtext) values(14, @command3)
-- end

fetch next from immediate_children into @immediate_child_table_name, @immediate_child_objid, @immediate_child_nickname,
@immediate_child_partition_viewname, @join_filter_id, @join_filterclause, @join_unique_key, @child_expand_proc, @pubid, @child_has_col_tracking
end

close immediate_children
deallocate immediate_children

select cmdtext from #tmpproccmd where cmdtext is not null order by phase, step
drop table #tmpproccmd

return 0

FAILURE:

close immediate_children
deallocate immediate_children

drop table #tmpproccmd

return 1
end

sp_MSenumsubscriptions (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_MSenumsubscriptions(nvarchar @subscription_type
, nvarchar @publisher
, nvarchar @publisher_db
, bit @reserved)

MetaData:

   
create procedure sys.sp_MSenumsubscriptions
(
@subscription_type nvarchar(5) = N'push',
@publisher sysname = N'%',
@publisher_db sysname = N'%',
@reserved bit = 0 -- not to be documented used by UI and sp_MSenumallsubscriptions
)
AS
begin
set nocount on

declare @dbname sysname
,@category int
,@proc nvarchar(200)
,@retcode int
,@cur_db sysname
,@type_value int
,@subscriptiontype_anon int

-- Security check. If not 'db_owner' return without querying
if is_member('db_owner') <> 1
return(0)
select @subscriptiontype_anon= 2
,@cur_db = db_name()
,@type_value = case
when (LOWER(@subscription_type)=N'push') then 0
when (LOWER(@subscription_type)=N'pull') then 1
when (LOWER(@subscription_type)=N'both') then 2
else 100 end
--
-- If we are being invoked by sp_MSenumallsubscriptions
-- we can skip creation of the temp table, for other cases
-- create the temp table
--
if (@reserved = 0)
begin
create table #tmp_subscriptions (
publisher sysname not null,
publisher_db sysname not null,
publication sysname null,
replication_type int not NULL,
subscription_type int not NULL,
last_updated datetime null,
subscriber_db sysname not null,
update_mode smallint null,
last_sync_status int null,
last_sync_summary sysname null,
last_sync_time datetime null
)
end

if object_id(N'dbo.sysmergesubscriptions') is not NULL
begin
-- return all subscriptions that this database is a subscriber to
-- suppress all subscriptions that originate from this database.
insert into #tmp_subscriptions
select p.publisher
,p.publisher_db
,p.name
,2
,s.subscription_type
,s.last_sync_date
,s.db_name
,cast(NULL as smallint)
,s.last_sync_status
,s.last_sync_summary
,s.last_sync_date
from dbo.sysmergepublications as p
join dbo.sysmergesubscriptions as s
on p.pubid = s.pubid
and s.pubid <> s.subid
and lower(s.subscriber_server) collate database_default = lower(@@servername) collate database_default
where (s.subscription_type=@type_value OR @type_value=2)
and ((@publisher = N'%') or (p.publisher = @publisher))
and ((@publisher_db = N'%') or ( p.publisher_db = @publisher_db))
and s.db_name = @cur_db
and p.pubid not in
(select pubid from dbo.sysmergepublications pubs where
lower(pubs.publisher) = LOWER(publishingservername()) AND
pubs.publisher_db = @cur_db)
end

if object_id(N'dbo.MSreplication_subscriptions') is not NULL
begin
if object_id(N'dbo.MSsubscription_properties') is not NULL and
object_id(N'dbo.MSsubscription_agents') is not NULL
begin
-- update_mode in MSreplication_subscriptions table is not reliable.
insert into #tmp_subscriptions
select s.publisher
,s.publisher_db
,s.publication
,case isnull(p.publication_type,0) when 0 then 0 else 1 end
,s.subscription_type
,s.time
,@cur_db
-- NOTE: For Queued case: we will always return 2/3 for the 4/5 case
-- since we overload update_mode based on queue_type
,case when isnull(a.update_mode,0) = 4 then 2
when isnull(a.update_mode,0) = 5 then 3
else isnull(a.update_mode,0) end
,a.last_sync_status
,a.last_sync_summary
,a.last_sync_time
from dbo.MSreplication_subscriptions s with (NOLOCK)
left outer join dbo.MSsubscription_agents a with (NOLOCK)
on (UPPER(s.publisher) = UPPER(a.publisher) and
s.publisher_db = a.publisher_db and
((s.publication = a.publication and
s.independent_agent = 1 and
a.publication <> N'ALL') or
(a.publication = N'ALL' and s.independent_agent = 0)) and
s.subscription_type = a.subscription_type)
left outer join dbo.MSsubscription_properties p with (NOLOCK)
on (UPPER(s.publisher) = UPPER(p.publisher) and
s.publisher_db = p.publisher_db and
s.publication = p.publication and
-- don't use property table for push.
s.subscription_type <> 0)
where
((@publisher = N'%') OR (UPPER(s.publisher) = UPPER(@publisher))) AND
((@publisher_db = N'%') or ( s.publisher_db = @publisher_db)) and
-- eliminate duplicate entries for agents using multiple subscription streams
s.transaction_timestamp = (SELECT MAX(t.transaction_timestamp) FROM dbo.MSreplication_subscriptions t
WHERE t.publisher = s.publisher
AND t.publisher_db = s.publisher_db
AND t.publication = s.publication) and
((s.subscription_type = 0 and @type_value = 0) or
-- For pull, return both pull and anonymous
(s.subscription_type <> 0 and @type_value = 1) or
@type_value = 2)
end
-- Property table does not exists.
else if object_id(N'dbo.MSsubscription_agents') is not NULL
begin
-- update_mode in MSreplication_subscriptions table is not reliable.
insert into #tmp_subscriptions
select s.publisher
,s.publisher_db
,s.publication
-- Property table does not exists. Say transactional.
,0
,s.subscription_type
,s.time, @cur_db
-- NOTE: For Queued case: we will always return 2/3 for the 4/5 case
-- since we overload update_mode based on queue_type
,case when isnull(a.update_mode,0) = 4 then 2
when isnull(a.update_mode,0) = 5 then 3
else isnull(a.update_mode,0) end
,a.last_sync_status
,a.last_sync_summary
,a.last_sync_time
from dbo.MSreplication_subscriptions s with (NOLOCK)
left outer join dbo.MSsubscription_agents a with (NOLOCK)
on (UPPER(s.publisher) = UPPER(a.publisher) and
s.publisher_db = a.publisher_db and
((s.publication = a.publication and
s.independent_agent = 1 and
a.publication <> N'ALL') or
(a.publication = N'ALL' and s.independent_agent = 0)) and
s.subscription_type = a.subscription_type)
where
((@publisher = N'%') OR (UPPER(s.publisher) = UPPER(@publisher))) AND
((@publisher_db = N'%') or ( s.publisher_db = @publisher_db)) and
-- eliminate duplicate entries for agents using multiple subscription streams
s.transaction_timestamp = (SELECT MAX(t.transaction_timestamp) FROM dbo.MSreplication_subscriptions t
WHERE t.publisher = s.publisher
AND t.publisher_db = s.publisher_db
AND t.publication = s.publication) and
((s.subscription_type = 0 and @type_value = 0) or
-- For pull, return both pull and anonymous
(s.subscription_type <> 0 and @type_value = 1) or
@type_value = 2)
end
-- Agents table does not exists.
else if object_id(N'dbo.MSsubscription_properties') is not NULL
begin
-- update_mode in MSreplication_subscriptions table is not reliable.
insert into #tmp_subscriptions
select s.publisher
,s.publisher_db
,s.publication
,case isnull(p.publication_type,0) when 0 then 0 else 1 end
,s.subscription_type
,s.time
,@cur_db
-- NOTE: For Queued case: we will always return 2/3 for the 4/5 case
-- since we overload update_mode based on queue_type
,case when isnull(s.update_mode,0) = 4 then 2
when isnull(s.update_mode,0) = 5 then 3
else isnull(s.update_mode,0) end
,NULL -- a.last_sync_status,
,NULL -- a.last_sync_summary,
,NULL -- a.last_sync_time
from dbo.MSreplication_subscriptions s with (NOLOCK)
left outer join dbo.MSsubscription_properties p with (NOLOCK)
on (UPPER(s.publisher) = UPPER(p.publisher) and
s.publisher_db = p.publisher_db and
s.publication = p.publication and
-- don't use property table for push.
s.subscription_type <> 0)
where
((@publisher = N'%') OR (UPPER(s.publisher) = UPPER(@publisher))) AND
((@publisher_db = N'%') or ( s.publisher_db = @publisher_db)) and
-- eliminate duplicate entries for agents using multiple subscription streams
s.transaction_timestamp = (SELECT MAX(t.transaction_timestamp) FROM dbo.MSreplication_subscriptions t
WHERE t.publisher = s.publisher
AND t.publisher_db = s.publisher_db
AND t.publication = s.publication) and
((s.subscription_type = 0 and @type_value = 0) or
-- For pull, return both pull and anonymous
(s.subscription_type <> 0 and @type_value = 1) or
@type_value = 2)
end
-- Both table does not exists
else
begin
-- update_mode in MSreplication_subscriptions table is not reliable.
insert into #tmp_subscriptions
select s.publisher
,s.publisher_db
,s.publication
,0
,s.subscription_type
,s.time
,@cur_db
-- NOTE: For Queued case: we will always return 2/3 for the 4/5 case
-- since we overload update_mode based on queue_type
,case when isnull(s.update_mode,0) = 4 then 2
when isnull(s.update_mode,0) = 5 then 3
else isnull(s.update_mode,0) end
,NULL -- a.last_sync_status,
,NULL -- a.last_sync_summary
,NULL -- a.last_sync_time
from dbo.MSreplication_subscriptions s with (NOLOCK)
where
((@publisher = N'%') OR (UPPER(s.publisher) = UPPER(@publisher))) AND
((@publisher_db = N'%') or ( s.publisher_db = @publisher_db)) and
-- eliminate duplicate entries for agents using multiple subscription streams
s.transaction_timestamp = (SELECT MAX(t.transaction_timestamp) FROM dbo.MSreplication_subscriptions t
WHERE t.publisher = s.publisher
AND t.publisher_db = s.publisher_db
AND t.publication = s.publication) and
((s.subscription_type = 0 and @type_value = 0) or
-- For pull, return both pull and anonymous
(s.subscription_type <> 0 and @type_value = 1) or
@type_value = 2)
end
end
--
-- If we are being invoked by sp_MSenumallsubscriptions
-- we can skip select of the temp table, for other cases
-- select from the temp table
--
if (@reserved = 0)
begin
select * from #tmp_subscriptions
end
--
-- all done
--
return (0)
end

sp_MSget_min_seqno (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_MSget_min_seqno(int @agent_id)

MetaData:

 CREATE PROCEDURE sys.sp_MSget_min_seqno  
(
@agent_id int,
@xact_seqno varbinary(16) OUTPUT
)
as
BEGIN
declare @publisher_db_id int,
@publisher sysname,
@publisher_dbname sysname,
@publication_id int,
@dbversion int,
@publication sysname,
@retcode int,
@IsP2PAgent bit,
@nosync_type tinyint

--
-- Security Check
--
if @agent_id is not null
begin
exec @retcode = sys.sp_MScheck_pull_access
@agent_id = @agent_id,
@agent_type = 0 -- distribution agent
if @@error <> 0 or @retcode <> 0
return (1)
end
else
begin
return (1)
end

-- exec sys.sp_MSispeertopeeragent @agent_id = @agent_id, @is_p2p = @IsP2PAgent output

-- if @@error <> 0 or @IsP2PAgent <> 0
-- begin
-- return (1)
-- end

select @publisher_db_id = publisher_database_id
from MSdistribution_agents
where id = @agent_id

if @publisher_db_id is null
return (1)

-- MSsubscriptions has a clustered unique key (agent_id, article_id). For one agent_id, there may
-- be multiple article_id. However, all rows with the same @agent_id should have a same value on nosync_type
select @nosync_type = nosync_type
from MSsubscriptions
where agent_id = @agent_id

if @nosync_type != 3
begin
-- In the case where we have a different nosync type, this tells
-- the distribution agent that can begin replication.
-- This should be removed in the future.
select @xact_seqno = 0x00000000000000000001
return (0)
end

select top 1 @xact_seqno = xact_seqno
from MSrepl_transactions msrt
where msrt.publisher_database_id=@publisher_db_id
order by xact_seqno asc

return(0)
END

--
-- Name: sp_MSget_repl_commands
--
-- Descriptions: Note: sp_MSget_repl_commands and sp_MSget_repl_count have very similar
-- queries. Any changes to one of them should be applied to all others.
--
-- Parameters: as defined in create statement
--
-- Returns: 0 - success
-- 1 - Otherwise
--
-- BUGBUG: If resource db support does not improve - this SP should be moved back
-- to instdist.sql for supportability. It accesses system catalogs
--
-- Security: Public procedure invoked via RPC. PAL check
-- Requires Certificate signature for catalog access
--
raiserror(15339,-1,-1,'sp_MSget_repl_commands')

sp_MSget_max_used_identity (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_MSget_max_used_identity(nvarchar @publisher
, nvarchar @publisher_db
, nvarchar @publication
, nvarchar @article)

MetaData:

 create procedure sys.sp_MSget_max_used_identity  
(
@publisher sysname,
@publisher_db sysname,
@publication sysname,
@article sysname,
@max_used numeric(38,0) output
)
as
set nocount on

declare @retcode tinyint
declare @publisher_id int

--
-- security check
-- only db_owner can execute this
--
if (is_member ('db_owner') != 1)
begin
raiserror(14260, 16, -1)
return (1)
end

-- Has to be executed from distribution database
if sys.fn_MSrepl_isdistdb (db_name()) != 1
begin
raiserror (21482, 16, -1, 'sp_MSget_max_used_identity', 'distribution')
return 1
end

select @publisher_id = srvid
from master..sysservers
where upper(srvname) = upper(@publisher)
if @publisher_id is NULL
begin
-- Publisher @publisher does not exist.
raiserror(21618, 16, -1, @publisher)
return 1
end

select top 1 @max_used = max_used from dbo.MSmerge_identity_range_allocations
where publisher_id = @publisher_id and
publisher_db = @publisher_db and
publication = @publication and
article = @article
order by time_of_allocation desc
if @@error<>0
return 1

return 0

sp_MSget_logicalrecord_lineage (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_MSget_logicalrecord_lineage(uniqueidentifier @pubid
, int @parent_nickname
, uniqueidentifier @parent_rowguid
, bigint @dest_common_gen)

MetaData:

 create procedure sys.sp_MSget_logicalrecord_lineage(@pubid uniqueidentifier, @parent_nickname int, @parent_rowguid uniqueidentifier, @dest_common_gen bigint)  
AS
begin

declare @metadata_type tinyint,
@logical_record_lineage varbinary(311),
@retcode smallint,
@procname nvarchar(270),
@num_dest_changed_rows int

exec @retcode = sys.sp_MSrepl_PAL_rolecheck @pubid = @pubid
if (@retcode <> 0) or (@@error <> 0)
return 1

select @procname= 'dbo.' + select_proc from dbo.sysmergearticles
where nickname = @parent_nickname and pubid=@pubid

set @metadata_type = 4
set @logical_record_lineage = NULL
set @num_dest_changed_rows = 0

-- check for row in base table
exec @retcode= @procname @maxschemaguidforarticle = NULL, @type=@metadata_type output, @rowguid=@parent_rowguid
if @@error <>0 or @retcode <> 0
return (1)

begin tran
if (@metadata_type = 3)
begin
select @metadata_type = 2, @logical_record_lineage = logical_record_lineage
from dbo.MSmerge_contents
with (serializable)
where tablenick = @parent_nickname and rowguid = @parent_rowguid
end
else
begin
-- row is not in base table; either it is in tombstone, or it is missing
set @metadata_type= 0

select @metadata_type = type, @logical_record_lineage = logical_record_lineage
from dbo.MSmerge_tombstone where
tablenick = @parent_nickname and rowguid = @parent_rowguid
end

select @num_dest_changed_rows = count(*) from dbo.MSmerge_contents mc join
(select distinct nickname from dbo.sysmergepartitioninfoview where logical_record_parent_nickname = @parent_nickname)
as smpi
on mc.tablenick = smpi.nickname
and mc.logical_record_parent_rowguid = @parent_rowguid
and (mc.generation > @dest_common_gen or mc.generation = 0)

select @num_dest_changed_rows = @num_dest_changed_rows + count(*) from dbo.MSmerge_tombstone mc
join (select distinct nickname from dbo.sysmergepartitioninfoview where logical_record_parent_nickname = @parent_nickname)
as smpi
on mc.tablenick = smpi.nickname
and mc.logical_record_parent_rowguid = @parent_rowguid
and (mc.generation > @dest_common_gen or mc.generation = 0)

commit tran

-- select out our result set
select @logical_record_lineage, @metadata_type, @num_dest_changed_rows

return (0)
end

Total Pageviews