April 22, 2012

sp_dropserver (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_dropserver(nvarchar @server
, char @droplogins)

MetaData:

 create procedure sys.sp_dropserver  
@server sysname, -- server name
@droplogins char(10) = NULL -- drop all related logins?
as
declare @ret int, @is_linked bit, @server_id int
select @droplogins = LOWER(@droplogins collate Latin1_General_CI_AS)

-- DISALLOW USER TRANSACTION
set implicit_transactions off
if @@trancount > 0
begin
raiserror(15002,-1,-1,'sys.sp_dropserver')
return (1)
end

-- CHECK PERMISSIONS
if not (has_perms_by_name(null, null, 'alter any linked server') = 1)
begin
EXEC %%System().AuditEvent(ID = 1380209484, Success = 0, TargetLoginName = NULL, TargetUserName = NULL, Role = NULL, Object = NULL, Provider = NULL, Server = @server)
raiserror(15247,-1,-1)
return (1)
end
else
begin
EXEC %%System().AuditEvent(ID = 1380209484, Success = 1, TargetLoginName = NULL, TargetUserName = NULL, Role = NULL, Object = NULL, Provider = NULL, Server = @server)
end

-- VALIDATE OPTION
if @droplogins is not null AND @droplogins <> 'droplogins'
begin
raiserror(15600,-1,-1, 'sys.sp_dropserver')
return (1)
end

begin transaction

-- CHECK/LOCK SERVER NAME / GET SERVER ID
EXEC %%LinkedServer ( Name = @server ) . Lock ( Exclusive = 1 )
IF @@ERROR <> 0
begin
rollback tran
raiserror(15015,-1,-1,@server)
return (1)
end

-- CHECK @droplogins PARAMETER: No sysremotelogins OR sysoledbusers ROWS EXCEPT
-- THE DEFAULT OLEDB-MAPPING.
if @droplogins is null AND exists (
SELECT * FROM master.dbo.sysservers s WHERE s.srvname = @server AND
( exists (select * from master.dbo.sysremotelogins WHERE remoteserverid = s.srvid)
OR exists (select * from master.dbo.sysoledbusers WHERE rmtsrvid = s.srvid
AND NOT (loginsid IS NULL AND status = 1) )
) )
begin
rollback tran
raiserror(15190,-1,-1,@server)
return (1)
end

-- CHECK TO SEE IF THE SERVER IS USED BY REPLICATION.
if object_id('sp_MSrepl_check_server') is not null
begin
EXEC @ret = sp_MSrepl_check_server @server
if @ret <> 0 or @@error <> 0
begin
rollback tran
return 1
end
end

-- is this a linked server?
select @is_linked = is_linked, @server_id = server_id from sys.servers where name = @server

-- DROP THE SERVER (ALSO DROPS ALL REMOTE/LINKED LOGINS)
EXEC %%LinkedServer ( Name = @server ) . Remove()

-- Server_id = 0 means the local server name
if @server_id = 0
begin
-- EMDEventType(x_eet_Alter_Instance), EMDUniversalClass(x_eunc_Server), src major id, src minor id, src name
-- -1 means ignore target stuff, target major id, target minor id, target name,
-- # of parameters, 5 parameters
EXEC %%System().FireTrigger(ID = 214, ID = 100, ID = 0, ID = 0, Value = NULL,
ID = -1, ID = 0, ID = 0, Value = NULL,
ID = 2, Value = @server, Value = @droplogins, Value = NULL, Value = NULL, Value = NULL, Value = NULL, Value = NULL)
end
else if @is_linked = 1
begin
-- EMDEventType(x_eet_Drop_LinkedServer), EMDUniversalClass(x_eunc_LinkedServer), src major id, src minor id, src name
-- -1 means ignore target stuff, target major id, target minor id, target name,
-- # of parameters, 5 parameters
EXEC %%System().FireTrigger(ID = 262, ID = 102, ID = 0, ID = 0, Value = @server,
ID = -1, ID = 0, ID = 0, Value = NULL,
ID = 2, Value = @server, Value = @droplogins, Value = NULL, Value = NULL, Value = NULL, Value = NULL, Value = NULL)
end
else
begin
-- EMDEventType(x_eet_Drop_Remote_Server), EMDUniversalClass(x_eunc_Server), src major id, src minor id, src name
-- -1 means ignore target stuff, target major id, target minor id, target name,
-- # of parameters, 5 parameters
EXEC %%System().FireTrigger(ID = 240, ID = 100, ID = 0, ID = 0, Value = @server,
ID = -1, ID = 0, ID = 0, Value = NULL,
ID = 2, Value = @server, Value = @droplogins, Value = NULL, Value = NULL, Value = NULL, Value = NULL, Value = NULL)
end
-- SUCCESS
commit transaction
return (0) -- sp_dropserver

No comments:

Post a Comment

Total Pageviews