Best way to make all tables read only - sql-server

I have a SQL Server 2016 database of which I am the owner. It's an archive that's no longer accessible to anyone else. I still need to read this database by linking to it from other SQL Server databases and from Access.
I would like to be able to alter and create views in this database but I would like to prevent myself from inadvertently changing any data there.
What's the quickest, easiest, and easily reversible way to allow myself only select access to all the tables?

alter database current set read_only
To reverse, or if you want to modify views, run
alter database current set read_write
No permissions-based solution is effective against the database owner.

Related

Recreate Database Schema-Only

I want to make a process for recreating a database schema-only. Not a backup/restore but standing up a schema-only copy of a database on a different server, that we would populate with data manually. I was thinking about using SMO in powershell. Does anyone know what the best approach for this would be? Prefer to stay away from 3rd party options, I have the time to do it myself.
Thanks!
One way is to use DBCC CLONEDATABASE(structures + statistics):
DBCC CLONEDATABASE('original_db_name', 'cloned_db')
ALTER DATABASE [cloned_db] SET READ_WRITE WITH NO_WAIT
Backup
Restore on second server
ALTER DATABASE [cloned_db] MODIFY NAME = original_db_name;

How to easily copy a SQL Server database without using restore or attach?

I would to copy a SQL Server 2012 database from one server to another with the least amount of manual work and without doing a restore or attach database because I don't have access to the source server or backup files.
I would like to have a copy of all the objects and data. This includes tables with primary (including identity designation) and foreign keys, views, stored procedures, constraints and triggers.
If I use SSMS, I have to use a combination of data imports and scripting the objects. One issue with this is that I have many tables and manually enabling identity inserts is a hassle. Maybe one way is to use a diff tool to do all this work for me if possible or find a way to script the identity properties across the tables.
Is there a simpler more straightforward way to copy a database?
Replication. There are different types. Transactional will keep your copy updated with any changes. Snapshot will not, etc.
Without access to the server, I am not sure what you can do at all?

Is there a command to stop using a database in SQL Server?

I'm refering to when we say USE dbTest we start using that database and we can create tables and what not, and if we want to change databases we could just say USE dbNotatest and it would change the database we're using.
But is there a way to stop using the database we selected in the first place, without starting to use another one?
To stop using a database, you will need to change your database context. For example, if you are trying to drop your database and you are in the context of that database, simply switch to another database (commonly master or tempdb).
If other connections are open to the database and preventing you from dropping the database, you will need to kill the connected spids. This can be tedious, so an option to force close all connections and then drop your database that usually works for me is:
use [master];
ALTER DATABASE [foo] SET OFFLINE WITH ROLLBACK IMMEDIATE;
ALTER DATABASE [foo] SET ONLINE;
DROP DATABASE [foo];
By taking the database offline with rollback immediate, I force all connections closed and rollback any open transactions. Now, I could drop it while it is offline, but if I do the database files will remain on the file system. Dropping a database online will remove the database files, so I bring it back online before I drop it.
There is no option in SQL Server.
you can use only use query to manipulate other databasees otherwise you don have any options.
you can detach it just by using another database . for example in Microsoft sql sever management you can use the test database that already exists

How to fetch data from a different sql server database without having access?

I have two databases A and B in same SQL Server instance.. I need to write a trigger -- After update of a table in database B it will fetch data from few tables of database A and then insert data in some table of database B .. The issue is the user who will be accessing the database B does not have access to database A .. If I write a trigger with 'sa' account, will it work when the user inserts some data in database B? Also let me know the scenario what would I have to do if database A is in a different SQL Server?
It can work, but you have to do a few things to get there. Here's the easiest way (though not necessarily the best):
Set the owners of both Databases to 'sa'.
Turn on CROSS-DATABASE chaining for both databases.
Turn on TRUSTWORTHY for the source database (B).
Edit the Trigger and add WITH EXECUTE AS OWNER before the FOR clause.
Note that while this works, it has significant security considerations (particularly #2 and #3). Here is a link that explains this and some other methods and some of the security issues: http://msdn.microsoft.com/en-us/library/ms188304.aspx

Database availability during database update

I have a database from a 3rd party. They supply a tool to update the database data weekly. The tool is pretty old and uses ODBC. Updates can either be incremental or can delete all database data then recreate the data. The update can take several hours. In order to have high availability, it was suggested to have 2 SQL databases, and store a "active database" setting in another database to determine which of the two databases applications should use (while the other could be being updated).
One issue we are running into is: How to do reference the active database in stored procedures in other databases?
Is this the right approach? Is there a simple, perhaps-infrastructure-based approach? (Should this be posted on ServerFault?)
Note: Databases are read-only besides the update tool.
If the databases are on different servers, you can create an alias for the server which will redirect to the other server in SQL Server Configuration Manager. Under SQLNative Client 10.0 Configuration (or 9.0 if you're in SQL Server 2005) you can add a new alias.
Otherwise, you can always rename the databases using sp_dbrename so thata your client applications are always using database1 while you are updating database2.
If you want to use different databases inside a stored procedure you either need to:
Duplicate all the calls. Ugly. You would end with a lot of:
if #firstDatabase=1
select * from database1..ExampleTable where ...
else
select * from database2..ExampleTable where ...
Use dynamic queries. Less ugly:
set #sqlQuery='select * from '+#currentDatabase+'..ExampleTable where...'
exec sp_executesql #sqlQuery
I admit that neither solution is perfect...
I'd take the approach of having the stored procedures in both databases with some sort of automatic trigger to update the stored procedures in the other database if a stored procedure is changed.

Resources