Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5965925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:42:40+00:00 2026-05-22T19:42:40+00:00

I am trying to create a user via a T-SQL script in SQL Server

  • 0

I am trying to create a user via a T-SQL script in SQL Server 2005/2008. I run the following SQL to check if the user exists and create the user if it does not:

IF NOT EXISTS (SELECT * 
               FROM   sys.database_principals 
               WHERE  name = N'MyDomain\MyUser') 
  BEGIN 
      CREATE USER [MyDomain\MyUser] FOR LOGIN [MyDomain\MyUser] WITH default_schema=[dbo]; 

      SELECT 1; 
  END 
ELSE 
  BEGIN 
      SELECT 0; 
  END  

Unfortunately, this does not work when the windows account MyDomain\MyUser is already in the database but under a different name. An example of this would be if MyDomain\MyUser was the account that created the database then it will already be in the database under the dbo user. When this is the case I get the following error:

Msg 15063, Level 16, State 1, Line 1
The login already has an account under
a different user name.

How can I check if the windows user MyDomain\MyUser is already a user in the database even if it is under a different user name?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:42:41+00:00Added an answer on May 22, 2026 at 7:42 pm

    Do the testing for windows user vs sys.database_principals using the SID rather than the name/user_id:

    select * from sys.database_principals where sid = SUSER_SID(N'MyDomain\MyUser')
    

    Since this uses the SID of the login ID, it doesn’t matter what name it was mapped to in the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we are trying to create C# CLR Stored Procedure on a SQL Server 2005
I'm trying to execute CREATE USER with a given username (via sql parameter) exec
Alright wasted hours and hours trying to get MSSQL Server 2008 to login via
Trying to create a user account in a test. But getting a Object reference
I'm trying to create a user control that allows users to make something like
I am trying to create a user interface using XAML. However, the file is
I am trying to create a winForms user control. But I want would like
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a list to return some JSON data to a view. Following
I'm trying to run one SQL command to update multiple databases. If I copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.