I\'m trying to make my integration tests more idempotent. One idea was to execute rollback after every test, the other idea was to some how programatically parse the text,
I think the command you are looking for is SET NOEXEC ON. If you set this for your connection, the queries will be parsed but will not be executed. Another option would be SET PARSEONLY ON
, but I'm honestly not sure what the difference between the two really is.
Use the following query
SET PARSEONLY ON
--Your query here
SET PARSEONLY OFF
Really it depends upon the purpose of the tests.
The most reliable way would be to use the rollback after every test if your statements lend themselves to that (aren't too heavy weight to make it viable).
I have done this in the past and have been glad to be notified of runtime issues that I wouldn't have caught any other way.
SQL Server 2012 can parse your syntax, procedures and tables with the following system procedures and functions:
They are supposedly replacing "SET FMTONLY".
I have tested them and they work a lot better than "SET NOEXEC ON" and "SET PARSEONLY ON"
Examples:
Will not throw an error:
sp_describe_undeclared_parameters
@tsql = N'SELECT object_id, name, type_desc FROM sys.indexes;'
Will correctly throw an error ("SET NOEXEC" and "SET PARSEONLY" do not throw an error in this case):
sp_describe_undeclared_parameters
@tsql = N'SELECT object_id, name, type_desc FROM sys.indexes;SELECT object_id, name, type_desc FROM sys.NOTaTABLE;'
VSTSDBPro has a query parser you can access programmatically: http://blogs.msdn.com/b/gertd/archive/2008/08/21/getting-to-the-crown-jewels.aspx
SET PARSEONLY : Examines the syntax of each Transact-SQL statement and returns any error messages without compiling or executing the statement.