I took some code here: http://stackoverflow.com/questions/2193598/check-if-role-consists-of-particular-user-in-db
SELECT *
FROM sys.database_role_members AS RM
JOIN sys.database_principals AS U
ON RM.member_principal_id = U.principal_id
JOIN sys.database_principals AS R
ON RM.role_principal_id = R.principal_id
WHERE U.name = 'operator1'
AND R.name = 'myrole1'
that query returns 1 row. It means that user 'operator1' belongs to role 'myrole1'. Now I'm trying to create a stored procedure for this:
CREATE PROCEDURE [dbo].[Proc1]
(
@userName varchar,
@roleName varchar
)
AS
IF EXISTS(SELECT *
FROM sys.database_role_members AS RM
JOIN sys.database_principals AS U
ON RM.member_principal_id = U.principal_id
JOIN sys.database_principals AS R
ON RM.role_principal_id = R.principal_id
WHERE U.name = @userName
AND R.name = @roleName)
RETURN 0
ELSE RETURN -1
I use standard command from sql server 2008 'Execute stored procedure'
DECLARE @return_value int
EXEC @return_value = [dbo].[Proc1]
@userName = N'operator1',
@roleName = N'myrole1'
SELECT 'Return Value' = @return_value
GO
it always returns -1. WHY ???