Solved

Easy query

Posted on 2000-04-08
3
227 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
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 142

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now