1

I'm configuring a pipeline to deploy a test environment for an app, along with app services and storage there is a SQL database. I have already automated the creation of all of them in azure and is working.

After the test is finished, the environment is destroyed to save resources until a new test is required.

Now I need to add a SQL user to the test database while deploying, it's on the same server, to simplify the login already exists, so I want to assign the same to the new database.

Something like this.

  • Server1 already has user1 as login (was manually created)

  • Database1 already has user1 as user (was manually assigned)

Desired:

  • Add User1 as user of database2

There is a way to automate this while building the test environment?

Thanks in advance.

1 Answer 1

1

You can go for IF NOT EXISTS and create login and user accordingly.

IF NOT EXISTS(SELECT name 
                FROM [sys].[server_principals]
                WHERE name = N'test')
 BEGIN
    CREATE LOGIN test WITH password = 'TestPassword'
 END

 IF NOT EXISTS(SELECT name 
                FROM [sys].[database_principals]
                WHERE name = N'test')
 BEGIN
    CREATE USER test for login test;
 END
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Venkataraman R thanks for your sugestion, it's working now, but instead of server_principals am using sys.sql_logins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.