How to use sql server database in preview mode? - sql

As we know in Oracle no data or action query is committed until we call a commit. I want such implementation with SQL Server that I can run action queries on db but data is not changed permanently, it just let me see data before and after my action queries.
Or is there any way to mirror database on same server in such a way that I can test my queries on secondary database but it didn't have any impact on primary database.

You can use the IMPLICIT_TRANSACTIONS setting to achieve a similar function to what Oracle does:
Transactions that are automatically opened as the result of this setting being ON must be explicitly committed or rolled back by the user at the end of the transaction. Otherwise, the transaction and all of the data changes it contains are rolled back when the user disconnects.
You can, if you so choose, change a setting in Management Studio so that this setting is always in force when you open new query windows:

You can do it within same query window, in which you have written 'begin tran'
do not commit until not conform.
you can execute number of query within same window, which was give you your query preview before committing transaction.
It's only possible within same query window. you can not able view preview in other query window.

You can change your Isolation level from Read Committed to Snapshot to work around with your test queries.(If you dont want to see change in data during your transaction)
Please refer the below link for Snapshot Isolation level:
http://gavindraper.com/2012/02/18/sql-server-isolation-levels-by-example/

Related

Making queries in HSQLDB during transaction

I'm running a test using HSQLDB and I want to be able making select queries on database while this test to see what is changing. I suppose transaction or something else is blocking the database while test and I'm not able to make queries ...
Is it possible to configure hsql server in that way to be able to do these queries?
This answer is valid if you are starting a HyperSQL Server and connecting with the jdbc:hsqldb:hsql:xxxx connection URL.
The default transaction isolation model is LOCKS. In this model, the tables are locked when their rows are modified during a transaction. You need to use the MVCC model for the database to allow other connections to access the table.
However, when you modify the rows, these changes are only visible to the transaction that makes the change until it commits the data and the changes become visible to other connections.

SQL restore data from transaction log

I have a transaction which update data today, but the code used in the transaction have bugs and some of the data updated wrongly.
May i ask if it's possible to restore data by using the transaction log stored in the database?
Thanks a lot
using a full back up and a collection of transaction logs you can restore a database to a point in time. If you are looking to not take the database offline to restore, you would need to restore the database to a new database and identify the data that you want to fix and manually "fix" it. See https://technet.microsoft.com/en-us/library/ms190982(v=sql.105).aspx
Its very complicated to restore from Tlog,unless you know what you are looking for..use some kind of tool like Apex SQL log (not a freeware) to generate statements from Tlog
You can read more details here
http://www.apexsql.com/sql_tools_log.aspx

Database Transaction Notifying of Changes When no Changes Made when using Database Link

I use sqldeveloper (but this problem has also been duplicated on TOAD, so I don't think it's specifically related to sqldeveloper). The basic problem is after I run a complex SELECT query that uses a database link through sqldeleloper, it will notify that I have changes that need to be rolled back or committed when I go to close my session. The weird part is that I was only running a SELECT query and there are no changes to COMMIT. Can someone explain this behavior to me?
An implicit transaction occurs when selecting over a dblink.
When Oracle performs a distributed SQL statement Oracle reserves an
entry in the rollback segment area for the two-phase commit
processing. This entry is held until the SQL statement is committed
even if the SQL statement is a query
More on this here. I'll try to dig up an Oracle link if I can.
More from the horses mouth:)
Two-Phase Commit Mechanism
A database must guarantee that all statements in a transaction,
distributed or non-distributed, either commit or roll back as a unit.
The effects of an ongoing transaction should be invisible to all other
transactions at all nodes; this transparency should be true for
transactions that include any type of operation, including queries,
updates, or remote procedure calls.
More from Oracle's Distributed Database Concepts guide

Lock request time out period exceeded - Telerik OpenAccess ORM

I have a big SQL Server 2008 R2 database with many rows that are updated constantly. Updating is done by a back end service application that calls stored procedures. Within one of those stored procedures there is a SQL cursor that recalculates and updates data. This all runs fine.
But, our frontend web application needs to search through these rows and this search sometimes results in a
Lock request time out period exceeded. at
Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery()..
After doing some research I have found that the best way to make this query to run without problems is to make it run with "read uncommitted isolation level". I've found that this setting can be made in the Telerik OpenAccess settings, but that's a setting that affects the complete database ORM project. That's not what I want! I want this level for this query only.
Is there a way to make this specific LINQ query to run in this uncommitted isolation level?
Or can we make this one query to use a WITH NOLOCK hint?
Use
SET LOCK_TIMEOUT -1
in the beginning of your query.
See the reference manual
Runnung the queries in read uncommitted isolation level (and using NOLOCK hint) can cause many strange problems, you have to clearly understand why do you do this and how it can interfere with your dataflow

How to rollback changes in SQL Server

By mistake I have updated data on production database. Is there any way to rollback those transactions.
I have executed the update statement from management studio and the script does not have in
Begin Trans/rollback/commit.
Thanks
Here is what I would do in this case:
Restore backup in separate database and compare these databases to recover rows that exist in backup?
If your database is in full recovery mode try reading transaction log to recover the remaining rows.
In order to read transaction log you can use a third party tool such as ApexSQL Log or try to do this yourself through fn_dblog function (here is an example but it’s quite complex).
Here are other posts on this topic:
Read the log file (*.LDF) in SQL Server 2008
How can I rollback an UPDATE query in SQL server 2005?
Without transaction (or indeed even with a committed transaction), there is no easy way to revert the changes made.
Transaction are mostly useful to ensure that a series of changes to the database are performed as a single unit, i.e. so that either all of these changes get performed [in the order prescribed] or that none of them get performed at all (or more precisely that the database server rolls-back whatever changes readily done would there be a problem before all changes are completed normaly).
Depending on the recovery model associated with your database, the SQL log file may be of help in one of two ways:
If you have a backup and if the log file was started right after this backup, the logfile may help "roll forward" the database to the point that preceded the unfortunate changes mentioned in the question. (aka point-in-time restore)
If no such backup is avaiable, the log file may be suitable to reverse the unfortunate changes
Both of these approaches imply that the SQL log was indeed maintained as some of the recovery models, are such that the log file get truncated (its data lost) after each successful batch/transaction. And neither of these approaches is easy, the latter in particular probably require third party software (or a lenghty procedure) etc.
Depending on how your backups are set up, you may be able to do a point in time restore. Talk to your DBA. You may also want to take the DB offline ASAP to prevent more changes that would eventually be lost when you do the restore.