SQL Server restorig backup on Developement (Or Test) Server and user mapping - sql

I got 3 server.
development (my local development machine)
Test purpose (Beta site).
Production
I got Instances in both SQL server 2005 and 2008.
So, When I want updated data, i take the latest backup from our production server and restore it on the Test or development Server. I got a script that make the changes (restore the file and make some slight changes on the databases.)
My problem is, each time i do this, The SQL server Login (created on the server) I use lose it's Database user mapping. I got to delete it form the restored database and recreate it from the root Security Logins.
Is there a way to include that in my restore script?

I got to delete it form the
restored database and recreate it from
the root Security Logins.
No, you don't need to do this - at least not on SQL Server 2005 and 2008.
What you need to do (like in a script) is this:
USE (your database)
GO
ALTER USER YourUserNo1 WITH LOGIN = YourServerLoginNo1
That associates your user YourUserNo1 in your database (whatever) to the server login called YourServerLoginNo1.
Works pretty nice'n'easy, I'd say! You can even do this as a last step in the restore script (which I'm sure you have to restore those things over and over again)

When restoring our production database to the report server each month, I run:
EXEC sp_change_users_login 'Auto_Fix', 'usernamegoeshere'

Related

Repair Suspected TFS database

i am using this T-SQL to repair my TFS suspected database
EXEC sp_resetstatus [TFS_Projects];
ALTER DATABASE [TFS_Projects] SET EMERGENCY
DBCC checkdb([TFS_Projects])
ALTER DATABASE [TFS_Projects] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ([TFS_Projects], REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE [TFS_Projects] SET MULTI_USER
but when i use this T-SQL i will get error
Database 'TFS_Projects' cannot be opened due to inaccessible files or insufficient memory or disk space.
how can i repair my SQL database?
i am using SQL Server 2012
UPDATE 1:
this error will Occurred in line :
DBCC checkdb([TFS_Projects])
UPDATE 2:
i have 20GB free on hard drive that my mdf and ldf on it
UPDATE 3:
i can not chek Autogrow becuase when i right click on db the error will appear
mdf and ldf is not readonly
and i am loged in by windows administartor, and loged in sql server by sa
1) If possible add more hard drive space either by removing of unnecessary files from hard drive or add new hard drive with larger size.
2) Check if the database is set to Autogrow on.
3) Check if the account which is trying to access the database has enough permission to perform operation.
4) Make sure that .mdf and .ldf file are not marked as read only on operating system file system level.
Found here: http://blog.sqlauthority.com/2007/08/02/sql-server-fix-error-945-database-cannot-be-opened-due-to-inaccessible-files-or-insufficient-memory-or-disk-space-see-the-sql-server-error-log-for-details/
i update my answer becuase old answer is dangerous and will damage TFS database!
this answer is from microsoft : http://msdn.microsoft.com/en-us/library/jj620932.aspx
To back up your databases
Launch TFSBackup.exe.
The TFSBackup.exe tool is in the Tools folder where you installed Team Foundation Server. The default location is C:\Program Files\Microsoft Team Foundation Server 12.0\Tools.
In Source SQL Server Instance, enter the name of the SQL Server instance that hosts the TFS databases you want to back up and choose Connect.
In Select databases to backup, choose the databases to back up.
Need help? List of TFS 2010 databases on MSDN; List of TFS 2012 databases on MSDN.
In Backup Databases to, enter the name of a network share that is configured with read/write access for Everyone, or accept the default location in the file system of the SQL Server you connected to in step 2.
Note Note
If you want to overwrite backups stored in this network location, you can choose Overwrite existing database backups at this location.
Choose Backup Now.
The Backup tool reports progress on each database being backed up.
Choose Close.
Restore your data
To restore your TFS data
Launch TFSRestore.exe.
The TFSRestore.exe tool is in the Tools folder where you installed Team Foundation Server. The default location is C:\Program Files\Microsoft Team Foundation Server 12.0\Tools.
In Target SQL Server Instance, enter the SQL Server instance you will use as the data tier and choose Connect.
Choose Add Share and enter the UNC path to the network share that is configured with read/write access to Everyone where you stored the backups of your TFS data. For example, \servername\sharename.
If the backup files are located on the file system of the server that is running TFSRestore.exe, you can use the drop down box to select a local drive.
Note Note
The service account for the instance of SQL Server you identified at the start of this procedure must have read access to this share.
In the left hand navigation pane, choose the network share or local disk you identified in the previous step.
The TFS Restore Tool displays the database backups stored on the file share.
Select the check boxes for the databases you want to restore to the SQL Server you identified at the start of this procedure.
Important note Important
For SharePoint, you must only restore the WSS_Content database. Do not restore the WSS_AdminContent or WSS_Config databases. You want the new SharePoint Foundation versions of these databases, not the ones from the previous version of SharePoint or from a SharePoint installation running on any other server.
Choose Overwrite the existing database(s) and then choose Restore.
The Database Restore Tool restores your data and displays progress reports.
Choose Close.
Probably your database is corrupt either due to an unexpected server restart or filesystem corruption. You can use CheckDB to check for issues & remove the suspect status. However, the best bet is to restore it from a working backup.
Some reasons for when this happens are
Database is corupted
database files are being "opened" or held by some process (operating system, other program(s)...)
Not enough disk space for SQL Server
Insufficient memory (RAM) for SQL Server
Unexpected SQL Server shutdown caused by power failure
How to repair a suspect database

sql server user authentication not working

i created a software in vb.net 2005. i create a database in sql server 2005. i connect it with window authentication. everything works fine.
but problem started when i replace my database with old database copyed from different computer.
i attach screnshot of both database with attached users.
old database
and new database is
when i try to connect my software with old database with window authentication, it is not connected.
can anyone please tell me what the problem is and how to solve this?
When you restore a Microsoft SQL Server database on a different machine, the user could be no more the same on the previous machine. Even if you see the same username listed.
This user is called an 'orphaned user'.
Meaning that there is no login id or password associated with the user since the original SID identifying the user is no more valid on the new machine.
You could check if you have this situation with
EXEC sp_change_users_login 'Report'
and, you could fix with
EXEC sp_change_users_login 'Auto_Fix', 'Gauntam-PC\Gautam'

How to create sql script to dump sql server table data?

I am new to sql scripting. I have a problem where I sometimes run a standalone mode that doesn't allow a connection from my database to my actual program. I am trying to automate a way to take the data out of my sql server database and push it to my program somehow. Just as a backup.
Is this possible?
Source: fmsinc.com/free/NewTips/SQL/SQLServerExpr..
Download and install the SQL Server Management Studio Express interface from the following URL (if you do not already have this utility installed on your server):
http://www.microsoft.com/downloads/details.aspx?FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796&displaylang=en
Please note that an installation of SQL Server Express does not install this interface (at the time this article was created for the 2005 version). This is a separate download.
Open the SQL Server Management Studio Express interface.
Create a new backup device using the wizard (expand Server Objects => New Backup Device).
Right click on the new backup device and select the database that you wish to backup.
Select the backup type (most often this is “Full”).
Choose a name for the job. Insert a description if you would like.
Click the Add button to add a Device Destination (this is where you would like the backup .bak output file to be placed).
Click Ok. The backup device is now setup. Repeat steps 3-7 for each database that you wish to backup.
Create a batch file using the following syntax (this is used to execute the backup device).
A syntax example is below (remember to save this file with a “BAT” file extension):
===< Begin Code Batch Script >===
sqlcmd -S MYSERVER\OFFICESERVERS -E -Q "BACKUP DATABASE MASTER TO TEST"
===< End Code Batch Script >=====
..where “MYSERVER” is the name of the SQL Server machine.
..where “OFFICESERVERS” is the name of the SQL Server.
..where “Test” is the name of the backup job.
..where “MASTER” is the name of the database.
Finally, open the Microsoft Windows Scheduler and assign the batch file created above to a scheduled event. Set the event to run at the point you wish.
EDIT: As you asked if 2 peoples are working remotely - in that case one will be server or both will be clients. What you can do is to create backup file on Server & the get that backup file for client OR in other case define 2 different Jobs & set their starting points for each of them. A backup file will be created for each job that will be executed & get that from destination for each client.

SQL Server Express unable to attach or delete mdf file

My database file Whist.mdf was attached to a SQL Server Express through Management Studio, but it stopped working in that sense that NHibernate in my asp.net solution fired some connection exceptions. I just detached it from Management Studio because I couldn't open the database in there either.
But when I try to reattach it I get this error:
Attach database failed for Server 'MyPCName\SQLECPRESS'. (Microsoft.SqlServer.Smo) Unable to open the physical file "C:\Program
Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Whist.mdf".
Operating system error 32: "32(process cannot access the file because
it is being used by another process)". (Microsoft SQL Server, Error:
5120)
I have googled it but can't find a solution that works for me. E.g.:
http://www.byteblocks.com/post/2010/01/06/Unable-to-open-the-physical-file-Microsoft-SQL-Server.aspx
"Cannot open user default database. Login failed." after installing SQL Server Management Studio Express
There is something about User Instance but I cant figure out if it have anything to do with that:
http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx
I have tried to delete the mdf file in windows but I can't do that either:
The action can't be completed because the file is open in system
I have tried stopping the SQL Server process, closing explore and delete from cmd and rebooting...
Hope someone can help - I guess it is just a simple thing..
I think you are on the right track. The error messages you are getting indicate that some SQL Server process is keeping the database file open. And SQL Server is designed to only allow the database file to be open by a single SQL Server process at a time.
You are also probably right to suspect User Instance of SQL Server Express to be the guilty process here. User Instance is a special SQL Server process that doesn't run as a service and instead is started and owned by the application that tries to connect to it. In your case there are two User Instances that can be the problem, one owned by the ASP.NET application you mentioned and one owned by you and started by Visual Studio. If you are using IIS Express or you configured the IIS App Pool for the ASP.NET application to run as your account then we only have one User Instance to worry about.
My recommendation is that you don't use User Instances and don't try to connect directly to the database file. Go over your connection strings and remove User Instance=true from all of them. You want to remove AttachDBFilename=... property as well.
If the database file is added to any of your Visual Studio solutions remove it (just be careful not to delete the file itself). The last step is to remove any connections to the database file from your Data Connections node in the Server Explorer.
Then restart the machine to make sure any User Instances that were running in the background were shutdown. After the machine starts again you should be able to attach the database file back to your main SQL Server Express using SQL Server Management studio. Let's name the database Whist
To connect to your database use the following connection string:
Data Source=.\SQLExpress;Integrated Security=true;Initial Catalog=Whist
When you connect from the ASP.NET application, make sure that you have created an account in your SQL Server Express for the application.
Turned out to be an issue with Daemon tools for me as well associating the .mdf file as an image.
If you want to copy your .mdf and .ldf file from your database, you can set your database file to 'offline' first . Please follow this steps:
Open your SQL Server 2008(suppose you are using SQL Server 2008 ).
Select your Database that you want to copy then left click.
Go to Tasks -> Take Offline.
Right click.
Then you can copy this database file to any folders.

Change sql server 2008 r2 database name/schema and username

I have two accounts at Softsys Hosting with a domain on each account. I need to clone the database from one domain to the other, so I can run one as a staging site and the other as the live site.
I've taken a back-up of the live database and tried to restore it to the dev account, but it fails because of database/schema/user name conflicts (both databases will be on the same server apparently so it won't let the 'same' database be created twice, which makes sense).
Softsys said that I need to take a local back-up of the database, change the name, schema name and user name, then upload that to my dev environment. The trouble is, I just can't get it to work. When I restore the back-up to my local system it won't let me log-in via the same username/password as on the live server - I can only seem to log is as 'sa'.
Can someone please explain to a database dunce, how I can get this .bak file from the live database running on my local machine with a working user account, and also how to change the names of the schema/user/database so that I'll be able to upload it to the dev server without any conflicts with the live domain?
I'm using MSSQL 2008 R2 and SQL Server Management Studio 2008 R2 Express.
Thanks all...
The reason you cannot use the login from your Prod box on your Dev box is that the login does not exist on the Dev box.
When you move the database over, it sounds like you only have the SA login on the Dev box.
You should script out the login(s) that you use on the prod server and recreate them on your Dev box. Read up here http://support.microsoft.com/kb/246133
If you have the login on your dev box, or you just created it with the script above, you are going to have to connect the Login on the server to the User in the datbase.
You need to link the the new Login on the server to the User in the database. Use the ALTER USER statement to fix this. http://msdn.microsoft.com/en-us/library/ms176060.aspx
This used to be done with sp_change_users_login http://msdn.microsoft.com/en-us/library/ms174378.aspx