Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Easy query

Posted on 2000-04-08
3
Medium Priority
?
246 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 90 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

610 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