TSQL equivalent of PostgreSQL "PERFORM" keyword?
I'm testing query performance in a loop. Rather than return a hundred duplica开发者_StackOverflowtes of a result set, I want to run a select statement hundreds of times, discarding the results each time.
PostgreSQL has the syntax "perform select...", which will execute the select statement and discard the results. "37.6.2. Executing a Query With No Result" http://www.postgresql.org/docs/8.2/static/plpgsql-statements.html
Is there an equivalent keyword in TSQL?
I'm aware that SSMS has a "Discard results" options, but it appears that the results are not discarded after each iteration and would still accumulate in memory until all iterations are complete. I suppose selecting into a temporary variable could work, but it would be much simpler to just discard the results with a keyword. I'd rather avoid construction of table variable definitions and the potential overhead of storing the results in temporary tables.
I searched through the SQL-92 specification for an equivalent keyword, but I was unable to find any and, in my experiences, don't know of one specific to SQL Server.
If you are using ADO.Net, execute the query without a result set by using command.ExecuteNonQuery();
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=MyDatabase;";
string sqlDelete = "DELETE FROM MyTable WHERE Id = 2";
SqlConnection connection = new SqlConnection(sqlConnectString);
SqlCommand command = new SqlCommand(sqlDelete, connection);
connection.Open( );
int rowsAffected = command.ExecuteNonQuery( );
Console.WriteLine("{0} row(s) affected.", rowsAffected);
Console.WriteLine("Record with Id = 2 deleted.");
connection.Close( );
}
}
精彩评论