在存储过程中如何运行查询并有条件地执行某些操作

时间:2010-05-11 14:00:49

标签: sql sql-server sql-server-2005 tsql stored-procedures

如何在存储过程中执行此操作(SQL 2005):

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

感谢您的帮助。今天我的谷歌技能真的让我失望: - (

3 个答案:

答案 0 :(得分:5)

我会这样写的

if exist (select 1 from table1 where line like '%success%')
begin
    delete from table1 where line not like '%success%'
end

答案 1 :(得分:5)

因此,如果有line成功的行,那么删除line不成功的行吗?

IF EXISTS (SELECT * from table1 where line like '%success%')
   delete from table1 where line NOT like '%success%'

答案 2 :(得分:1)

我根本不会保留支票。检查的目的是什么?

只需使用删除查询和where子句。