3

I want the user of my SSDT Database Project to be created as a user in another database on the same server and give it permissions to connect and add it to the db_datareader role.

I failed to do so. The PostDeployment Script complains about:

  • USE command is not allowed
  • not knowing the Create User OtherDB.USERNAME Syntax

How can this be realized? Isn't there a script option where I can place any command I want?

Solomon Rutzky
70.1k8 gold badges160 silver badges306 bronze badges
asked Aug 26, 2015 at 11:50
0

1 Answer 1

3

The action described in the question should not be a problem. I was able to create a Post Deployment script and add the following code:

USE [tempdb];
GO
PRINT DB_NAME();
IF (USER_ID(N'g') IS NULL)
BEGIN
 PRINT 'Creating user [g]...';
 CREATE USER [g] WITHOUT LOGIN;
END;
ELSE
BEGIN
 PRINT 'Dropping user [g]...';
 DROP USER [g];
END;

It builds and publishes with no errors, and executes the commands as expected.

Make sure that your SQL script is truly a Post Deployment script. If it is set to Build Script then it probably will get those errors. Look at the properties of the script and make sure that Build Action is set to PostDeploy.

And please make sure that you are using the latest update of SSDT. The most recent version can be found here:

Download Latest SQL Server Data Tools

answered Aug 26, 2015 at 15:11
4
  • TY. sp_addrolemember works in post deployment. Question: Why do you create the whole user in de PDS instead of making the object part of the project? Commented Aug 31, 2015 at 14:52
  • 1
    @Magier The reason I created the user in the PDS was due to it being in a different database, a database that is not part of my solution. That and I was going based on your statement of "PostDeployment Script complains about not knowing the Create User OtherDB.USERNAME Syntax.". But if you can make it part of the project then that is fine too. I was mainly showing that you could indeed use a USE statement in a PDS. Commented Aug 31, 2015 at 15:02
  • @SolomonRutzky should I use a post deployment script to create a role and grant permissions on views and SP's to this role? Commented Mar 14, 2019 at 17:04
  • @McNets You should be able to include a Role as part of the Database Project. For permissions, that might need to be a post deployment script. I am not sure if there is a way to handle permissions via SSDT, and I don't have time to test right now. Commented Mar 14, 2019 at 17:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.