I try to find some way to delete a lot of data from table using batch with progress of deleting.
To delete I use the following code:
select 'to set rowcount'
WHILE @@ROWCOUNT <> 0
BEGIN
DELETE TOP (1000) @table from @table
END
But it does not provide info about state of deleting. So I try to add this functionality.
static void Main(string[] args)
{
var cmd = _connection.CreateCommand();
cmd.CommandText =
@"declare @table table(id int)
declare @i int = 0
insert into @table values (1)
WHILE @i < 20
BEGIN
insert into @table
select * from @table
set @i = @i + 1
END
select 'to set rowcount'
WHILE @@ROWCOUNT <> 0
BEGIN
select 'new batch'
DELETE TOP (100000) @table from @table
END";
var reader = cmd.ExecuteReader();
do
{
reader.Read();
Console.WriteLine(reader[0]);
} while (reader.NextResult());
Console.WriteLine("done");
Console.ReadLine();
}
It works but the output data is returned in the end of execution. But I try to get new batch
before processing of each batch. I don't want to separate each batch to separated SqlCommand
. I feel it can affect performance.