如果用if语句选择性使用insert或者delete等,和直接使用excute来执行哪个的效率更好,谢谢!

解决方案 »

  1.   


    EXEC SP_EXECUTESQL N'STRSQL'
      

  2.   

    楼主可是这意思?declare @tb table(id int primary key, value varchar(30))
    declare @id int=10,@value varchar(30)='aaaa'
     
    --1 直接
    Insert into @tb(id,value)
    Select @id,@value
    where not exists(select * from @tb b where b.id=@id )
    Select * from @tb Delete from @tb where id=@id
    --2 选择性
    if not exists (select * from @tb b where b.id=@id )
    Begin
    Insert into @tb(id,value)
    Select @id,@value
    end
    if exists (select * from @tb b where b.id=@id )
    Begin
    Delete from @tb where id=@id
    end
      

  3.   

    我之前用过一个软件,可以写出SQL语句,然后执行,系统会分析出来相同结果的SQL,并且按照执行效率排序,我一下记不得了,你可以搜搜看看,等想起发上来~!