I have a problem regarding SQL batches in SQL Server.
Assume I execute a SqlCommand using the following code:
private void Example(SqlConnection connection, SqlTransaction transaction)
{
using (SqlCommand cmd = new SqlCommand("select * from T1; EXECUTE('update T2 set 1=2')", connection, transaction))
{
SqlDataReader reader = cmd.ExecuteReader();
..
..
}
}
Now, since the first statement within the batch succeded, I get NO EXCEPTION and a RESULT eventhough the EXECUTE(''')-statement clearly is in error.
If I reverse the statements, I get an exception since a resultset isn't generated before the first error.
Now, suppose that I would like to detect the error in both cases, how do I do it without generating "redundant" code as in:
private void Example(SqlConnection connection, SqlTransaction transaction)
{
using (SqlCommand cmd = new SqlCommand("select INTO #T1 from T1; EXECUTE('update T2 set 1=2'); select * from #T1", connection, transaction))
{
SqlDataReader reader = cmd.ExecuteReader();
..
..
}
}
No this question may seem strange but still... I would like to know the answer, please don't ask why :)