April 13, 2012

sp_addrolemember (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_addrolemember(nvarchar @rolename
, nvarchar @membername)

MetaData:

 --  FOR BACKWARD COMPATIBILTY ONLY --   
create procedure sys.sp_addrolemember
@rolename sysname,
@membername sysname
AS
-- SETUP RUNTIME OPTIONS / DECLARE VARIABLES --
set nocount on
declare @ret int, -- return value of sp call
@memuid int,
@stmtR nvarchar(4000)

-- Was a part of check to prevent inside a user Xact. Leave for backward compat.--
set implicit_transactions off

exec @ret = sys.sp_validname @rolename
if @ret <> 0
return(1)

exec @ret = sys.sp_validname @membername
if @ret <> 0
return(1)

BEGIN TRANSACTION

-- EXCL LOCK MEMBER --
EXEC %%Owner(Name = @membername).Lock(Exclusive = 1) -- may fail

-- ATTEMPT ADDING IMPLICIT ROW FOR NT NAME --
if @@error <> 0 -- not found
begin
EXEC @ret = sys.sp_MSadduser_implicit_ntlogin @membername
if (@ret <> 0)
begin
ROLLBACK TRANSACTION
raiserror(15410, -1, -1, @membername)
return (1)
end
-- Member locked by sp_MSadduser_implicit_ntlogin
end

set @stmtR = 'alter role '
set @stmtR = @stmtR + quotename(@rolename, ']')
set @stmtR = @stmtR + ' add member '
set @stmtR = @stmtR + quotename(@membername, ']')

-- issue DDL to create the role --
exec (@stmtR)
if @@error <> 0
begin
ROLLBACK TRANSACTION
return (1)
end

-- RETURN SUCCESS --
COMMIT TRANSACTION
return (0) -- sp_addrolemember

No comments:

Post a Comment

Total Pageviews