views:

37

answers:

1

I'm using ADO .NET and MS SQL Server 2008.

I have a connection object to a server and a command:

SqlConnection conn = /* my connection*/;
string cmd = "some_sql_command";

I want to check if SQL Server can execute cmd. I don't want to execute cmd, but I want to know If SQL Server can execute it.

cmd can be any single SQL statement, it's not a procedure, transaction or multiple commands etc..

+3  A: 

Well, you can always start a transaction, execute the command and then roll back the transaction so that no changes are made to the database.

Another option is to use ServerConnection.ExecuteNonQuery method, which, when passed ParseOnly flag, only parses the command and does not do anything else.

Anton Gogolev
Just as a note, Query Analyzer / Management Studio don't truly obey SET PARSEONLY ON - they still execute the statement. You also need to use SET NOEXEC ON and/or SET FMTONLY ON, however in doing so you lose the binding capability of the parser, so it won't warn you about invalid object or column names.
Aaron Bertrand