Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top