March 26, 2013 at 8:18 am
I am receiving a daily download of a database and restoring it locally to do some custom reporting on. I have this working from Batch files running nightly.
The problem I have is that when I restore the database, it also overwrites the local users of the database which includes a user I added to access the tables from Visual Studio.
How can I restore a database, but leave the local users tables alone?
Thank you in advance.
March 26, 2013 at 8:21 am
You can't
SQL needs to restore the database as it was at the time of backing it up.
To do what you need to do add another script to your batch which creates the login and user and assigned the necessary permissions.
March 26, 2013 at 11:46 am
I am still basic newbie to sql(1 year) how can I create accounts and permissions on a db from dos batch file? If you have a basic example I can take it from there.
Thank you.
Steve
March 27, 2013 at 2:54 am
When restoring a database from one SS instance to another there is two things I generally do from a script:
sp_changedbowner 'USERNAME'
sp_revokedbaccess 'USERNAME'
sp_grantdbaccess 'USERNAME'
These are needed in order to match the user SIDs for the new instance.
March 27, 2013 at 3:05 am
shathaway 77080 (3/26/2013)
I am still basic newbie to sql(1 year) how can I create accounts and permissions on a db from dos batch file? If you have a basic example I can take it from there.Thank you.
Steve
The login will already exist, so you need to change the context to the database.
USE databasename
GO
Then create the user
CREATE USER username FOR LOGIN loginname
GO
Then create the permissions you need, eg, db_owner
EXEC sp_addrolemember 'db_owner', 'username'
GO
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply