T-SQL. Can the stored procedure add a new database user?



  • I'm putting in an annex where I want to add a new database user through SqlCommand. Can you tell me if you can do this?



  • In SQL Server, almost everything can be done through SQL Command.

    In all the standard management Studio dialogues, there's a script button that generates a violin for all the actions you've done in the dialogue:

    введите сюда описание изображения

    In order to create a user + user mapping on the base, it heals:

    USE [master]
    GO
    CREATE LOGIN [SomeUser] WITH PASSWORD=N'test', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
    GO
    USE [SomeDatabase]
    GO
    CREATE USER [SomeUser] FOR LOGIN [SomeUser]
    GO
    USE [SomeDatabase]
    GO
    ALTER USER [SomeUser] WITH DEFAULT_SCHEMA=[dbo]
    GO
    USE [SomeDatabase]
    GO
    ALTER ROLE [db_owner] ADD MEMBER [SomeUser]
    GO
    

    Accordingly, you need two teams. One based on master (as required):

    CREATE LOGIN [SomeUser] WITH PASSWORD=N'test', 
    DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
    

    Second, on the basis to which access is needed (may fulfil one SqlCommand challenge):

    CREATE USER [SomeUser] FOR LOGIN [SomeUser]
    ALTER USER [SomeUser] WITH DEFAULT_SCHEMA=[dbo]
    ALTER ROLE [db_owner] ADD MEMBER [SomeUser]
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2