Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_changegroup

 

Changegroup is no longer supported in SQL 2008. Use sp_addrolemember and sp_droprolemember instead.




Syntax
create procedure sys.sp_changegroup
    @grpname    sysname,    -- name of new role
    @username   sysname     -- user to switch
as
    -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES
	set nocount on
	declare @uid        int,
			@ruid       int,
            @cnt        int,
            @ret        int,
            @oldgrp     sysname

  	select @ruid = principal_id from sys.database_principals
                where name = @grpname and type = 'R'

	-- ERROR IF GROUP NOT FOUND
    if @ruid is null
    begin
	    raiserror(15014,-1,-1,@grpname)
	    return (1)
    end

    -- LIMIT TO USERS WITH ACCESS (BACKWARD COMPAT ONLY!)
    select @uid = principal_id from sys.database_principals u
    				join sys.database_permissions p on p.class = 0 and p.major_id = 0 and p.minor_id = 0
    				and p.grantee_principal_id = u.principal_id and p.grantor_principal_id = 1 and p.type = 'CO'
		where u.name = @username and u.type in ('S', 'U') and p.state in ('G', 'W')
    if @uid is null
	begin
		raiserror(15008,-1,-1,@username)
		return (1)
	end

    -- ONLY VALID IF USER IS MEMBER OF NO MORE THAN ONE GROUP
    select @cnt = count(*) from sys.database_role_members where member_principal_id = @uid
    if @cnt > 1
    begin
	    raiserror(15415, -1, -1)
	    return (1)
    end

	-- AUDIT SUCCESSFUL SECURITY CHECK
	dbcc auditevent (110, 3, 1, NULL, @username, @grpname, NULL, NULL, NULL, NULL)

    -- REMOVE MEMBERSHIP IF NEEDED
	if (@cnt = 1)
	begin
        select @oldgrp = user_name(role_principal_id) from sys.database_role_members where member_principal_id = @uid
        EXEC @ret = sys.sp_droprolemember @oldgrp, @username
        if @ret <> 0
            return (1)
	end

    -- ADD MEMBERSHIP
    if (@grpname <> 'public')
    begin
        EXEC @ret = sys.sp_addrolemember @grpname, @username
        if @ret <> 0
            return (1)
    end

    -- RETURN SUCCESS
	return (0) -- sp_changegroup

 
Last revision
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