I cannot understand if this is an acceptable operation. I need to select records from a SQL Server 2008 database and then delete them as a single transaction from ASP.NET code. Note that .NET code must be able to retrieve the data that was first selected.
Something like that:
SELECT * FROM [tbl] WHERE [id] > 6; DELETE FROM [tbl] WHERE [id] > 6
I tried it using SQL Fiddle, but if so:
SELECT * FROM [tbl]
I get the full table as if nothing had been deleted.
EDIT According to the complete .NET code below for retrieving records:
string strSQLStatement = "SELECT * FROM [tbl] WHERE [id] > 6;" + "DELETE FROM [tbl] WHERE [id] > 6"; using (SqlCommand cmd = new SqlCommand(strSQLStatement, connectionString)) { using (SqlDataReader rdr = cmd.ExecuteReader()) { while(rdr.Read()) { //Read values val0 = rdr.GetInt32(0); val3 = rdr.GetInt32(3); //etc. } } }
source share