views:

79

answers:

3

How would I do this in a stored procedure (SQL 2005):

count = select count(*) from table1 where line like '%success%'
if count > 0:
   delete from table1 where not line like '%success%'

Thanks for any help. My google skills are really failing me today :-(

+5  A: 

I would write that this way

if exist (select 1 from table1 where line like '%success%')
begin
    delete from table1 where line not like '%success%'
end
SQLMenace
+5  A: 

So if there are any rows where line is like success then delete any rows where line is not like success?

IF EXISTS (SELECT * from table1 where line like '%success%')
   delete from table1 where line NOT like '%success%'
Alex K.
+1  A: 

I would not keep the check at all. What is the purpose of that check?

Just use the delete query along with the where clause.

danish
I assume the table is a log of some sort, so if the process ended without a "success" message you can find out what went wrong. On the other hand, when there is a "success" message he's not interested in the other messages, only that it was successful.That's my guess.
Tony