Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server query dry run

Tags:

sql

sql-server

I run a lot of queries that perform INSERT's, insert SELECT's, UPDATE's and ALTER's on tables, and when developing these queries, the intermediate steps that are run to test that various parts of the query work, potentially change the table or the data within the table.

Is it possible to do a dry run of a query and have SQL Management Studio give you what the results would be, without actually modifying the data or the table structure?

At the moment I have to back up the database, and run the query. If it works, good, if it doesn't, I have to restore the database (which can take around a hour) and I'm trying to avoid wasting all this time having to restore databases.

like image 323
bizzehdee Avatar asked Nov 07 '13 13:11

bizzehdee


People also ask

What is dry run in SQL?

A dry run (or a practice run) is a testing process where potential failure effects are intentionally mitigated.

What is Dry Run query?

When you run a query in the bq command-line tool, you can use the --dry_run flag to estimate the number of bytes read by the query. You can also use the dryRun parameter when submitting a query job using the API or client libraries. Dry runs do not use query slots, and you are not charged for performing a dry run.

How fix slow running query in SQL Server?

SQL Server uses nested loop, hash, and merge joins. If a slow-performing query is using one join technique over another, you can try forcing a different join type. For example, if a query is using a hash join, you can force a nested loops join by using the LOOP join hint.

How do I rollback a transaction in SQL Server?

You just have to write the statement ROLLBACK TRANSACTION, followed by the name of the transaction that you want to rollback. Now, try to run the AddBook transaction to insert the record where the name is Book15 (make sure that no book with this name already exists in the Books table).


2 Answers

Use an SQL transaction to make your changes then back them out.

Before you execute your script:

BEGIN TRANSACTION; 

After you execute your script and have done your checking:

ROLLBACK TRANSACTION; 

Every change in your script will then be undone.

Note: Make sure you don't have a COMMIT in your script!

like image 129
Bohemian Avatar answered Oct 10 '22 21:10

Bohemian


Begin the transaction, perform the table operations, and rollback as shown below:

BEGIN TRAN  UPDATE  C SET column1 = 'XXX' FROM table1 C  SELECT * FROM table1 WHERE column1 = 'XXX'  ROLLBACK TRAN 

This will rollback all the operations performed since the last commit since the beginning of this transaction.

like image 43
chami007 Avatar answered Oct 10 '22 21:10

chami007