June 4, 2012

sp_MStablespace (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_MStablespace(nvarchar @name
, int @id)

MetaData:

   
create procedure sys.sp_MStablespace
@name nvarchar(517), @id int = null
as
declare @rows int, @datasizeused int, @indexsizeused int, @pagesize int
declare @dbname nvarchar(128)
select @dbname = db_name()

if (@id is null)
select @id = id from dbo.sysobjects where id = object_id(@name) and (OBJECTPROPERTY(id, N'IsTable') = 1)
if (@id is null)
begin
RAISERROR (15009, -1, -1, @name, @dbname)
return 1
end

-- rows --
SELECT @rows = convert(int, rowcnt)
FROM dbo.sysindexes
WHERE indid < 2 and id = @id

if (object_id('master.dbo.sp_MSSQLDMO90_version') is not null)
BEGIN

-- data --
SELECT @datasizeused =
SUM(CASE WHEN a.type <> 1 THEN a.used_pages
WHEN p.index_id < 2 THEN a.data_pages
ELSE 0
END)
FROM sys.indexes as i
JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id
JOIN sys.allocation_units as a ON a.container_id = p.partition_id
where i.object_id = @id

-- index --
SELECT @indexsizeused =
sum(isnull(sidx.used,0)-isnull(sidx.dpages,0))
FROM dbo.sysindexes sidx
WHERE sidx.indid < 2 and sidx.id = @id
END
ELSE
BEGIN
-- data --
SELECT @datasizeused =
(SELECT sum(dpages)
FROM dbo.sysindexes
WHERE indid < 2 and id = @id)
+
(SELECT isnull(sum(used), 0)
FROM dbo.sysindexes
WHERE indid = 255 and id = @id)

-- Do not consider 2 < indid < 255 rows, those are nonclustered indices, and the space used by them are included by indid = 0(table) --
-- or indid = 1(clustered index) already. indid = 0(table) and = 1(clustered index) are mutual exclusive --
-- index --
SELECT @indexsizeused =
(SELECT sum(used)
FROM dbo.sysindexes
WHERE indid in (0, 1, 255) and id = @id)
- @datasizeused
END

-- Pagesize on this server (sysindexes stores size info in pages) --
select @pagesize = v.low / 1024 from master..spt_values v where v.number=1 and v.type=N'E'

select Rows = @rows, DataSpaceUsed = @datasizeused * @pagesize, IndexSpaceUsed = @indexsizeused * @pagesize

sp_MStran_ddlrepl (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_MStran_ddlrepl(xml @EventData
, int @procmapid)

MetaData:

 create procedure sys.sp_MStran_ddlrepl   
(
@EventData xml
,@procmapid int
)
AS
begin
set nocount on
declare @retcode int

-- validate the procmapid
if @procmapid not in (1,2,3,4)
begin
raiserror(15021, 16, -1, '@procmapid')
goto FAILURE
end
-- if transactional replication is not enabled for this db, don't do anything
if (sys.fn_MSrepl_istranpublished (db_name(),0) != 1)
return 0

declare @object_name sysname
,@object_owner sysname
,@qual_object_name nvarchar(512) -- qualified 2-part-name
,@objid int
,@objecttype varchar(32)
,@encrypted nvarchar(32)
,@pass_through_scripts nvarchar(max)
,@eventDoc int
,@dbname sysname
,@targetobject nvarchar(51)
,@debug_print bit

if object_id('MSrepl_debug_DDL') is not null
set @debug_print = 1
else
set @debug_print = 0

set @targetobject = N''

select @object_name = event_instance.value('ObjectName[1]', 'sysname')
,@object_owner = event_instance.value('SchemaName[1]', 'sysname')
,@objecttype = event_instance.value('ObjectType[1]', 'varchar(32)')
,@encrypted = event_instance.value('(TSQLCommand/SetOptions/@ENCRYPTED)[1]', 'nvarchar(32)')
,@pass_through_scripts = event_instance.value('(TSQLCommand/CommandText)[1]', 'nvarchar(max)')
,@targetobject = event_instance.value('TargetObjectName[1]', 'nvarchar(512)')
FROM @EventData.nodes('/EVENT_INSTANCE') as R(event_instance)

if @debug_print = 1
select 'stage' = 'xmlnoderefs : '
, '@object_name' = @object_name
, '@object_owner' = @object_owner
, '@objecttype' = @objecttype
, '@encrypted' = @encrypted
, '@pass_through_scripts' = @pass_through_scripts
, '@targetobject' = @targetobject

-- If the object being manipulated is a database level trigger that is owned by replication, raise error
if exists (select * from sys.triggers where name = @object_name and parent_class = 0 and @objecttype = 'TRIGGER' and @object_name in (N'tr_MStran_altertable', N'tr_MStran_alterview', N'tr_MStran_alterschemaonly', N'tr_MStran_altertrigger'))
begin
raiserror(21598, 16, 1)
goto FAILURE
end

-- If the object being manipulated is a database level trigger that is not owned by replication, return immediately
if exists (select * from sys.triggers where name = @object_name and parent_class = 0 and @objecttype = 'TRIGGER' and @object_name not in (N'tr_MStran_altertable', N'tr_MStran_alterview', N'tr_MStran_alterschemaonly', N'tr_MStran_altertrigger'))
return 0

select @qual_object_name = quotename(@object_owner) + N'.' + quotename(@object_name)
select @objid = object_id(@qual_object_name)
select @dbname = db_name()

if @debug_print = 1
select 'stage' = 'quotename : '
, '@qual_object_name' = @qual_object_name
, '@objid' = @objid
, '@dbname' = @dbname

-- can not alter to 'with encrypted' if object is published
-- return immediately if object is not published
if UPPER(@encrypted) = N'TRUE'
begin
if (UPPER(@objecttype) != 'TRIGGER' and
exists (SELECT * FROM dbo.sysextendedarticlesview WHERE objid = @objid)
or UPPER(@objecttype) = 'TRIGGER' and
exists (select * from sysarticles a join sys.objects o on a.objid = o.parent_object_id
where o.object_id = @objid and cast (a.schema_option as int) & 256 = 256) )
begin
raiserror(21815, 16, 1, @qual_object_name)
goto FAILURE
end
else
return 0
end

-- stored procedures published as "proc execution" article cannot contain
-- table value parameters (Note: proc exec articles are only listed in
-- dbo.sysarticles
if exists (select *
from sys.parameters sp
inner join dbo.sysarticles a
on sp.object_id = a.objid
where sp.system_type_id = 243
and sp.object_id = @objid
and a.type in (0x08, 0x18))
begin
raiserror(25023,16,-1)
goto FAILURE
end
select @pass_through_scripts = sys.fn_replgetparsedddlcmd(@pass_through_scripts
,N'ALTER'
,@objecttype
,@dbname
,@object_owner
,@object_name
,@targetobject)

if @debug_print = 1
select 'stage' = 'sys.fn_replgetparsedddlcmd output : '
, '@pass_through_scripts' = @pass_through_scripts

-- sys.fn_replgetparsedddlcmd will return empty string if DDL contains
-- syntax that we don't currently handle (after Katmai DDL
-- improvement)
-- It will also handle a duplicate trigger in the case of "alter table switch"
if @pass_through_scripts = N''
return 0

-- deal with alternate dest table/owner for alter table inside sp_MStran_altertable
if UPPER(@objecttype) != N'TABLE' and UPPER(@objecttype) != N'TRIGGER'
begin
select @pass_through_scripts = N'ALTER ' + @objecttype + N' '
+ @qual_object_name + N' '
+ @pass_through_scripts
end

-- if object is not published, don't do anything, unless SWITCH is the following keyword
if (UPPER(@objecttype) != 'TRIGGER' and exists (SELECT * FROM dbo.sysextendedarticlesview WHERE objid = @objid)
or UPPER(@objecttype) = 'TRIGGER' and exists (select * from sysarticles a join sys.objects o on a.objid = o.parent_object_id
where o.object_id = @objid )
or UPPER(@objecttype) = N'TABLE' and LEFT(@pass_through_scripts, LEN(N'SWITCH')) = N'SWITCH')
begin
declare @proc_name sysname

-- Security Check
EXEC @retcode = sys.sp_MSreplcheck_publish
IF @@ERROR <> 0 or @retcode <> 0
goto FAILURE

select @proc_name = case
when (@procmapid = 1) then 'sys.sp_MStran_altertable'
when (@procmapid = 2) then 'sys.sp_MStran_alterview'
when (@procmapid = 3) then 'sys.sp_MStran_alterschemaonly'
when (@procmapid = 4) then 'sys.sp_MStran_altertrigger'
end

exec @retcode = @proc_name @qual_object_name, @objid, @pass_through_scripts, @targetobject
if @retcode <>0 or @@ERROR<>0
goto FAILURE
end
return 0
FAILURE:
rollback tran
return 1
end

sp_MStestbit (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_MStestbit(varbinary @bm
, smallint @coltotest)

MetaData:

 create procedure sys.sp_MStestbit  
@bm varbinary(128),
@coltotest smallint
AS
declare @word smallint
declare @bit smallint
declare @mask binary(2)
declare @mval int
declare @oldword binary(2)

if @coltotest < 1 return 0

SELECT @word = 1 + FLOOR((@coltotest -1)/16)

SELECT @bit = (@coltotest -1) % 16

SELECT @mval = POWER(2, @bit)
SELECT @mask = convert( binary(2), unicode( substring( convert( nchar(2), convert( binary(4), @mval ) ), 2, 1 ) ) )

SELECT @oldword = convert( binary(2), SUBSTRING( convert( nvarchar(64),@bm), @word, 1) )
IF @oldword IS NULL return 0

return convert( smallint, @oldword ) & convert( smallint, @mask )

sp_MStablerefs (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_MStablerefs(nvarchar @tablename
, nvarchar @type
, nvarchar @direction
, nvarchar @reftable
, int @flags)

MetaData:

   
create procedure sys.sp_MStablerefs
@tablename nvarchar(517),
@type nvarchar(20) = N'actualtables',
@direction nvarchar(20) = N'primary',
@reftable nvarchar(517) = null,
@flags int = 0
as
-- tablename: table whose references are being evaluated --
-- type : '[actual | all][tables | keys | keycols]'; all candidates, or only those actually referenced --
-- direction: look for references from @tablename to 'primary' table(s), or to @tablename from 'foreign' table(s) --
-- reftable : limit scope to this table, if non-null --
-- -- @flags added for DaVinci uses. If the bit isn't set, use 6.5 -- --
-- -- sp_MStablerefs '%s', null, 'both' -- --

create table #sprefs (
id int NOT NULL, -- id of reftable --
constid int NULL, -- id of key --
referenced bit NOT NULL -- well, is it? --
)

-- @flags is for daVinci --
if (@flags is null)
select @flags = 0

if (@tablename = N'?') begin
PRINT N''
PRINT N'sp_MStablerefs:'
PRINT N'@tablename nvarchar(257), -- table whose references are being evaluated -- '
PRINT N'@type nvarchar(20) = [actualtables], -- [[actual | all][tables | keys | keycols]]; all candidates, or only those actually referenced -- '
PRINT N'@direction nvarchar(20) = [primary], -- look for references from @tablename to [primary] or to @tablename from [foreign], or [both] -- '
PRINT N'@reftable nvarchar(257) = null -- limit scope to this table, if non-null -- '
return 0
end

if (lower(@direction) = N'both') begin
select
N'PK_Table' = PKT.name,
N'FK_Table' = FKT.name,
N'Constraint' = object_name(r.constid),
c.status,
cKeyCol1 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey1)),
cKeyCol2 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey2)),
cKeyCol3 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey3)),
cKeyCol4 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey4)),
cKeyCol5 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey5)),
cKeyCol6 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey6)),
cKeyCol7 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey7)),
cKeyCol8 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey8)),
cKeyCol9 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey9)),
cKeyCol10 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey10)),
cKeyCol11 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey11)),
cKeyCol12 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey12)),
cKeyCol13 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey13)),
cKeyCol14 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey14)),
cKeyCol15 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey15)),
cKeyCol16 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey16)),
cRefCol1 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey1)),
cRefCol2 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey2)),
cRefCol3 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey3)),
cRefCol4 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey4)),
cRefCol5 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey5)),
cRefCol6 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey6)),
cRefCol7 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey7)),
cRefCol8 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey8)),
cRefCol9 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey9)),
cRefCol10 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey10)),
cRefCol11 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey11)),
cRefCol12 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey12)),
cRefCol13 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey13)),
cRefCol14 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey14)),
cRefCol15 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey15)),
cRefCol16 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey16)),
N'PK_Table_Owner' = schema_name(sysoPKT.schema_id),
N'FK_Table_Owner' = schema_name(sysoFKT.schema_id),
N'DeleteCascade' = OBJECTPROPERTY( r.constid, N'CnstIsDeleteCascade'),
N'UpdateCascade' = OBJECTPROPERTY( r.constid, N'CnstIsUpdateCascade')
from dbo.sysreferences r, dbo.sysconstraints c, dbo.sysobjects PKT, sys.all_objects sysoPKT, dbo.sysobjects FKT, sys.all_objects sysoFKT
where r.constid = c.constid and (@tablename is null or
(r.rkeyid = object_id(@tablename) or r.fkeyid = object_id(@tablename))) and PKT.id = sysoPKT.object_id and FKT.id = sysoFKT.object_id
and PKT.id = r.rkeyid and FKT.id = r.fkeyid
return 0
end -- @direction = 'both' --

declare @id int, @refid int
select @id = object_id(@tablename), @refid = object_id(@reftable)
if (@tablename is not null and @id is null) begin
RAISERROR (15001, -1, -1, @tablename)
return 1
end
if (@reftable is not null and @refid is null) begin
RAISERROR (15001, -1, -1, @reftable)
return 1
end

declare @dotables bit, @doall bit, @doprimary bit, @docols bit
select @dotables = case when (@type like N'allt%' or @type like N'actualt%') then 1 else 0 end,
@doall = case when (@type like N'all%') then 1 else 0 end,
@doprimary = case when (@direction like N'p%') then 1 else 0 end,
@docols = case when (@type like N'%keycol%') then 1 else 0 end

-- If a specific @tablename specified, see if it has the kind of keys we want. --
-- If asking for references from @tablename to 'primary', we must have an FKEY; --
-- if asking for references to @tablename from 'foreign', we must have an active REFerence. --
if (@id is not null) begin
declare @wantkeytype varchar(32)
select @wantkeytype = case @doprimary when 1 then 'TableHasForeignKey' else 'TableHasForeignRef' end
if not exists (select * from dbo.sysobjects where id = @id and objectproperty(id, @wantkeytype) <> 0)
goto ReturnSet
end

if (@dotables = 1) begin
if (@doprimary = 1) begin
-- Get all candidate tables (those with Primary/Unique keys in sysconstraints). --
insert #sprefs
select distinct id, null, 0 from dbo.sysconstraints where status & 0x0f in (1, 2)

-- Update the referenced bit if this table references it. --
update #sprefs set referenced = 1
where id in (select rkeyid from dbo.sysreferences where fkeyid = @id)
end else begin
-- All user tables are foreign-key candidate tables. --
insert #sprefs
select distinct id, null, 0 from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1

-- Update the referenced bit if it references this table. --
update #sprefs set referenced = 1
where id in (select fkeyid from dbo.sysreferences where rkeyid = @id)
end -- direction --

end else begin -- keys --
if (@doprimary = 1) begin
-- Get all candidate tables (those with Primary/Unique keys in sysconstraints) and the keys. --
insert #sprefs
select distinct id, constid, 0 from dbo.sysconstraints where status & 0x0f in (1, 2)

-- Follow r.rkeyindid back to sysindexes to get the name and then 'rconstid' to see if this table references it. --
update #sprefs set referenced = 1 from #sprefs s, dbo.sysreferences r, dbo.sysindexes i
where r.fkeyid = @id
and i.id = r.rkeyid and i.indid = r.rkeyindid and i.status & 0x1800 <> 0
and s.constid = object_id(N'[' + REPLACE(i.name, N']', N']]') + N']')

end else begin
-- First add tables with FOREIGN keys defined. --
insert #sprefs
select distinct id, constid, 0 from dbo.sysconstraints where status & 0x0f in (3)

-- All user tables are foreign-key candidate tables, so add any tables we haven't yet, if @doall. --
-- (This would be used for 'push' key definition; defining FK's from the standpoint of the PK table). --
insert #sprefs
select distinct id, null, 0 from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1
and @doall = 1 and id not in (select id from #sprefs)

-- Update the referenced bit if it references this table. --
update #sprefs set referenced = 1
where constid in (select constid from dbo.sysreferences where rkeyid = @id)
end -- direction --
end -- tables or keys --

-- Exclude system and MS-internal objects, or tables/keys that aren't in the @reftable we want, if any specified. --
delete #sprefs where id in (select id from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') <> 1 or category & 0x0002 <> 0)
or (@refid is not null and id != @refid)

-- Output --
ReturnSet:
if (@docols = 0) begin
if (@tablename is not null) begin
select candidate_table = N'[' + REPLACE(schema_name(syso.schema_id), N']', N']]') + N']' + N'.' + N'[' + REPLACE(object_name(o.id), N']', N']]') + N']',
candidate_key = case @dotables when 1 then N'N/A' else object_name(s.constid) end, s.referenced
from #sprefs s, dbo.sysobjects o, sys.all_objects syso where o.id = s.id and (@doall = 1 or s.referenced = 1) and o.id = syso.object_id
order by object_name(o.id), schema_name(syso.schema_id), object_name(s.constid)
end else begin
select candidate_table = N'[' + REPLACE(schema_name(syso.schema_id), N']', N']]') + N']' + N'.' + N'[' + REPLACE(object_name(o.id), N']', N']]') + N']',
candidate_key = case @dotables when 1 then N'N/A' else object_name(s.constid) end
from #sprefs s, dbo.sysobjects o, sys.all_objects syso where o.id = s.id and o.id = syso.object_id
order by object_name(o.id), schema_name(syso.schema_id), object_name(s.constid)
end
end else begin -- @docols = 1 --
-- This is currently just implemented for 'nonNULLtablename', 'actualkeycols', 'foreign'. --
select candidate_table = N'[' + REPLACE(schema_name(syso.schema_id), N']', N']]') + N']' + N'.' + N'[' + REPLACE(object_name(o.id), N']', N']]') + N']',
candidate_key = object_name(s.constid),
cKeyCol1 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey1)),
cKeyCol2 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey2)),
cKeyCol3 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey3)),
cKeyCol4 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey4)),
cKeyCol5 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey5)),
cKeyCol6 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey6)),
cKeyCol7 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey7)),
cKeyCol8 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey8)),
cKeyCol9 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey9)),
cKeyCol10 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey10)),
cKeyCol11 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey11)),
cKeyCol12 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey12)),
cKeyCol13 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey13)),
cKeyCol14 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey14)),
cKeyCol15 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey15)),
cKeyCol16 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey16))
from #sprefs s, dbo.sysobjects o, dbo.sysreferences r, sys.all_objects syso
where o.id = s.id and r.constid = s.constid and s.referenced = 1 and o.id = syso.object_id
order by object_name(o.id), schema_name(syso.schema_id), object_name(s.constid)
end

sp_MStablekeys (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_MStablekeys(nvarchar @tablename
, nvarchar @colname
, int @type
, nvarchar @keyname
, int @flags)

MetaData:

   
create procedure sys.sp_MStablekeys
@tablename nvarchar(776) = null, @colname nvarchar(258) = null, @type int = null, @keyname nvarchar(517) = null, @flags int = null
as



create table #tempID
(
cName nvarchar(132) COLLATE database_default NOT NULL, -- Index name --
cPK1 int, cPK2 int, cPK3 int, cPK4 int, cPK5 int, cPK6 int, cPK7 int, cPK8 int,
cPK9 int, cPK10 int, cPK11 int, cPK12 int, cPK13 int, cPK14 int, cPK15 int, cPK16 int -- 1 if DESC --
)

create table #tempID2
(
cPKName nvarchar(132) COLLATE database_default NOT NULL, -- PK name --
cPK int -- Combined info for PK --
)

create table #spkeys
(
cType tinyint NOT NULL, -- key Type --
cName nvarchar(258) COLLATE database_default NOT NULL, -- key Name --
cFlags int NULL, -- e.g., 1 = clustered for PK/Unique --
cColCount int NULL, -- number of columns (or column pairs) in the key --
cFillFactor tinyint NULL, -- Fill factor of index creation --
cRefTable nvarchar(520) COLLATE database_default NULL, -- owner-qual Referenced table name for FKs --
cRefKey nvarchar(260) COLLATE database_default NULL, -- name of referenced key in referenced table --
-- Note: cConstID replaces the column list used in 6.0, for speed.
-- The output set MUST replace this with either index_col(@tablename, cIndexID, 1-16) and NULL * 16
-- (for PK/UQ) UNION col_name(r.fkeyid, r.fkey1-16) and col_name(r.rkeyid, r.rkey1-16), for SQLDMO,
-- and these MUST BE nvarchar(132) for alignment in the SQLDMO cache structure!
cConstID int NULL, -- Reference constraint ID, if Foreign Key --
cIndexID int NULL, -- ID of this key's index, if PK/UQ --
cGroupName sysname COLLATE database_default NULL, -- FileGroup name of this key, if PK/UQ --
cDisabled int NULL, -- 0 if enabled, 1 if disabled --
cPrimaryFG int NULL, -- 1 if primary FG, 0 otherwise --
cDeleteCascade int NULL, -- 1 if it is a foreign key constraint with a cascade delete --
cUpdateCascade int NULL -- 1 if it is a foreign key constraint with a cascade update --
)

-- This proc returns the table's DRI keys. @type is the type(s) of key(s) to return. --
-- Make sure @type is only the key types (DRI_PRIMARYKEY, DRI_UNIQUE, DRI_REFERENCE). --
if (@type is null)
select @type = 0x000e
else
select @type = @type & 0x000e

-- Flags usage: For daVinci, to pass call thru to sp_MStablerefs. --
if (@flags is null)
select @flags = 0

set nocount on
declare @cType int, @cName nvarchar(258), @cFlags int, @cRefTable nvarchar(520), @fillfactor tinyint
declare @objid int, @constid int, @indid int, @keycnt int, @q1 nvarchar(2000), @q2 nvarchar(2000), @objtype int, @groupname sysname
declare @cDisabled int, @PrimaryFG int, @cDeleteCascade int, @cUpdateCascade int

-- First see if @keyname was defined, and override @tablename and @type if so. --
if (@keyname is not null)
begin
select @objid = id, @type = power(2, status & 0x0f) from dbo.sysconstraints where constid = object_id(@keyname)
if (@objid is null) begin
RAISERROR (15001, -1, -1, @keyname)
return 1
end
-- Now get the tablename for the index_col below --
select @tablename = N'[' + REPLACE(schema_name(syso.schema_id), N']', N']]') + N']' + N'.' + N'[' + REPLACE(syso.name, N']', N']]') + N']' from sys.all_objects syso where syso.object_id = @objid
end else begin
-- Want all keys for this table (of @type type). --
select @objid = id, @objtype = (case when OBJECTPROPERTY(id, N'IsTable') = 1 then 1 else 0 end)
from dbo.sysobjects where id = object_id(@tablename)
if (@objid is null) begin
RAISERROR (15001, -1, -1, @tablename)
return 1
end
if (@objtype <> 1) begin
RAISERROR (15218, -1, -1, @tablename)
return 1
end
if @colname is not null and not exists (select * from dbo.syscolumns where id = @objid and name = @colname) begin
RAISERROR (14305, -1, -1, @colname, @tablename)
return 1
end

-- Skip cursor opening if we don't have any keys (of the type wanted); return a set anyway, for the cache. --
if (((@type & power(2, 1)) = 0 or objectproperty(@objid, 'TableHasPrimaryKey') = 0) and
((@type & power(2, 2)) = 0 or objectproperty(@objid, 'TableHasUniqueCnst') = 0) and
((@type & power(2, 3)) = 0 or objectproperty(@objid, 'TableHasForeignKey') = 0))
goto ReturnSet
end

-- Preprocessor won't replace within quotes so have to use str(). --
declare @sysgenname nvarchar(12), @pkstr nvarchar(12), @uqstr nvarchar(12), @fkstr nvarchar(12), @objtypebits nvarchar(12)
select @sysgenname = ltrim(str(convert(int, 0x00020000)))
select @pkstr = ltrim(str(convert(int, 1)))
select @uqstr = ltrim(str(convert(int, 2)))
select @fkstr = ltrim(str(convert(int, 3)))
select @objtypebits = ltrim(str(convert(int, 0x0f)))

-- Other ints we need strings for --
declare @objidstr nvarchar(12), @typestr nvarchar(12)
select @objidstr = ltrim(str(@objid))
select @typestr = ltrim(str(@type))

-- Qualifying key name. Size buffer = 517 (keyname)+ 517 (for escaping) + 100 (misc string) --
declare @qualkeyname nvarchar(1134)
select @qualkeyname = null
if (@keyname is not null) begin
select @qualkeyname = N' and constid = object_id(''' + REPLACE(@keyname, '''', '''''') + N''')'
end

-- -- -- -- -- -- -- -- -- -- -- /
-- Main cursor loop. --
-- -- -- -- -- -- -- -- -- -- -- /
-- exec(N'declare hC insensitive cursor for select constid, status & ' + @objtypebits + N', status & ' + @sysgenname + --
exec(N'declare hC cursor global for select constid, status & ' + @objtypebits + N', status & ' + @sysgenname +
N' from dbo.sysconstraints where id = ' + @objidstr + N' and (' + @typestr + N' & power(2, status & 0x0f) != 0) ' + @qualkeyname)
open hC
fetch hC into @constid, @cType, @cFlags
while (@@fetch_status >= 0) begin
if (object_name(@constid) is null) begin
RAISERROR(55555, 16, 1); -- N'Assert failed: object_name(@constid) is null in sp_MStablekeys (pk/uq)'
return 1
end

-- DRI_PRIMARYKEY, DRI_UNIQUE --
if (@cType in (1, 2)) begin
-- Get the index id enforcing this constraint. --
select @indid = i.indid, @cName = o.name, @fillfactor = i.OrigFillFactor,
@cFlags = @cFlags | (case indid when 1 then 0x00000001 else 0 end), -- test for clustered index --
-- clustered index keys are part of non-clustered index key list, which cause incorrect sysindexes.keycnt --
@keycnt = case indid when 1 then keycnt else (select count(x.id) from dbo.sysindexkeys x where i.indid = x.indid and x.id = @objid) end,
@groupname = f.groupname,
@PrimaryFG = FILEGROUPPROPERTY( f.groupname, N'IsPrimaryFG' )
from dbo.sysindexes i, dbo.sysobjects o, dbo.sysfilegroups f
-- Use '=' instead of 'LIKE' in comparision, so we can handle wide card character correctly --
where o.id = @constid and i.name = o.name and i.status & 0x1800 <> 0 and i.groupid = f.groupid
if (@indid is null) begin
RAISERROR(77777, 16, 1); -- N'Assert failed: @indid is null in sp_MStablekeys (pk/uq)'
return 1
end

insert #spkeys values (@cType, @cName, @cFlags, @keycnt, @fillfactor, null, null, null, @indid, @groupname, 0, @PrimaryFG, 0, 0)
end

-- DRI_REFERENCE --
else if (@cType in (3)) begin
-- Get the key column information from sysreferences. --
select @keycnt = r.keycnt, @cName = object_name(r.constid), @cRefTable = N'[' + schema_name(syso.schema_id) + N']' + N'.' + N'[' + o.name + N']',
@cDisabled = OBJECTPROPERTY( r.constid, N'CnstIsDisabled' ),
@cDeleteCascade = OBJECTPROPERTY( r.constid, N'CnstIsDeleteCascade'),
@cUpdateCascade = OBJECTPROPERTY( r.constid, N'CnstIsUpdateCascade')
from dbo.sysreferences r, dbo.sysobjects o, sys.all_objects syso where r.constid = @constid and o.id = r.rkeyid and o.id = syso.object_id

-- Follow r.rkeyindid back to sysindexes to get the ref key name. --
declare @cRefKey nvarchar(132)
select @cRefKey = i.name, @cFlags = c.status from dbo.sysreferences r, dbo.sysindexes i, dbo.sysconstraints c
where c.constid = r.constid and r.constid = @constid
and i.id = r.rkeyid and i.indid = r.rkeyindid and i.status & 0x1800 <> 0

-- Load our temp table. --
insert #spkeys values (@cType, @cName, @cFlags, @keycnt, null, @cRefTable, @cRefKey, @constid, null, null, @cDisabled, 0, @cDeleteCascade, @cUpdateCascade)
end -- Key type --

-- Get the next row. --
fetch hC into @constid, @cType, @cFlags
end -- PRIMARY/UNIQUE --
deallocate hC

-- Work on the descending information --
set nocount on
insert #tempID
select cName,
indexkey_property(object_id(@tablename), cIndexID, 1, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 2, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 3, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 4, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 5, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 6, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 7, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 8, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 9, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 10, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 11, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 12, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 13, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 14, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 15, N'isdescending'),
indexkey_property(object_id(@tablename), cIndexID, 16, N'isdescending')
from #spkeys
order by cType, cName

-- Construct the bit --
declare @idx int
declare @Name nvarchar(132)
declare @Inx_1 int, @Inx_2 int, @Inx_3 int, @Inx_4 int, @Inx_5 int, @Inx_6 int, @Inx_7 int, @Inx_8 int
declare @Inx_9 int, @Inx_10 int, @Inx_11 int, @Inx_12 int, @Inx_13 int, @Inx_14 int, @Inx_15 int, @Inx_16 int

declare hCur cursor global for select * from #tempID
open hCur
fetch next from hCur into @Name, @Inx_1, @Inx_2, @Inx_3, @Inx_4, @Inx_5, @Inx_6, @Inx_7, @Inx_8,
@Inx_9, @Inx_10, @Inx_11, @Inx_12, @Inx_13, @Inx_14, @Inx_15, @Inx_16
while (@@FETCH_STATUS = 0)
begin
select @idx = 0x0000
select @idx = (case when (@Inx_1 = 1) then @idx | 0x0001 else @idx end), @idx = (case when (@Inx_2 = 1) then @idx | 0x0002 else @idx end), @idx = (case when (@Inx_3 = 1) then @idx | 0x0004 else @idx end), @idx = (case when (@Inx_4 = 1) then @idx | 0x0008 else @idx end), @idx = (case when (@Inx_5 = 1) then @idx | 0x0010 else @idx end), @idx = (case when (@Inx_6 = 1) then @idx | 0x0020 else @idx end), @idx = (case when (@Inx_7 = 1) then @idx | 0x0040 else @idx end), @idx = (case when (@Inx_8 = 1) then @idx | 0x0080 else @idx end),
@idx = (case when (@Inx_9 = 1) then @idx | 0x0100 else @idx end), @idx = (case when (@Inx_10 = 1) then @idx | 0x0200 else @idx end), @idx = (case when (@Inx_11 = 1) then @idx | 0x0400 else @idx end), @idx = (case when (@Inx_12 = 1) then @idx | 0x0800 else @idx end), @idx = (case when (@Inx_13 = 1) then @idx | 0x1000 else @idx end), @idx = (case when (@Inx_14 = 1) then @idx | 0x2000 else @idx end), @idx = (case when (@Inx_15 = 1) then @idx | 0x4000 else @idx end), @idx = (case when (@Inx_16 = 1) then @idx | 0x8000 else @idx end)
insert #tempID2 select @Name, @idx

fetch next from hCur into @Name, @Inx_1, @Inx_2, @Inx_3, @Inx_4, @Inx_5, @Inx_6, @Inx_7, @Inx_8,
@Inx_9, @Inx_10, @Inx_11, @Inx_12, @Inx_13, @Inx_14, @Inx_15, @Inx_16
end
close hCur
deallocate hCur
set nocount off


-- Now output the data --
ReturnSet:
set nocount off
select cType, cName, cFlags, cColCount, cFillFactor, cRefTable, cRefKey,
cKeyCol1 = convert(nvarchar(132), index_col(@tablename, cIndexID, 1)),
cKeyCol2 = convert(nvarchar(132), index_col(@tablename, cIndexID, 2)),
cKeyCol3 = convert(nvarchar(132), index_col(@tablename, cIndexID, 3)),
cKeyCol4 = convert(nvarchar(132), index_col(@tablename, cIndexID, 4)),
cKeyCol5 = convert(nvarchar(132), index_col(@tablename, cIndexID, 5)),
cKeyCol6 = convert(nvarchar(132), index_col(@tablename, cIndexID, 6)),
cKeyCol7 = convert(nvarchar(132), index_col(@tablename, cIndexID, 7)),
cKeyCol8 = convert(nvarchar(132), index_col(@tablename, cIndexID, 8)),
cKeyCol9 = convert(nvarchar(132), index_col(@tablename, cIndexID, 9)),
cKeyCol10 = convert(nvarchar(132), index_col(@tablename, cIndexID, 10)),
cKeyCol11 = convert(nvarchar(132), index_col(@tablename, cIndexID, 11)),
cKeyCol12 = convert(nvarchar(132), index_col(@tablename, cIndexID, 12)),
cKeyCol13 = convert(nvarchar(132), index_col(@tablename, cIndexID, 13)),
cKeyCol14 = convert(nvarchar(132), index_col(@tablename, cIndexID, 14)),
cKeyCol15 = convert(nvarchar(132), index_col(@tablename, cIndexID, 15)),
cKeyCol16 = convert(nvarchar(132), index_col(@tablename, cIndexID, 16)),
cRefCol1 = convert(nvarchar(132), null),
cRefCol2 = convert(nvarchar(132), null),
cRefCol3 = convert(nvarchar(132), null),
cRefCol4 = convert(nvarchar(132), null),
cRefCol5 = convert(nvarchar(132), null),
cRefCol6 = convert(nvarchar(132), null),
cRefCol7 = convert(nvarchar(132), null),
cRefCol8 = convert(nvarchar(132), null),
cRefCol9 = convert(nvarchar(132), null),
cRefCol10 = convert(nvarchar(132), null),
cRefCol11 = convert(nvarchar(132), null),
cRefCol12 = convert(nvarchar(132), null),
cRefCol13 = convert(nvarchar(132), null),
cRefCol14 = convert(nvarchar(132), null),
cRefCol15 = convert(nvarchar(132), null),
cRefCol16 = convert(nvarchar(132), null),
cIndexID,
cGroupName,
cDisabled,
cPrimaryFG,
cDeleteCascade,
cUpdateCascade,
Descending = t.cPK
from #spkeys, #tempID2 t where cType in (1, 2)
and cName = t.cPKName
and (@colname is null or
index_col(@tablename, cIndexID, 1) = @colname or
index_col(@tablename, cIndexID, 2) = @colname or
index_col(@tablename, cIndexID, 3) = @colname or
index_col(@tablename, cIndexID, 4) = @colname or
index_col(@tablename, cIndexID, 5) = @colname or
index_col(@tablename, cIndexID, 6) = @colname or
index_col(@tablename, cIndexID, 7) = @colname or
index_col(@tablename, cIndexID, 8) = @colname or
index_col(@tablename, cIndexID, 9) = @colname or
index_col(@tablename, cIndexID, 10) = @colname or
index_col(@tablename, cIndexID, 11) = @colname or
index_col(@tablename, cIndexID, 12) = @colname or
index_col(@tablename, cIndexID, 13) = @colname or
index_col(@tablename, cIndexID, 14) = @colname or
index_col(@tablename, cIndexID, 15) = @colname or
index_col(@tablename, cIndexID, 16) = @colname
)
UNION
select c.cType, c.cName, c.cFlags, c.cColCount, c.cFillFactor, c.cRefTable, c.cRefKey,
cKeyCol1 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey1)),
cKeyCol2 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey2)),
cKeyCol3 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey3)),
cKeyCol4 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey4)),
cKeyCol5 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey5)),
cKeyCol6 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey6)),
cKeyCol7 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey7)),
cKeyCol8 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey8)),
cKeyCol9 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey9)),
cKeyCol10 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey10)),
cKeyCol11 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey11)),
cKeyCol12 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey12)),
cKeyCol13 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey13)),
cKeyCol14 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey14)),
cKeyCol15 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey15)),
cKeyCol16 = convert(nvarchar(132), col_name(r.fkeyid, r.fkey16)),
cRefCol1 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey1)),
cRefCol2 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey2)),
cRefCol3 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey3)),
cRefCol4 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey4)),
cRefCol5 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey5)),
cRefCol6 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey6)),
cRefCol7 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey7)),
cRefCol8 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey8)),
cRefCol9 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey9)),
cRefCol10 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey10)),
cRefCol11 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey11)),
cRefCol12 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey12)),
cRefCol13 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey13)),
cRefCol14 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey14)),
cRefCol15 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey15)),
cRefCol16 = convert(nvarchar(132), col_name(r.rkeyid, r.rkey16)),
cIndexID,
cGroupName,
cDisabled,
cPrimaryFG,
cDeleteCascade,
cUpdateCascade,
0
from #spkeys c, dbo.sysreferences r where c.cType = 3 and r.constid = c.cConstID
and (@colname is null or
col_name(r.fkeyid, r.fkey1) = @colname or
col_name(r.fkeyid, r.fkey2) = @colname or
col_name(r.fkeyid, r.fkey3) = @colname or
col_name(r.fkeyid, r.fkey4) = @colname or
col_name(r.fkeyid, r.fkey5) = @colname or
col_name(r.fkeyid, r.fkey6) = @colname or
col_name(r.fkeyid, r.fkey7) = @colname or
col_name(r.fkeyid, r.fkey8) = @colname or
col_name(r.fkeyid, r.fkey9) = @colname or
col_name(r.fkeyid, r.fkey10) = @colname or
col_name(r.fkeyid, r.fkey11) = @colname or
col_name(r.fkeyid, r.fkey12) = @colname or
col_name(r.fkeyid, r.fkey13) = @colname or
col_name(r.fkeyid, r.fkey14) = @colname or
col_name(r.fkeyid, r.fkey15) = @colname or
col_name(r.fkeyid, r.fkey16) = @colname
)
order by cType, cName

if (@flags & 1 <> 0)
exec sys.sp_MStablerefs @tablename, N'actualkeycols', N'foreign'

sp_MStablechecks (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_MStablechecks(nvarchar @tablename
, int @flags)

MetaData:

   
create procedure sys.sp_MStablechecks
@tablename nvarchar(517), @flags int = null
as
-- -- @flags added for DaVinci uses. If the bit isn't set, use 6.5 -- --
-- -- sp_MStablechecks '%s' -- --

declare @id int
select @id = object_id(@tablename)
if (@id is null) begin
RAISERROR (15001, -1, -1, @tablename)
return 1
end

-- @flags is for daVinci. --
if (@flags is null)
select @flags = 0

-- We'll put out the check text if it's all in one row (most likely); otherwise leave it --
-- blank for refetching in its entirety via sp_helptext, unless @flags wants it anyway. --
select object_name(t.id),
case when (@flags & 1 <> 0 or not exists (select * from dbo.syscomments where id = t.id and colid = 2))
then t.text else null end,
c.status & (convert(int, 0x00200000) | convert(int, 0x00020000) | convert(int, 0x00004000)),
OBJECTPROPERTY( t.id, N'CnstIsDisabled' )
from dbo.syscomments t, dbo.sysconstraints c
where t.id = c.constid and c.id = @id and c.status & 0x0f = 4
and (@flags & 1 <> 0 or t.colid = 1)
order by object_name(t.id), t.colid

sp_MSsubscriptionvalidated (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_MSsubscriptionvalidated(uniqueidentifier @subid
, uniqueidentifier @pubid
, bit @log_attempt)

MetaData:

   
create procedure sys.sp_MSsubscriptionvalidated
@subid uniqueidentifier,
@pubid uniqueidentifier,
@log_attempt bit = 0
as
declare @now datetime
declare @retcode int

select @now=getdate()

if ({fn ISPALUSER(@pubid)} <> 1)
begin
if (@pubid is NULL)
begin
RAISERROR (21723, 16, -1, 'sp_MSsubscriptionvalidated')
return 1
end
else
begin
RAISERROR (14126, 11, -1)
return 1
end
end

if @log_attempt=0
update dbo.sysmergesubscriptions set last_validated = @now, attempted_validate=@now
where subid = @subid and pubid = @pubid
else
update dbo.sysmergesubscriptions set attempted_validate = @now
where subid = @subid and pubid = @pubid
if @@rowcount <> 1 or @@error <> 0
begin
RAISERROR (20070, 16, -1)
return (1)
end
return (0)

Total Pageviews