Check that user belongs to db role
up vote
1
down vote
favorite
I took some code here: 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 ???
tsql
add a comment |
up vote
1
down vote
favorite
I took some code here: 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 ???
tsql
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I took some code here: 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 ???
tsql
I took some code here: 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 ???
tsql
tsql
edited May 23 '17 at 12:07
Community♦
11
11
asked Feb 25 '10 at 8:16
Alexander Stalt
43751321
43751321
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You need to define the length attribute for each of your procedure parameters. If you change the beginning of your stored procedure declaration to the following, it will return the correct response:
CREATE PROCEDURE [dbo].[Proc1]
(
@userName varchar(255),
@roleName varchar(255)
)
AS
Without the length attributes, SQL Server auto-defines the length of the variables as 1, so the user and role were being matched against "o" and "m", respectively. SQL Server is inconsistent in it's behavior of how it treats variables without the length specifier - sometimes they have a length of 30 and sometimes they have a length of 1. It is best practice to always specify the length attributes on string variables. See the following SQL Blog article for more information:
- Bad habits to kick : declaring VARCHAR without (length)
Thanks for an input, Templar. Sorry for long-time answer. I didn't know that I should mark your answer as useful and other things ...
– Alexander Stalt
Mar 29 '10 at 13:21
add a comment |
up vote
0
down vote
This code is not good, because it can return false, if a user is member of a group, which then is member of a role.
User the built-in function IS_MEMBER() -- this is much simpler and always accureate.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to define the length attribute for each of your procedure parameters. If you change the beginning of your stored procedure declaration to the following, it will return the correct response:
CREATE PROCEDURE [dbo].[Proc1]
(
@userName varchar(255),
@roleName varchar(255)
)
AS
Without the length attributes, SQL Server auto-defines the length of the variables as 1, so the user and role were being matched against "o" and "m", respectively. SQL Server is inconsistent in it's behavior of how it treats variables without the length specifier - sometimes they have a length of 30 and sometimes they have a length of 1. It is best practice to always specify the length attributes on string variables. See the following SQL Blog article for more information:
- Bad habits to kick : declaring VARCHAR without (length)
Thanks for an input, Templar. Sorry for long-time answer. I didn't know that I should mark your answer as useful and other things ...
– Alexander Stalt
Mar 29 '10 at 13:21
add a comment |
up vote
2
down vote
accepted
You need to define the length attribute for each of your procedure parameters. If you change the beginning of your stored procedure declaration to the following, it will return the correct response:
CREATE PROCEDURE [dbo].[Proc1]
(
@userName varchar(255),
@roleName varchar(255)
)
AS
Without the length attributes, SQL Server auto-defines the length of the variables as 1, so the user and role were being matched against "o" and "m", respectively. SQL Server is inconsistent in it's behavior of how it treats variables without the length specifier - sometimes they have a length of 30 and sometimes they have a length of 1. It is best practice to always specify the length attributes on string variables. See the following SQL Blog article for more information:
- Bad habits to kick : declaring VARCHAR without (length)
Thanks for an input, Templar. Sorry for long-time answer. I didn't know that I should mark your answer as useful and other things ...
– Alexander Stalt
Mar 29 '10 at 13:21
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to define the length attribute for each of your procedure parameters. If you change the beginning of your stored procedure declaration to the following, it will return the correct response:
CREATE PROCEDURE [dbo].[Proc1]
(
@userName varchar(255),
@roleName varchar(255)
)
AS
Without the length attributes, SQL Server auto-defines the length of the variables as 1, so the user and role were being matched against "o" and "m", respectively. SQL Server is inconsistent in it's behavior of how it treats variables without the length specifier - sometimes they have a length of 30 and sometimes they have a length of 1. It is best practice to always specify the length attributes on string variables. See the following SQL Blog article for more information:
- Bad habits to kick : declaring VARCHAR without (length)
You need to define the length attribute for each of your procedure parameters. If you change the beginning of your stored procedure declaration to the following, it will return the correct response:
CREATE PROCEDURE [dbo].[Proc1]
(
@userName varchar(255),
@roleName varchar(255)
)
AS
Without the length attributes, SQL Server auto-defines the length of the variables as 1, so the user and role were being matched against "o" and "m", respectively. SQL Server is inconsistent in it's behavior of how it treats variables without the length specifier - sometimes they have a length of 30 and sometimes they have a length of 1. It is best practice to always specify the length attributes on string variables. See the following SQL Blog article for more information:
- Bad habits to kick : declaring VARCHAR without (length)
edited Nov 11 at 14:04
Aaron Bertrand
206k27361402
206k27361402
answered Mar 6 '10 at 4:28
Templar
3,31972737
3,31972737
Thanks for an input, Templar. Sorry for long-time answer. I didn't know that I should mark your answer as useful and other things ...
– Alexander Stalt
Mar 29 '10 at 13:21
add a comment |
Thanks for an input, Templar. Sorry for long-time answer. I didn't know that I should mark your answer as useful and other things ...
– Alexander Stalt
Mar 29 '10 at 13:21
Thanks for an input, Templar. Sorry for long-time answer. I didn't know that I should mark your answer as useful and other things ...
– Alexander Stalt
Mar 29 '10 at 13:21
Thanks for an input, Templar. Sorry for long-time answer. I didn't know that I should mark your answer as useful and other things ...
– Alexander Stalt
Mar 29 '10 at 13:21
add a comment |
up vote
0
down vote
This code is not good, because it can return false, if a user is member of a group, which then is member of a role.
User the built-in function IS_MEMBER() -- this is much simpler and always accureate.
add a comment |
up vote
0
down vote
This code is not good, because it can return false, if a user is member of a group, which then is member of a role.
User the built-in function IS_MEMBER() -- this is much simpler and always accureate.
add a comment |
up vote
0
down vote
up vote
0
down vote
This code is not good, because it can return false, if a user is member of a group, which then is member of a role.
User the built-in function IS_MEMBER() -- this is much simpler and always accureate.
This code is not good, because it can return false, if a user is member of a group, which then is member of a role.
User the built-in function IS_MEMBER() -- this is much simpler and always accureate.
answered Aug 29 '14 at 13:52
user872744
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2332544%2fcheck-that-user-belongs-to-db-role%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown