SQL Server - Keep synchronized a read-only database - sql

I need to keep synchronized a couple of databases, one is on sql 2000 and the other on 2005. The one on 2000 should be kept in read-only mode to make sure the users does not enter data. The 2005 is the one which are going to be updated by the users.
So I could develop a script to truncate and insert into the 2000 version with data from 2005 every night. My question is if there is some way to disable the read-only mode temporaly while the script is running. Is there a better approach?
Thanks,

You can disable the read-only mode while the script is running. You may also want to set RESTRICTED_USER to keep any users from accessing the database while processing.

I would tend along the response offered by Mike Henderson.
Essentially leverage db security to prevent users from modifying data and allow the account used to synchronize data to write.
Not sure what your time constraints are, but if you are relying on the db to be read-only to prevent end-users making changes, there is the possibility that they could get in during the time period that the db is in read/write mode.

Just for future reference, this is how I proceeded,
ALTER DATABASE MyDB SET READ_WRITE WITH ROLLBACK IMMEDIATE
ALTER DATABASE MyDB SET READ_ONLY WITH ROLLBACK IMMEDIATE
Thank you,

Related

How to lock Sql Server 2012 and Prevent Establishing Connection and perform Data migration

In My University Management System i want to Perform Data Migration on Specific Day and so that i want close all open connections and Perform Data Migration as a Db admin user and after that Allow user to Establish connections to Database
after some Searching i find that script
To Disable MultiUser
alter database dbName set single_user with rollback immediate
To Enable MultiUser
alter database dbName set multi_user with rollback immediate
Question :
how to Perform my required Operation as a db admin?
it is a good way to do this or some better way ?
what does Disable Script doing?
what does Enable Script doing?
From this Disable Scriptall open connections will close ?
What you said is about the same what we do in this situation. The first statement puts the DB in "single_user" mode. That means, only YOUR current session is connected to the db after that statement. The "with rollback immidiate" option specifies that all open transaction and current queries and connections are closed and rolled back, so the database is in consistent state afterwards.
Personally, I dislike the "single_user" mode, because it allows any single user to connect if you accidentially close your connection. I'd use "restricted_user" instead. In that mode, only members of the db_owner fixed database role and dbcreator and sysadmin fixed server roles are allowed to connect to the database, but it does not limit their number. That will work better IF your users are not db_owner.
You second example just reverses that change and opens the DB to everyone ("multi_user") again.

SQL Server backup restore issues?

Ok, so I'm having a bit of an issue - I executed some code on my SQL Server and didn't realize that I didn't have the WHERE bit selected. Of course, when I saw the "608 rows affected" rather than "1 row affected", I freaked a bit.
Luckily, I have a backup saved, but for some reason, I'm getting a couple issues. Now, I took the server down, so I know that it's not being used by anyone, but it's giving me the following error
"Restore failed for Server 'myserver'.
System.Data.sqlclient.sqlerror: Exclusive access could not be obtained
because the database is in use. (Microsoft.SqlServer.Smo)"
I saw something that stated I should be using
Alter Database Databases
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE PRODUCT
FROM DISK = ''
but I'm having three reservations about this code. First, I'm completely unsure of how to turn multi_user back on. Second, I don't know where the program stores its backups. Third, this SQL is a bit above my head - I'm relatively new to the language, honestly, so I'm not sure how this will affect things.
Anyone have any answers to my troubles?
I might suggest instead of overwriting the existing database from the backup, that you instead recover the backup with a different database name... this way you can have the current and previous databases side-by-side.
Then, you can simply write an update statement to recover the data from just that one specific table instead of resetting the whole database.
EDIT: The specifics would depend on your environment, but the restore would look something like this:
restore database PRODUCT_OLD
from disk='C:\PRODUCT.bak'
with
move 'PRODUCT_Data' to 'C:\PRODUCT_OLD_Data.MDF',
move 'PRODUCT_Log' to 'C:\PRODUCT_OLD_Log.LDF'
And then the update statement would also be based on your specific table...
right on the database click tasks->takeoffline , when its succeed do the same thing but put it Bring Online
then try to restore your database
Set the database to single user is correct. When you are complete with your restoration you'll execute this
alter database YourDb
set multi_user
As for where your .bak file resides, you'll have to find it prior to restoring.
Scary.
Ok, some things to check:
Make sure you are the only person connected to the server.
Make sure no other applications, web servers, app servers, etc. hold connections to your DB.
Make sure your SQL manager has no open windows to tables or other objects in your database.
THEN you should be able to do the restore w/o single user stuff.
Go to the activity Monitor and see if users are still logged in then kill the process for that user using the respective db.
Then go ahead restore the backup

Simulate SQL Server database going down

I've written an application that connects to a system over the network and logs events from that system to a SQL Server database.
I need to test the behaviour of the application when the SQL Server goes down. Is there a way to Kill just the one Database on a SQL Server system without affecting the others?
If not is there a way to simulate the SQL Server going down.
It shouldn't matter but the app is written in Java.
You can use sqlcmd to set the database in single-user mode or detach the database using T-SQL. This will simulate the database going offline in a controlled fashion, but not simulate the server going down in an uncontrolled fashion, which perhaps could be more useful.
Extending #bzlm's answer:
USE master
GO
ALTER DATABASE YourDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
you can make the database offline
ALTER DATABASE YourDatabase SET OFFLINE
GO
In addition to the other answers: You might even want to test different failure modes.
The other answers simulate the DB going down, while the server the DB runs on stays up.
You might for example also want to simulate a network failure or a server crash; this could probably be done by altering the network settings on the app server, or just by pulling its (network) plug.
Of course, whether this makes sense depends on your app.
I simply stop SQL Server service.

Problem with user logins after db Restore

I have two SQL 2005 instances that reside on different networks. I need to backup a database from instance A and restore it to a database in instance B on a weekly basis so that both databases hold the same data. After the restore, logins SIDS on database B are changed and therefore users can't log into database B and connection strings for the web application it supports are broken. Is there a work around for this? Thanks.
There is a stored procedure for that: sp_change_users_login (see: http://msdn.microsoft.com/en-gb/library/ms174378.aspx). In particular, take a look at the Auto_Fix action. However, be aware that this SP is going to be deprecated in the future.
Insead, we're now supposed to use ALTER USER (see: http://msdn.microsoft.com/en-gb/library/ms176060.aspx).

SQL server database start/stop issue

I am using SQL Server 2008 Enterprise for development. I find from SQL Server logs, there are items like,
2009-09-20 19:54:33.55 spid53 Starting up database 'DummyOrderDB'.
My confusion is, I think we could only start/stop database server instance (the contained database will be started/stopped when the containing database server instance start/stop), can we just start/stop a database without touch database server instance? I did not find such menu from SSMS.
thanks in advance,
George
That is an auto close database. Auto-close databases are 'closed' when not in use and each time an user uses one, they run a short recovery and the text above is displayed. SQL Express creates databases as auto close ON by default. To turn off the auto-close behavior, run:
ALTER DATABASE <dbname> SEt AUTO_CLOSE OFF;
Yes, we can. Of course Starting and Stopping databases only make sense when the server itself is started (that helps ;-) ), but each individual database has to be, say, initialized before it can be used in earnest. Also when you detach a database, it first shuts down. (which ensures data integrity and other clean-up are taken care of etc.)