Showing posts with label behavior. Show all posts
Showing posts with label behavior. Show all posts

Friday, March 9, 2012

How can one user view other users that belong to a database role?

After upgrading my database from SQL2000 to SQL2005 I have noticed a change in behavior of sp_helprolemember.

In SQL2000 I could connect as 'user1' and use sp_helprolemember to find all users that belong to a certain role.

In SQL2005 sp_helprolemember seems to only show me the roles that connected user belongs to. For example, if I connect as 'user1' I only see the roles that 'user1' belongs to.

Any advice on how to duplicate the behavior from SQL2000?

The procedure relies on the security catalog views which are user sensitive, the query which is executed behind the scenes will bring back only the roles for the user.

select DbRole = g.name, MemberName = u.name, MemberSID = u.sid

from sys.database_principals u, sys.database_principals g, sys.database_role_members m

where g.principal_id = m.role_principal_id

and u.principal_id = m.member_principal_id

order by 1, 2

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Thanks for the post, however you've only explained why it's behaving as it does. I'm looking for advice on duplicating the Sql2000 behavior. For example, if I log on as user A I'd like to know if user B is a member of role ABC.|||Look what the Workbench does behind the scenes, it will execute the following commands:

set NOCOUNT OFF

CREATE TABLE #tmp_role_member_ids (id int not null, role_id int null, sub_role_id int null, generation int null)

declare @.generation int

set @.generation = 0

INSERT INTO #tmp_role_member_ids (id) SELECT

rl.principal_id AS [ID]

FROM

sys.database_principals AS rl

WHERE

(rl.type = 'R')and(rl.name=N'db_owner')

UPDATE #tmp_role_member_ids SET role_id = id, sub_role_id = id, generation=@.generation

WHILE ( 1=1 )

BEGIN

INSERT INTO #tmp_role_member_ids (id, role_id, sub_role_id, generation)

SELECT a.member_principal_id, b.role_id, a.role_principal_id, @.generation + 1

FROM sys.database_role_members AS a INNER JOIN #tmp_role_member_ids AS b

ON a.role_principal_id = b.id

WHERE b.generation = @.generation

if @.@.ROWCOUNT <= 0

break

set @.generation = @.generation + 1

END

DELETE #tmp_role_member_ids WHERE id in (SELECT

rl.principal_id AS [ID]

FROM

sys.database_principals AS rl

WHERE

(rl.type = 'R')and(rl.name=N'db_owner') )

UPDATE #tmp_role_member_ids SET generation = 0;

INSERT INTO #tmp_role_member_ids (id, role_id, generation)

SELECT distinct id, role_id, 1 FROM #tmp_role_member_ids

DELETE #tmp_role_member_ids WHERE generation = 0

SELECT

u.name AS [Name]

FROM

sys.database_principals AS rl

INNER JOIN #tmp_role_member_ids AS m ON m.role_id=rl.principal_id

INNER JOIN sys.database_principals AS u ON u.principal_id = m.id

WHERE

(rl.type = 'R')and(rl.name=N'db_owner')

ORDER BY

[Name] ASC

drop table #tmp_role_member_ids

HTH, Jens K. Suessmeyer.

-
http://www.sqlserver2005.de
-

How can one user view other users that belong to a database role?

After upgrading my database from SQL2000 to SQL2005 I have noticed a change in behavior of sp_helprolemember.

In SQL2000 I could connect as 'user1' and use sp_helprolemember to find all users that belong to a certain role.

In SQL2005 sp_helprolemember seems to only show me the roles that connected user belongs to. For example, if I connect as 'user1' I only see the roles that 'user1' belongs to.

Any advice on how to duplicate the behavior from SQL2000?

The procedure relies on the security catalog views which are user sensitive, the query which is executed behind the scenes will bring back only the roles for the user.

select DbRole = g.name, MemberName = u.name, MemberSID = u.sid

from sys.database_principals u, sys.database_principals g, sys.database_role_members m

where g.principal_id = m.role_principal_id

and u.principal_id = m.member_principal_id

order by 1, 2

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Thanks for the post, however you've only explained why it's behaving as it does. I'm looking for advice on duplicating the Sql2000 behavior. For example, if I log on as user A I'd like to know if user B is a member of role ABC.|||Look what the Workbench does behind the scenes, it will execute the following commands:

set NOCOUNT OFF

CREATE TABLE #tmp_role_member_ids (id int not null, role_id int null, sub_role_id int null, generation int null)

declare @.generation int

set @.generation = 0

INSERT INTO #tmp_role_member_ids (id) SELECT

rl.principal_id AS [ID]

FROM

sys.database_principals AS rl

WHERE

(rl.type = 'R')and(rl.name=N'db_owner')

UPDATE #tmp_role_member_ids SET role_id = id, sub_role_id = id, generation=@.generation

WHILE ( 1=1 )

BEGIN

INSERT INTO #tmp_role_member_ids (id, role_id, sub_role_id, generation)

SELECT a.member_principal_id, b.role_id, a.role_principal_id, @.generation + 1

FROM sys.database_role_members AS a INNER JOIN #tmp_role_member_ids AS b

ON a.role_principal_id = b.id

WHERE b.generation = @.generation

if @.@.ROWCOUNT <= 0

break

set @.generation = @.generation + 1

END

DELETE #tmp_role_member_ids WHERE id in (SELECT

rl.principal_id AS [ID]

FROM

sys.database_principals AS rl

WHERE

(rl.type = 'R')and(rl.name=N'db_owner') )

UPDATE #tmp_role_member_ids SET generation = 0;

INSERT INTO #tmp_role_member_ids (id, role_id, generation)

SELECT distinct id, role_id, 1 FROM #tmp_role_member_ids

DELETE #tmp_role_member_ids WHERE generation = 0

SELECT

u.name AS [Name]

FROM

sys.database_principals AS rl

INNER JOIN #tmp_role_member_ids AS m ON m.role_id=rl.principal_id

INNER JOIN sys.database_principals AS u ON u.principal_id = m.id

WHERE

(rl.type = 'R')and(rl.name=N'db_owner')

ORDER BY

[Name] ASC

drop table #tmp_role_member_ids

HTH, Jens K. Suessmeyer.

-
http://www.sqlserver2005.de
-

how can NULL be equal to todays date (was "Weird Query behavior plz help")

I have a query that is behaving a little a weird. here is the example:

i have 1 table in this table i have 2 columns wich are date and time

DATE_DEBUT_PERIODE_FISCALE DATE_FIN_PERIODE_FISCALE
---------- --------
1/27/1997 2/27/1997
1/1/2005 2/6/2005

here is my query:

BEGIN
declare @.datefin_flag datetime, @.strip datetime
SELECT @.strip = dateadd(d,datediff(d,0,getdate()),0)
SELECT @.datefin_flag = DATE_FIN_PERIODE_FISCALE FROM DM_LKP_CALENDRIER_PERIODE_F
WHERE DATE_DEBUT_PERIODE_FISCALE < @.strip AND DATE_FIN_PERIODE_FISCALE = @.strip
--select @.datefin_flag
--select @.strip
IF(@.datefin_flag != @.strip)
RAISERROR('You cant run this',16,1)
END

Well this Query should return the raiserror it returns completes successfuly
since todays date is not the same as the date in the database.
if you select @.datefin_flag it returns NULL and if you select @.strip it brings back todays date how can NULL be equal to to todays date assuming that todays date is equal to NULL. ?Your code is checking for NOT equal. NULL is not equal to anything (including another NULL).

-PatP|||Crap...didnt think of that how would i go about fixing this based on my expresion since if it looks in the table and doesnt find anything.|||How about:IF(@.datefin_flag != @.strip OR @.datein_flag IS NULL)
RAISERROR('You cant run this',16,1)
END-PatP|||I'd write it this way...

IF NOT EXISTS( SELECT *
FROM DM_LKP_CALENDRIER_PERIODE_F
WHERE DATE_DEBUT_PERIODE_FISCALE < dateadd(d,datediff(d,0,getdate()),0)
AND DATE_FIN_PERIODE_FISCALE = dateadd(d,datediff(d,0,getdate()),0)
)
RAISERROR('You cant run this',16,1)
END

But I'd handle the raise a little differently

http://weblogs.sqlteam.com/brettk/archive/2004/05/25/1378.aspx|||Thanks alot Pat worked fine i realy apreciated.|||thanks also Brett that is a realy good alternative|||Your welcome...

But it's not really an alternative...