Solved

Easy query

Posted on 2000-04-08
3
232 Views
Last Modified: 2010-05-18
Suppose I have a user name called "Tom" and a database called "testDB"
How can I add the user to the database by query?

I have tried "exec sp_adduser Tom, testDB", but it gives me
"[Microsoft][ODBC SQL Server Driver][SQL Server][ The Login already has
an account under a different user name".

why is it?

Brian
0
Comment
Question by:BrianKwan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Accepted Solution

by:
chigrik earned 30 total points
ID: 2697377
Read about sp_addlogin from SQL Server Books Online. This is correct syntax:

sp_addlogin 'you_login', 'you_password', 'pubs'
GO
sp_adduser 'you_login', 'Tom'
GO
sp_addrolemember 'db_owner', 'Tom'
GO
SELECT name FROM sysusers WHERE name = 'Tom'
GO

This is the results set:

"
New login created.
Granted database access to 'you_login'.
'Tom' added to role 'db_owner'.
name                                                                                                                            
--------------------------------------------------------------------------------------------------------------------------------
Tom

(1 row(s) affected)
"
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2699193
If the login TOM has already a user (whatever name) in the given database, you won't be able to add him a second time.
If you can't see the user TOM in the database, this may result from a backup of your database from a different server. you may then use the sp_dropuser command first.
Hope this helps
0
 
LVL 3

Expert Comment

by:ahoor
ID: 2699930
You have to execute sp_adduser in the target database and supply the username and loginname.

So to go further on chigrik's answer:

use master
go
sp_addlogin 'Tom','password', 'testDB'
go
use testDB
go
sp_adduser 'Tom','Tom'
go

You may be able to skip the addlogin if it already exists...
the use database  is the main issue I guess.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question