Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error that SQL Login already exists

I have created 2 SQL Server Database Projects in VS 2013 and imported schemas from 2 databases that reside on the same SQL instance. Both databases have SQL Users, let's say [MyUser1] and [MyUser2], that use a single SQL Login, let's say [MyLogin]. The issue is both projects want to create the SQL Login and this causes the error

"SQL71508 The model already has an element that has the same name MyLogin"

DB1 Project

CREATE LOGIN [MyLogin] WITH PASSWORD = N'xyz';

CREATE USER [MyUser1] FOR LOGIN [MyLogin];

DB2 Project

CREATE LOGIN [MyLogin] WITH PASSWORD = N'xyz';

CREATE USER [MyUser2] FOR LOGIN [MyLogin];

I've tried:

  1. Removing CREATE LOGIN from one of the projects. Issue is then CREATE USER generates an error because it wants the CREATE LOGIN for the Login to exist.
  2. To find a way to ignore the error, but all I've found is how to ignore warnings.
  3. Using the following to check if Login already exists, however an error is generated at the "If" indicating, "SQL700001 The statement is not recognized in this context".

    If Not Exists (Select name From master.sys.server_principals Where name = 'MyLogin') Begin CREATE LOGIN [MyLogin] WITH PASSWORD = N'xyz'; End

Does anyone have any thoughts on how to correct or work around this issue? Thanks!

Also, my primary reason for creating the DB2 project was due to views in DB1 that are based on tables in DB2. Without the DB2 project referenced in the DB1 project, the SQL to create the views generated errors about not finding the tables in DB2. So, if anyone has any thoughts on how to workaround this issue, that would be helpful too.

like image 685
Gilligan Avatar asked May 05 '26 10:05

Gilligan


1 Answers

Never add the same item twice in SSDT projects. Where the same item exists in more than one project, create a master project for the server.

Leave the users alone, just move the logins to a common project.

Think inheritance when designing SSDT projects. Do not forget to include the IncludeCompositeObjects switch for deployments of each individual database.

like image 138
HeXanon Avatar answered May 08 '26 02:05

HeXanon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!