SQL Query to delete while database size is larger than 200GB

azure-sql-database

I have a SQL table which is used to store a large number of logs.

I want to have a stored procedure or something along the lines of that. That runs every minute and deletes items from the end of the log table as long as the database size is above 200GB.

so something like:

While(databaseSize > 200GB)
{
Batch delete 100 entires From [dbo].[logs] OrderBy DateTime
}

How would I go about doing this?

[Edit]
SQL Azure limits my database size to 250GB. Hence why I need to limit and still keep a small buffer for other tables.

Best Answer

In a maintenance plan run something like this like hourly

While((select count(*) from From [dbo].[logs]) > 1000000)
begin
    delete top (10000) From [dbo].[logs] OrderBy DateTime
end