create table t_01
(
a varchar(100) null,
b numeric null,
c datetime null
)create proc sp_01
(
@a varchar(100),
@b numeric,
@c datetime
)
as
begin
update t_01
set b = @b,
    c = @c
where a = @a
and not exists
(
select null from t_01
where a = @a    '*** 这里
and   b = @b
and   c = @c
)
end
想问的是sp_01我想做的效果是:
如果 a ,@a 都为null 时,不更新t_01
如果 
{a为null, @a不为null时,
或者 a不为null,@a 为null时,
或者 a <> @a 时,
     更新t_01.
}
如果 a = @a 时,不更新t_01那么我应该这样写?
where isnull(a,'') = isnull(@a,'')
and   isnull(b,-1) = isnull(@b,-1)
and   isnull(c,'1900-01-01') = isnull(@c,'1900-01-01')
但是这种情况,如果b字段的值真是-1,而@b为null,那么
and   isnull(b,-1) = isnull(@b,-1)
         这个便为真了,和我要的效果反了.各位大哥们一般是怎样处理的?
用case吗?用case的话应该怎样表达?

解决方案 »

  1.   

    update t_01
    set b = @b,
        c = @c
    where a = @a
    and not exists
    (
    select 1 from t_01
    where a = @a    '*** 这里
    and   b = @b
    and   c = @c
    )
      

  2.   

    if (a is null and @a is null) or a=@a 
     不更新t_01if (a is null and @a not is null) or (a not is null and @a is null) or (a<>@a)  
     更新t_01我就是简单写了一下思路,不知道我的理解是否正确。
      

  3.   

    或者你写在一句话里update ... where (a is null and @a not is null) or (a not is null and @a is null) or (a<>@a) and (not ((a is null and @a is null) or a=@a))
      

  4.   

    (b<>-1 and isnull(b,-1) = isnull(@b,-1))
      

  5.   

    zzebra(老 斑) 
    update ... where (a is null and @a not is null) or (a not is null and @a is null) or (a<>@a) and (not ((a is null and @a is null) or a=@a))
    你的这句也太牛了吧.:)如果我的表的字段是10个,那这种写法不把我玩死..:)
      

  6.   

    思路不清晰
    没有帮法帮你
    我给你提个建议:存储过程负责更新数据的语句可以先确定下来,比如你写的
    update t_01
    set b = @b,
        c = @c
    where a = @a
    and not exists
    (
    select 1 from t_01
    where a = @a    '*** 这里
    and   b = @b
    and   c = @c
    )
    其他的条件,可以作为存储过程的执行条件,如果执行条件不符合,则不执行该存储过程
    如你写的
    想问的是sp_01我想做的效果是:
    如果 a ,@a 都为null 时,不更新t_01
    -------------
    执行储存过程先判断这个条件成立不,一般都是判断是否不成立,如果不成立则返回一个表示
    存储过程不执行的值,
    如果成立,则继续执行
      

  7.   

    我想问的是:
    对 t_01 更新,当用户什么也没有改变t_01时,不更新t_01.我的实际情况,

    t01
    字段: a,b,c,last_upd_time,last_upd_user.有客户机和服务器,要求能够两边同步,而现在我的做法是根椐last_upd_time是否有改变
    来判断表t01的内容是否有被更新,如果有更新则t01表同步.那么这里就存在这样的问题:当用户打开表明细时,如果什么也没改变就按"保存",那么我就要判断表的内容是否与用户输入的相同.就是执行下面的语句:update t_01
    set b = @b,
        c = @c,
                 last_upd_time = getdate(),
                 last_upd_user = @u
    where a = @a
    and not exists
    (
    select 1 from t_01
    where a = @a  
    and   b = @b
    and   c = @c
    )在       where a = @a
    and   b = @b
    and   c = @c中,我应该怎样写才能使表t_01做到"什么也没有变,就不更新last_upd_time"
    我现在的做法是这样的,不过好像不好啊.
             where isnull(a,'') = isnull(@a,'')
    and   isnull(b,-1) = isnull(@b,-1)
    and   isnull(c,'1900-01-01') = isnull(@c,'1900-01-01')当为字符串时用'',当为数字时用-1,当为时间时用'1900-01-01'有更好的方法吗?
      

  8.   

    首先用户打开了表明细的时候,在没有经过任何修改的情况下,保存按纽应该是Disable的,如果他进行了修改后,才可变成Enable。其次,你完全可以使用触发器来实现这些功能,当用户对表明细进行修改的时候,触发Updata触发器,然后进行你想做的事情。“如果我的表的字段是10个,那这种写法不把我玩死..:)” -- 以后说话注意点,你自己的问题没表达清楚,不要乱讲话。记住:在这里给你回帖不是别人的义务。
      

  9.   

    sorry,
    对不起,zzebra(老 斑) 
    我说那话,完全没那意思,望你不介意.
    非常谢谢你的帮助,回答.
      

  10.   

    where exists
    (
    select null from t_01
    where (@a is null and a is null)
    or a = @a
    )
    end