April 27, 2012

sp_helpdevice (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_helpdevice(nvarchar @devname)

MetaData:

 create procedure sys.sp_helpdevice -- - 1996/04/08 00:00  
@devname sysname = NULL -- device to check out --
as

-- Create temp tables before any DML to ensure dynamic
-- Create a temporary table where we can build up a translation of
-- the device status bits.
--
create table #spdevtab
(
name sysname NOT NULL,
statusdesc nvarchar(255) null
)

-- alter the columns to master's collation, since we are inserting names from master.dbo.sysdevices.
-- This is needed because if this proc is being run in CDB, then the columns would be the CDB's data
-- collation that could be different from master db's collation.
--
declare @alterTab nvarchar(1024)
select @alterTab = N'alter table #spdevtab alter column name sysname COLLATE ' + convert(nvarchar(256), SERVERPROPERTY('collation')) + ' NOT NULL'
exec(@alterTab)
select @alterTab = N'alter table #spdevtab alter column statusdesc nvarchar(255) COLLATE ' + convert(nvarchar(256), SERVERPROPERTY('collation')) + ' NULL'
exec(@alterTab)


--
-- See if the device exists.
--

if not exists (select * from master.dbo.sysdevices where name = @devname)
begin
if (@devname is not null)
begin
raiserror(15012,-1,-1,@devname)
return (1)
end
end

set nocount on

--
-- Initialize the temporary table with the names of the devices.
--
insert into #spdevtab (name)
select name
from master.dbo.sysdevices
where (@devname is null or name = @devname)


--
-- Now figure out what kind of controller type it is.
--
-- cntrltype = 0 special (data disk)
-- 2 disk (dump)
-- 3-4 floppy (dump) Not supported in SQL 7.0
-- 5 tape No size information in SQL 7.0
-- 6 pipe
-- 7 virtual_device
--
update #spdevtab
set statusdesc = N'special'
from master.dbo.sysdevices d, #spdevtab
where d.cntrltype = 0
and #spdevtab.name = d.name
update #spdevtab
set statusdesc = N'disk'
from master.dbo.sysdevices d, #spdevtab
where d.cntrltype = 2
and #spdevtab.name = d.name

update #spdevtab
set statusdesc = N'tape'
from master.dbo.sysdevices d, #spdevtab
where d.cntrltype = 5
and #spdevtab.name = d.name

update #spdevtab
set statusdesc = N'virtual_device'
from master.dbo.sysdevices d, #spdevtab
where d.cntrltype = 7
and #spdevtab.name = d.name

update #spdevtab
set statusdesc = N'UNKNOWN DEVICE'
from master.dbo.sysdevices d, #spdevtab
where d.cntrltype >= 8
and #spdevtab.name = d.name


--
-- Now check out the status bits and turn them into english.
-- Status of 16 is a dump device.
--
update #spdevtab set statusdesc = statusdesc + N', ' + rtrim(v.name)
from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
where v.type = 'V' and v.number > -1
and d.status & v.number = 16
and #spdevtab.name = d.name

--
-- Status of 1 is a default disk.
--
update #spdevtab set statusdesc = statusdesc + N', ' + rtrim(v.name)
from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
where v.type = 'V' and v.number > -1
and d.status & v.number = 1
and #spdevtab.name = d.name

--
-- Status of 2 is a physical disk.
--
update #spdevtab
set statusdesc = substring(statusdesc, 1, 225) + N', ' + rtrim(v.name)
from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
where v.type = 'V' and v.number > -1
and d.status & v.number = 2
and #spdevtab.name = d.name

--
-- Add in its size in MB.
--
update #spdevtab
set statusdesc = statusdesc + N', ' + convert(varchar(10),
round((convert(float, d.size) * (select low from master.dbo.spt_values
where type = 'E' and number = 1)
/ 1048576), 1)) + ' MB'
from master.dbo.sysdevices d, #spdevtab, master.dbo.spt_values v
where d.status & 2 = 2
and #spdevtab.name = d.name
and v.number = 1
and v.type = 'E'

--
-- Status of 4 is a logical disk.
--
update #spdevtab
set statusdesc = substring(statusdesc, 1, 225) + N', ' + rtrim(v.name)
from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
where v.type = 'V' and v.number > -1
and d.status & v.number = 4
and #spdevtab.name = d.name

--
-- Status of 4096 is read only.
--
update #spdevtab
set statusdesc = substring(statusdesc, 1, 225) + N', ' + rtrim(v.name)
from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
where v.type = 'V' and v.number > -1
and d.status & v.number = 4096
and #spdevtab.name = d.name
--
-- Status of 8192 is deferred.
--
update #spdevtab
set statusdesc = substring(statusdesc, 1, 225) + N', ' + (v.name)
from master.dbo.sysdevices d, master.dbo.spt_values v, #spdevtab
where v.type = 'V' and v.number > -1
and d.status & v.number = 8192
and #spdevtab.name = d.name

set nocount off

--
-- The device number is in the high byte of sysdevices.low so
-- spt_values tells us which byte to pick out.
--
select device_name = d.name, physical_name = d.phyname,
description = #spdevtab.statusdesc,
status = d.status&12319, d.cntrltype,
size
from master.dbo.sysdevices d, #spdevtab, master.dbo.spt_values v
where d.name = #spdevtab.name
and v.type = 'E'
and v.number = 3

return(0) -- sp_helpdevice

sp_helplanguage (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_helplanguage(nvarchar @language)

MetaData:

 create procedure sys.sp_helplanguage -- - 1996/04/08 00:00  
@language sysname = NULL
as
-- Print all languages if the user didn't give the language name.
if @language is null
begin
if exists (select * from sys.syslanguages)
select * from sys.syslanguages
else
raiserror(15452,-1,-1)

-- Find out whether us_english is there or not.
if not exists (select * from sys.syslanguages
where name = 'us_english')
raiserror(15453,-1,-1)

return (0)
end

-- Report information on this language.
if exists (select * from sys.syslanguages where name = @language)
begin
select * from sys.syslanguages where name = @language
return (0)
end

if exists (select * from sys.syslanguages where alias = @language)
begin
select * from sys.syslanguages where alias = @language
return (0)
end

-- Couldn't find this language.
if @language = 'us_english'
begin
raiserror(15453,-1,-1)
return (0)
end
else
begin
raiserror(15033,-1,-1,@language)
return (1)
end
-- sp_helplanguage

sp_helpindex (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_helpindex(nvarchar @objname)

MetaData:

 create procedure sys.sp_helpindex  
@objname nvarchar(776) -- the table to check for indexes
as
-- PRELIM
set nocount on

declare @objid int, -- the object id of the table
@indid smallint, -- the index id of an index
@groupid int, -- the filegroup id of an index
@indname sysname,
@groupname sysname,
@status int,
@keys nvarchar(2126), -- Length (16*max_identifierLength)+(15*2)+(16*3)
@dbname sysname,
@ignore_dup_key bit,
@is_unique bit,
@is_hypothetical bit,
@is_primary_key bit,
@is_unique_key bit,
@is_columnstore bit,
@auto_created bit,
@no_recompute bit

-- Check to see that the object names are local to the current database.
select @dbname = parsename(@objname,3)
if @dbname is null
select @dbname = db_name()
else if @dbname <> db_name()
begin
raiserror(15250,-1,-1)
return (1)
end

-- Check to see the the table exists and initialize @objid.
select @objid = object_id(@objname)
if @objid is NULL
begin
raiserror(15009,-1,-1,@objname,@dbname)
return (1)
end

-- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
declare ms_crs_ind cursor local static for
select i.index_id, i.data_space_id, i.name,
i.ignore_dup_key, i.is_unique, i.is_hypothetical, i.is_primary_key, i.is_unique_constraint,
case when (type = 5 or type = 6) then 1 else 0 end,
case when (type = 5 or type = 6) then 0 else s.auto_created end,
case when (type = 5 or type = 6) then 0 else s.no_recompute end
from sys.indexes i left join sys.stats s
on i.object_id = s.object_id and i.index_id = s.stats_id
where i.object_id = @objid and type in (1, 2, 5, 6)
open ms_crs_ind
fetch ms_crs_ind into @indid, @groupid, @indname, @ignore_dup_key, @is_unique, @is_hypothetical,
@is_primary_key, @is_unique_key, @is_columnstore, @auto_created, @no_recompute

-- IF NO INDEX, QUIT
if @@fetch_status < 0
begin
deallocate ms_crs_ind
raiserror(15472,-1,-1,@objname) -- Object does not have any indexes.
return (0)
end

-- create temp table
CREATE TABLE #spindtab
(
index_name sysname collate catalog_default NOT NULL,
index_id int,
ignore_dup_key bit,
is_unique bit,
is_hypothetical bit,
is_primary_key bit,
is_unique_key bit,
is_columnstore bit,
auto_created bit,
no_recompute bit,
groupname sysname collate catalog_default NULL,
index_keys nvarchar(2126) collate catalog_default NULL -- see @keys above for length descr
)

-- Now check out each index, figure out its type and keys and
-- save the info in a temporary table that we'll print out at the end.
while @@fetch_status >= 0
begin
-- First we'll figure out what the keys are.
declare @i int, @thiskey nvarchar(131) -- 128+3

select @keys = index_col(@objname, @indid, 1), @i = 2
if (indexkey_property(@objid, @indid, 1, 'isdescending') = 1)
select @keys = @keys + '(-)'

select @thiskey = index_col(@objname, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@objid, @indid, @i, 'isdescending') = 1))
select @thiskey = @thiskey + '(-)'

while (@thiskey is not null )
begin
select @keys = @keys + ', ' + @thiskey, @i = @i + 1
select @thiskey = index_col(@objname, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@objid, @indid, @i, 'isdescending') = 1))
select @thiskey = @thiskey + '(-)'
end

select @groupname = null
if (serverproperty('EngineEdition') != 5)
select @groupname = name from sys.data_spaces where data_space_id = @groupid

-- INSERT ROW FOR INDEX
insert into #spindtab values (@indname, @indid, @ignore_dup_key, @is_unique, @is_hypothetical,
@is_primary_key, @is_unique_key, @is_columnstore, @auto_created, @no_recompute, @groupname, @keys)

-- Next index
fetch ms_crs_ind into @indid, @groupid, @indname, @ignore_dup_key, @is_unique, @is_hypothetical,
@is_primary_key, @is_unique_key, @is_columnstore, @auto_created, @no_recompute
end
deallocate ms_crs_ind

-- DISPLAY THE RESULTS
select
'index_name' = index_name,
'index_description' = convert(varchar(210), -- bits 16 off, 1, 2, 16777216 on, located on group
case when index_id = 1 then 'clustered' else 'nonclustered' end
+ case when ignore_dup_key <>0 then ', ignore duplicate keys' else '' end
+ case when is_unique <>0 then ', unique' else '' end
+ case when is_hypothetical <>0 then ', hypothetical' else '' end
+ case when is_primary_key <>0 then ', primary key' else '' end
+ case when is_unique_key <>0 then ', unique key' else '' end
+ case when is_columnstore <>0 then ', columnstore' else '' end
+ case when auto_created <>0 then ', auto create' else '' end
+ case when no_recompute <>0 then ', stats no recompute' else '' end
+ ' located on ' + ISNULL(groupname,'')),
'index_keys' = index_keys
from #spindtab
order by index_name


return (0) -- sp_helpindex

sp_helpfilegroup (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_helpfilegroup(nvarchar @filegroupname)

MetaData:

 create procedure sys.sp_helpfilegroup  
@filegroupname sysname = NULL -- filegroup name or all filegroups --
as

set nocount on
-- status & 0x40 is a log file and thus not in any filegroup
if @filegroupname IS NULL
begin
select g.groupname, g.groupid, 'filecount' =
(select count(*) from sysfiles f
where f.groupid = g.groupid
and (f.status & 0x40 <> 0x40))
from sysfilegroups g
end
else
begin
if (filegroup_id(@filegroupname) IS NULL)
begin
raiserror (15325, -1, -1, 'filegroup', @filegroupname)
return (1)
end
select g.groupname, g.groupid, 'filecount' =
(select count(*) from sysfiles f
where f.groupid = g.groupid
and (f.status & 0x40 <> 0x40))
from sysfilegroups g
where g.groupid = filegroup_id(@filegroupname)

select 'file_in_group' = name, fileid, filename,
'size' = convert(nvarchar(15), size * 8) + N' KB',
'maxsize' = (case maxsize when -1 then N'Unlimited'
else
convert(nvarchar(15), maxsize * 8) + N' KB' end),
'growth' = (case status & 0x100000 when 0x100000 then
convert(nvarchar(3), growth) + N'%'
else
convert(nvarchar(15), growth * 8) + N' KB' end)
from sysfiles
where groupid = filegroup_id(@filegroupname)
and (status & 0x40 <> 0x40)
order by fileid
end

return (0) -- sp_helpfilegroup

sp_helpfile (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_helpfile(nvarchar @filename)

MetaData:

 create procedure sys.sp_helpfile  
@filename sysname = NULL -- file name or all files --
as

set nocount on

if @filename IS NULL
begin
select name, fileid, filename,
filegroup = filegroup_name(groupid),
'size' = convert(nvarchar(15), convert (bigint, size) * 8) + N' KB',
'maxsize' = (case maxsize when -1 then N'Unlimited'
else
convert(nvarchar(15), convert (bigint, maxsize) * 8) + N' KB' end),
'growth' = (case status & 0x100000 when 0x100000 then
convert(nvarchar(15), growth) + N'%'
else
convert(nvarchar(15), convert (bigint, growth) * 8) + N' KB' end),
'usage' = (case status & 0x40 when 0x40 then 'log only' else 'data only' end)
from sysfiles
order by fileid

end
else
begin
if file_id(@filename) IS NULL
begin -- no such file
raiserror (15325, -1, -1, 'file', @filename)
return (1)
end
select name, filename,
filegroup = filegroup_name(groupid),
'size' = convert(nvarchar(15), convert (bigint, size) * 8) + N' KB',
'maxsize' = (case maxsize when -1 then N'Unlimited'
else
convert(nvarchar(15), convert (bigint, maxsize) * 8) + N' KB' end),
'growth' = (case status & 0x100000 when 0x100000 then
convert(nvarchar(3), growth) + N'%'
else
convert(nvarchar(15), convert (bigint, growth) * 8) + N' KB' end),
'usage' = (case status & 0x40 when 0x40 then 'log only' else 'data only' end)
from sysfiles
where fileid = file_id(@filename)
order by fileid
end

return (0) -- sp_helpfile

sp_helpextendedproc (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_helpextendedproc(nvarchar @funcname)

MetaData:

 create procedure sys.sp_helpextendedproc -- - 1996/08/14 15:53  
@funcname sysname = NULL
as

set nocount on

if @funcname is not null
begin
--
-- Make sure the function name exists
--
if not exists (select * from master.sys.all_extended_procedures
where name = @funcname)
begin
raiserror(15019,-1,-1,@funcname)
return (1)
end
-- print out select function name info --
select distinct name, dll = substring(dll_name,1,255)
from master.sys.all_extended_procedures
where name = @funcname
order by name
end
else
--
-- or print out all function name info
--
select distinct name, dll = substring(dll_name,1,255)
from master.sys.all_extended_procedures
order by name

return (0) -- sp_helpextendedproc

sp_helpdynamicsnapshot_job (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_helpdynamicsnapshot_job(nvarchar @publication
, nvarchar @dynamic_snapshot_jobname
, uniqueidentifier @dynamic_snapshot_jobid)

MetaData:

 create procedure sys.sp_helpdynamicsnapshot_job (  
@publication sysname = N'%',
@dynamic_snapshot_jobname sysname = N'%',
@dynamic_snapshot_jobid uniqueidentifier = null
)
as
declare @retcode int


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

declare @dynamic_snapshot_jobs table
( id int,
agent_id int,
job_name sysname,
job_id uniqueidentifier,
suser_sname sysname NULL,
host_name sysname NULL,
dynamic_snapshot_location nvarchar(255),
frequency_type int,
frequency_interval int,
frequency_subday int,
frequency_subday_interval int,
frequency_relative_interval int,
frequency_recurrence_factor int,
active_start_date int,
active_end_date int,
active_start_time_of_day int,
active_end_time_of_day int,
pubid uniqueidentifier
)
declare @frequency_type int
declare @frequency_interval int
declare @frequency_subday int
declare @frequency_subday_interval int
declare @frequency_relative_interval int
declare @frequency_recurrence_factor int
declare @active_start_date int
declare @active_end_date int
declare @active_start_time_of_day int
declare @active_end_time_of_day int
declare @publisher sysname
declare @publisher_db sysname
declare @suser_sname sysname
declare @host_name sysname
declare @id int
declare @distributor sysname
declare @distribdb sysname
declare @rpcsrvname sysname
declare @distproc nvarchar(4000)
declare @publication_cursor sysname
declare @pubid uniqueidentifier

select @publisher = publishingservername()
select @publisher_db = db_name()

insert @dynamic_snapshot_jobs
(id, agent_id, job_name, job_id, suser_sname, host_name, dynamic_snapshot_location, pubid)
select j.id,
j.agent_id,
j.name,
j.job_id,
j.dynamic_filter_login,
j.dynamic_filter_hostname,
j.dynamic_snapshot_location,
j.pubid
from dbo.sysmergepublications p
inner join MSdynamicsnapshotjobs j
on p.pubid = j.pubid
where (p.name = @publication or @publication = N'%')
and (j.name = @dynamic_snapshot_jobname or @dynamic_snapshot_jobname = N'%')
and (j.job_id = @dynamic_snapshot_jobid or @dynamic_snapshot_jobid is null)

-- Get distributor information for RPC
exec @retcode = sys.sp_MSrepl_getdistributorinfo @distributor = @distributor output,
@distribdb = @distribdb output,
@rpcsrvname = @rpcsrvname output
if @@error <> 0 or @retcode <> 0
return (1)

select @distproc = quotename(rtrim(@rpcsrvname)) + N'.' + quotename(@distribdb) + N'.' + N'dbo.sp_MShelpdynamicsnapshotjobatdistributor'

declare hJobsCursor cursor local fast_forward for
select id, suser_sname, host_name, pubid
from @dynamic_snapshot_jobs
open hJobsCursor
fetch hJobsCursor into @id, @suser_sname, @host_name, @pubid
while (@@fetch_status <> -1)
begin
if @suser_sname is not null or @host_name is not NULL
begin
select @publication_cursor = name from dbo.sysmergepublications where pubid = @pubid
exec @retcode = @distproc
@publisher = @publisher,
@publisher_db = @publisher_db,
@publication = @publication_cursor,
@dynamic_filter_login = @suser_sname,
@dynamic_filter_hostname = @host_name,
@frequency_type = @frequency_type output,
@frequency_interval = @frequency_interval output,
@frequency_subday = @frequency_subday output,
@frequency_subday_interval = @frequency_subday_interval output,
@frequency_relative_interval = @frequency_relative_interval output,
@frequency_recurrence_factor = @frequency_recurrence_factor output,
@active_start_date = @active_start_date output,
@active_end_date = @active_end_date output,
@active_start_time_of_day = @active_start_time_of_day output,
@active_end_time_of_day = @active_end_time_of_day output
if @@error <> 0 or @retcode <> 0
goto Failure

update @dynamic_snapshot_jobs
set frequency_type = @frequency_type,
frequency_interval = @frequency_interval,
frequency_subday = @frequency_subday,
frequency_subday_interval = @frequency_subday_interval,
frequency_relative_interval = @frequency_relative_interval,
frequency_recurrence_factor = @frequency_recurrence_factor,
active_start_date = @active_start_date,
active_end_date = @active_end_date,
active_start_time_of_day = @active_start_time_of_day,
active_end_time_of_day = @active_end_time_of_day
where id = @id
if @@error <> 0
goto Failure
end
fetch hJobsCursor into @id, @suser_sname, @host_name, @pubid
end
close hJobsCursor
deallocate hJobsCursor

select id, job_name, job_id, suser_sname, host_name, dynamic_snapshot_location,
frequency_type,
frequency_interval,
frequency_subday,
frequency_subday_interval,
frequency_relative_interval,
frequency_recurrence_factor,
active_start_date,
active_end_date,
active_start_time_of_day,
active_end_time_of_day
from @dynamic_snapshot_jobs

return 0

Failure:
close hJobsCursor
deallocate hJobsCursor

return 1

sp_helpdistributor_properties (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_helpdistributor_properties()

MetaData:

 create procedure sys.sp_helpdistributor_properties   
AS

declare @retcode int

-- Allow access if the user is 'sysadmin', member of 'db_owner' or 'repl_monitor'
-- role in a distribution database, or in the PAL of a publication whose publisher
-- uses this distributor.
exec @retcode = sys.sp_MSrepl_DistributorPALAccess
if (@retcode <> 0 or @@error <> 0)
BEGIN
DECLARE @login sysname
SET @login = suser_sname()
RAISERROR(21672, 16, -1, @login)
return (1)
END

if exists (select name from msdb.sys.objects where name = 'MSdistributor')
begin
-- There is currently only one property, so this will work
select 'heartbeat_interval' = convert(int, value) from msdb..MSdistributor where
property = 'heartbeat_interval'
return (0)
end
return (1)

sp_helpdistributor (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_helpdistributor(nvarchar @publisher
, nvarchar @local)

MetaData:

   
--
-- Name:
-- sp_helpdistributor
--
-- Description:
-- Procedure used to obtain distributor information.
--
-- Returns:
-- 0 == SUCCESS
-- 1 == FAILURE
-- Several output parameters or result set
--
-- Security:
-- limited public access
-- Requires Certificate signature for catalog access
--
-- Notes:
-- This is a public stored procedure used to gather general
-- distributor information. It can be run on a publisher or
-- a subscriber that has a sysservers entry for the distributor.
--
-- Four output parameters are accessible with public access:
--
-- @distributor Distribution server name
-- @distribdb Distribution database
-- @rpcsrvname rpc server name
-- @publisher_type Publisher type
--
-- One output parameter requires PAL access to a publication
-- associated with the publisher.
--
-- @directory Working directory
--
-- The remaining six output parameters require elevated authorization.
-- 'sysadmin' has access to all results, from any database, at a server
-- with a sysservers entry identifying the distributor. Access is also
-- extended to a 'db_owner' running in a publishing database at a
-- publisher
--
-- @account SQL Server Agent login
-- @min_distretention min distribution retention
-- @max_distretention max distribution retention
-- @history_retention history retention period
-- @history_cleanupagent history cleanup agent
-- @distrib_cleanupagent distribution cleanup agent
--
-- Parameters that the current user is not authorized to access are
-- returned as NULLs, both as output parameters and as columns in the
-- returned result set.
--
create procedure sys.sp_helpdistributor (
@distributor sysname = '%' OUTPUT, -- The distribution server name --
@distribdb sysname = '%' OUTPUT, -- The distribution database --
@directory nvarchar(255) = '%' OUTPUT, -- The working directory --
@account nvarchar(255) = '%' OUTPUT, -- The Windows NT user account --
@min_distretention int = -1 OUTPUT, -- The min distribution retention --
@max_distretention int = -1 OUTPUT, -- The max distribution retention --
@history_retention int = -1 OUTPUT, -- The history retention period --
@history_cleanupagent nvarchar(100) = '%' OUTPUT, -- The history cleanup agent --
@distrib_cleanupagent nvarchar(100) = '%' OUTPUT, -- The distribution cleanup agent --
@publisher sysname = NULL, -- Name of publisher --
@local nvarchar(5) = NULL, -- Get local server values --
@rpcsrvname sysname = '%' OUTPUT,
@publisher_type sysname = '%' OUTPUT
)
AS
BEGIN

SET NOCOUNT ON

--
-- Declarations.
--
DECLARE @loc_distributor sysname
DECLARE @loc_distribdb sysname
DECLARE @loc_directory nvarchar(255)
DECLARE @loc_account nvarchar(255)
DECLARE @loc_mindistretention int
DECLARE @loc_maxdistretention int
DECLARE @loc_historyretention int
DECLARE @loc_historycleanupagent nvarchar(100)
DECLARE @loc_distribcleanupagent nvarchar(100)
DECLARE @loc_security_mode int
DECLARE @loc_login sysname
DECLARE @loc_password sysname
declare @loc_rpcsrvname sysname
DECLARE @loc_publishertype sysname
DECLARE @proc nvarchar(255)
DECLARE @retcode int
declare @rpcsrvlogin sysname
declare @srvid smallint
declare @dist_rpcname sysname
declare @platform_nt binary
declare @has_dbowner_access bit
declare @has_PAL_access bit
declare @login sysname

select @has_dbowner_access = 1
select @has_PAL_access = 1
select @platform_nt = 0x1
select @login = suser_sname(suser_sid())

--
-- processing for publisher
--
IF @publisher IS NULL
BEGIN
--
-- 6.x compatibility
-- If local is set, we know the call is from a publisher.
-- set it to be @@REMSERVER
-- Otherwise, set it to be local server name
-- Note: @@REMSERVER is NULL for local sp calls
--
IF LOWER(@local) = 'local' AND @@REMSERVER IS NOT NULL
SELECT @publisher = @@REMSERVER
ELSE
SELECT @publisher = publishingservername()
END
--
-- Set attribute indicating whether user is 'db_owner'.
--
if LOWER(@local) <> 'local' or @local is NULL
begin
-- Determine whether user has dbowner access
if not ((is_srvrolemember('sysadmin') = 1) or
(is_member('db_owner') = 1 and
sys.fn_MSrepl_ispublished(db_name()) = 1)
)
begin
select @has_dbowner_access = 0

-- Setting @loc_account to '%' prevents reading the registry for the
-- account information at a remote distributor if user isn't authorized.
select @loc_account = '%'
end
end

--
-- Get the distribution server
--
SELECT @dist_rpcname = srvname,
@loc_distributor = datasource,
@srvid = srvid,
@loc_rpcsrvname = srvname
FROM master.dbo.sysservers
WHERE srvstatus & 8 <> 0

if @loc_distributor is null
GOTO DONE

select @rpcsrvlogin = null
-- sysoledbusers is for outgoing rpc servers only so it should be
-- appropriate for querying the remote login of the distributor link. But
-- as a safety measure, we will query sysremotelogins (for incoming RPC
-- calls) if no remote login is returned from sysoledbusers to maintain
-- full compatibility with the sysxlogins query that we used before.
select @rpcsrvlogin = rmtloginame
from master.dbo.sysoledbusers
where rmtsrvid = @srvid and loginsid is NULL

if @rpcsrvlogin is null
begin
select @rpcsrvlogin = remoteusername
from master.dbo.sysremotelogins
where remoteserverid = @srvid and sid is NULL
end

--
-- If remote distribuiton, execute sys.sp_helpdistributor on distribution
-- server.
--
IF UPPER(@loc_distributor) <> UPPER(@@SERVERNAME)
BEGIN
SELECT @proc = @dist_rpcname + '.master.sys.sp_helpdistributor'
--
-- from publisher
--
EXECUTE @retcode = @proc
@loc_distributor OUTPUT,
@loc_distribdb OUTPUT,
@loc_directory OUTPUT,
@loc_account OUTPUT,
@loc_mindistretention OUTPUT,
@loc_maxdistretention OUTPUT,
@loc_historyretention OUTPUT,
@loc_historycleanupagent OUTPUT,
@loc_distribcleanupagent OUTPUT,
@@SERVERNAME,
@local = 'local',
@publisher_type = @loc_publishertype OUTPUT
IF @retcode <> 0 or @@ERROR <> 0
RETURN (1)

GOTO DONE
END
--
-- validate the calling publisher
--
SELECT @loc_distribdb = distribution_db,
@loc_directory = working_directory,
@loc_publishertype = publisher_type
FROM msdb.dbo.MSdistpublishers
WHERE UPPER(name collate database_default ) = UPPER(@publisher) collate database_default
IF @@ERROR <> 0
RETURN 1

--
-- If distribution db is NULL, there is no matching distributor.
-- This typically would happen when calling on a distributor that
-- only has HREPL publishers. This case should result in no output
--
IF (@loc_distribdb IS NULL)
BEGIN
RETURN (0)
END

-- Security. Connection to remote distributor must have 'sysadmin' or 'db_owner'
-- in distribution database authorization. This prevents user from bypassing
-- security checks by explicitly setting 'local' parameter in the call.
if LOWER(@local) = 'local'
begin
exec @retcode = sys.sp_MSrepl_isdbowner @loc_distribdb
if @retcode <> 1 or @@error <> 0
RETURN (1)
end

SELECT @loc_mindistretention = min_distretention,
@loc_maxdistretention = max_distretention,
@loc_historyretention = history_retention
FROM msdb.dbo.MSdistributiondbs
WHERE name = @loc_distribdb collate database_default

--
-- Fetch the distribution account name.
--
IF ((@distributor = '%' AND @distribdb = '%' AND @directory = '%'
AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1
AND @history_retention = -1 AND @history_cleanupagent = '%'
AND @distrib_cleanupagent = '%' AND @publisher_type = '%' AND @rpcsrvname = '%' )
OR @account IS NULL) and ( platform() & @platform_nt = @platform_nt ) and ( @has_dbowner_access = 1 )
BEGIN
declare @instance sysname
declare @regkey nvarchar(260)
-- not changing for instapi work. hardcoding this path
select @instance = convert(sysname, SERVERPROPERTY('InstanceName'))
select @regkey = 'SYSTEM\CurrentControlSet\Services\'
-- default installation
if @instance is null
SELECT @regkey = @regkey + 'SQLServerAgent'
else
SELECT @regkey = @regkey + 'SQLAgent$' + @instance

SELECT @proc = 'master.dbo.xp_regread'
EXECUTE @retcode = @proc 'HKEY_LOCAL_MACHINE',
@regkey,
'ObjectName',
@param = @loc_account OUTPUT
IF @@ERROR <> 0 OR @retcode <> 0
SELECT @loc_account = NULL
END

--
-- Fetch the history cleanup agentname.
--
IF @loc_distribdb IS NOT NULL
SELECT @loc_historycleanupagent = formatmessage (20567, @loc_distribdb)

--
-- Fetch the distribution cleanup agent name.
--
IF @loc_distribdb IS NOT NULL
SELECT @loc_distribcleanupagent = formatmessage (20568, @loc_distribdb)


DONE:

--
-- If user does not have 'db_owner' authorization, NULL restricted return parameters.
--
if @has_dbowner_access = 0
begin
select @loc_account = NULL
select @loc_mindistretention = NULL
select @loc_historyretention = NULL
select @loc_historycleanupagent = NULL
select @loc_distribcleanupagent = NULL
select @rpcsrvlogin = NULL
end

--
-- If @directory is to be returned and user does not have 'db_owner' access, check for PAL access.
--
IF ((@distributor = '%' AND @distribdb = '%' AND @directory = '%'
AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1
AND @history_retention = -1 AND @history_cleanupagent = '%'
AND @distrib_cleanupagent = '%' AND @rpcsrvname = '%' and @publisher_type = '%')
OR ( @directory is NULL ))
AND ( LOWER(@local) <> 'local' or @local is NULL )
AND ( @has_dbowner_access = 0 )
AND (@loc_rpcsrvname is not null and @loc_distribdb is not null)
begin
-- Check to determine whether the current user is in the PAL
-- of any publication that makes use of this publisher.
create table #pub (publisher_db sysname, publication sysname)

SELECT @proc = RTRIM(@loc_rpcsrvname) + '.' + RTRIM(@loc_distribdb) + '.sys.sp_MSpublication_access'
INSERT into #pub (publisher_db, publication)
EXEC @retcode = @proc
@publisher = @publisher,
@operation = N'get_publications',
@login = @login

if not exists (select * from #pub)
select @loc_directory = NULL
end

--
-- Return result set if no output parameters
--

IF (@distributor = '%' AND @distribdb = '%' AND @directory = '%'
AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1
AND @history_retention = -1 AND @history_cleanupagent = '%'
AND @distrib_cleanupagent = '%' AND @rpcsrvname = '%' and @publisher_type = '%')
SELECT 'distributor' = @loc_distributor,
'distribution database' = @loc_distribdb,
'directory' = @loc_directory,
'account' = @loc_account,
'min distrib retention' = @loc_mindistretention,
'max distrib retention' = @loc_maxdistretention,
'history retention' = @loc_historyretention,
'history cleanup agent' = @loc_historycleanupagent,
'distribution cleanup agent' = @loc_distribcleanupagent,
'rpc server name' = @loc_rpcsrvname,
'rpc login name' = @rpcsrvlogin,
'publisher type' = @loc_publishertype

--
-- Return output parameters if requested.
--

IF @distributor IS NULL
SELECT @distributor = @loc_distributor
IF @distribdb IS NULL
SELECT @distribdb = @loc_distribdb
IF @directory IS NULL
SELECT @directory = @loc_directory
IF @account IS NULL
SELECT @account = @loc_account
IF @min_distretention IS NULL
SELECT @min_distretention = @loc_mindistretention
IF @max_distretention IS NULL
SELECT @max_distretention = @loc_maxdistretention
IF @history_retention IS NULL
SELECT @history_retention = @loc_historyretention
IF @history_cleanupagent IS NULL
SELECT @history_cleanupagent = @loc_historycleanupagent
IF @distrib_cleanupagent IS NULL
SELECT @distrib_cleanupagent = @loc_distribcleanupagent
IF @publisher_type IS NULL
SELECT @publisher_type = @loc_publishertype

IF @rpcsrvname IS NULL
BEGIN
--
-- BUGBUG : The value for @rpcsrvname must match the value returned by
-- sp_MSrepl_getdistributorinfo or we will see indefinite blocking
-- in some areas of our code. Example-incremental add article. So
-- if you make a change here make it in sp_MSrepl_getdistributorinfo
--
-- For the following cases use LOCAL SERVER NAME
-- 1. Hetero will always use local server
-- 2. Local distributor with sysadmin access (used to avoid blocking issues)
IF @loc_publishertype != N'MSSQLSERVER'
OR (IS_SRVROLEMEMBER('sysadmin') = 1
AND UPPER(@loc_distributor) = UPPER(@@SERVERNAME))
BEGIN
SELECT @rpcsrvname = srvname
FROM master.dbo.sysservers
WHERE UPPER(srvname collate database_default ) = UPPER(@loc_distributor)
END
-- Remote distributor or local with non-sysadmin rights
ELSE
BEGIN
SELECT @rpcsrvname = @loc_rpcsrvname
END
END

RETURN (0)
END

Total Pageviews