Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_addrole

  No additional text.


Syntax
create procedure sys.sp_addrole
    @rolename   sysname,        -- name of new role
    @ownername  sysname = NULL  -- name of owner of new role
as
    -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES 
	set nocount on
	declare @ret        int,    -- return value of sp call
            @owner      int,
			@stmtR		nvarchar(4000),
			@stmtS		nvarchar(4000)

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

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

    if @ownername is null
        select @ownername = user_name()

	set @stmtR = 'create role '
	set @stmtR = @stmtR + quotename(@rolename, ']')
	set @stmtR = @stmtR + ' authorization '
	set @stmtR = @stmtR + quotename(@ownername, ']')

	-- Create Schema if not one exists with same name and owner
	set @stmtS = 'create schema '
	set @stmtS = @stmtS + quotename(@rolename, ']')
	set @stmtS = @stmtS + ' authorization '
	set @stmtS = @stmtS + quotename(@rolename, ']')
	
	BEGIN TRANSACTION

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


	-- create the Schema if not one exists with same name and owner
	declare @schema_uid int
	declare @owner_uid  int
	select @owner_uid  = principal_id from sys.database_principals where name = @rolename
	select @schema_uid = principal_id from sys.schemas where name = @rolename
	if (@schema_uid is null or		-- there is no schema since if there is one, it will have an owner
		@schema_uid <> @owner_uid)  -- for error message
	begin
		exec (@stmtS)
		if @@error <> 0
		begin
			ROLLBACK TRANSACTION
			return (1)
		end
	end

	COMMIT TRANSACTION

    -- RETURN SUCCESS 
    return (0) -- sp_addrole

 
Last revision 2008RTM
See also

  sp_addgroup (Procedure)
sp_addrolemember (Procedure)
sp_adduser (Procedure)
sp_cdc_disable_db_internal (Procedure)
sp_cdc_enable_db_internal (Procedure)
sp_changegroup (Procedure)
sp_grant_publication_access (Procedure)
sp_MScreate_dist_tables (Procedure)
sp_MSmergepublishdb (Procedure)
sp_MSprocesspublisherlink (Procedure)
sp_MSreplrole (Procedure)
sp_MSrepl_dbrole (Procedure)
sp_MSrepl_FixPALRole (Procedure)
sp_MSrepl_FixTranPALRole (Procedure)
sp_sysutility_mi_configure_proxy_account (Procedure)
sp_sysutility_ucp_provision_proxy_account (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash