Restore SQL Server 2005 database without overwriting DB users - database

I have a database that I constantly need to restore.
Every time I do so, all the users that have access to DB are deleted and I have to manually add them again.
Is there any way that I can restore the DB but the users remain intact or any script that I run before/after the restore so that I don't have to manually add all the users again?

Before the restore, you can script the users as they currently exist. In object explorer, expand the database, expand security, highlight the users node and click f8 (object explorer details). In object explorer details, select the users you want to keep, right click and script as create to new window, clipboard etc.
Now run the restore, and when the restore is complete, run the script from above. If this is all happening on the same server, should work just fine as I described. If these are different servers, you may want to look into synchronizing server-level logins first.

Before you restore the database, use this script to script the logins and passwords: How to transfer logins and passwords between instances of SQL Server
Then, after you restore the database, execute the created script to re-create the logins

Related

Remove all SQL users from a DB

I have a database that I am migrating, but when I do the restore all the users of that database are also migrated.
Is there any way to do a restore of the DB only of the data? Without the users/roles
Or any way to remove all users from the DB?
(This users i want to remove)
In total there are 15BBDD with +50 users.
In Management Studio, you can right click on your database, select Tasks then Generate Scripts...
On Choose Objects screen choose users you want to remove, or all.
On Set scripting options page, go to Advanced, find Script DROP and CREATE and choose option to only Script DROP
Select option to open in a new query window.
Next, next, finish and you'll have your script to drop all users.

Who created a database in my SQL Server instance?

I'm trying to determine who created a database in my SQL Server instance. The .trc logs seem to have been purged and I can't locate a backup of them. I know when the database was created and have found the .bak file that was used to create the database, but I can't determine WHO created it.
Any other ideas how I can figure this out? (SSMS schema history report also doesn't go back far enough)
Based on the following article:
There is no dbo concept for server scope securables. They are always owned by the login that created them, no matter of any server roles that the login might be a member of.
So by default, the database owner is the one who created the database, but you have to make sure that no one changed this property:
To check the database owner, in SQL Server management studio, Right click on the database and in the Properties window >> General Tab >> check the owner property:

SQL Server 2008 - Put database user permissions back in after restore without sa privileges

What would be the best way to allow users to manage their own database restores from backup files? The reason I ask is because I have a user who is the db_owner for
a db. He loses access as soon as he restores the database from a backup file from another instance(of course because he does not have access on that instance), and
then someone with sa permissions has to restore his permissions. Is there anyway he can restore the backups, and then put back the db owner permission on the database that he already had?
Your best bet at this point is to create a custom stored procedure that does the following style pseudo code:
Take the database name, backup file name
Check to make sure the person requesting is the owner
Restore the database over the original
Set the owner back to the original in #2
Build in some logic to check and make sure people aren't trying to abuse it, give bad values, etc. Sign the procedure with a certificate and use a special account just for this purpose to lock down possible intruders/malicious people.

SQL Server 2008 - Delete data exclusively from stored procedure

I would like to stop power users from deleting data using SQL Server Management Studio. I need to archive data and add some info to the audit trail when data gets deleted.
Is there a way to stop them when they attempt to delete the data from SSMS?
Is there a way to know which process caused the deletion? such as from SSMS, application, stored proc?
Is there a way to allow only deletes from Stored Procedure?
Thanks
Create a new login and database user for this login. Then grant delete permission to this user, and revoke it from all others. Write procedure[s] that removes data, add WITH EXECUTE AS [previously created user that can delete data]. Grant other users with execute permissions to the procedure[s].
Well they are power users aren't they. You could set deny Delete permissions for them.
DENY DELETE TO [Your_User]
GO
From your second sentence I get the impression that deleting is not the real issue but that you need to archive data and create an audit trail when data is deleted. Why not use a delete trigger?

SQL Auth user that can restore some (but not all) databases

I'd like to setup an SQL Auth user in MS SQL 2005 that can restore some, but not all, databases in a particular instance.
I'm not sure if I should use Server Roles for this, since they would seem to apply to all databases, but Database Role membership doesn't seem right either (I don't want the SQL user to potentially 'lose' their restore ability if they restored a backup that didn't contain their database role membership).
How can I accomplish this?
You can't set up a user as such. This permissions sits above database/users at the server/login level
The login could have "dbcreator" which says:
...and can alter and restore their own
databases.
Even using GRANT would be tricky if not impossible, say, to "GRANT CREATE ANY DATABASE"
Restore is, in a way, a drop and create. Or simply a create.
I'd suggest the best solution (but probably not what you want to hear...) would be to create your own stored proc in master that checks rights and issue the RESTORE command if the login is set up as a user in that DB
Example: sp_checkandrestore 'dbname', 'backupfile'

Resources