Create user

I came across this code.

if not exists (select * from dbo.sysusers where name = N'name' and uid < 16382)

EXEC sp_grantdbaccess N'Name', N'Name'

GO

i was wondering if anyone can tell me the significance of uid < 16382

thanks



Answer this question

Create user

  • Mark Phelps

    Run the query:

    select * from dbo.sysusers --in 2000 and earlier

    The database roles will start at 16383, users are less than this.

    This isn't the case in 2005 (it is all I have to test on now, and it puts them all together and you can see them as different based on metadata.)

    Louis



  • Bailey58642

    Thanks.

    I ran the query on both 2000 and 2005.

    Database roles in 2005 start at 16383 as well.


  • Wilson Lim

    The fixed roles are the ones that start at 16383, but user ones are mixed in. I think this is some ancient holdover because of how 6.5 did it, or maybe 7.0.

    Either way, in 2000 and 2005, they have columns in sysusers to tell you if it is a role or a user:

    --2000
    select
    name, uid
    from sysusers where issqlrole = 0

    --2005
    select name, principal_id
    from sys.database_principals
    where type_desc = 'database_role'

    Or, using a compatibilty view:

    select name, uid
    from sys.sysusers where issqlrole = 0

    The < 16382 thing would eliminate fixed database roles, but not reliably if you have a really large number of users. In 2005, they have a column in the database_principals view of is_fixed_role:

    select name, principal_id
    from sys.database_principals
    where type_desc = 'database_role'
    and is_fixed_role = 0



  • Jackey Cheung

    Where did you find it

  • Create user