如果要从网页中,将一个数据写入数据库符合条件的一行中(没有非空要求),而符合这个条件的行不存在,这时,会报异常吗?

解决方案 »

  1.   


    要update到符合条件的行中, 但是数据库中又不存在这样的一行, 不会报错,rowcount = 0 而已。
      

  2.   


    那就简单了, 执行更新之后判断一下@@rowcount是不是等0 , 等于0的话就说明没更新到资料, 报错出来就可以啦
      

  3.   

    最好是主动判断
    if exists ...先update 再 @@rowcount 从编程思想来说,跟用try catch 一样, 算是后设防, 把本来可预知的错误当作不可预知的异常来处理, 不建议这么做。
      

  4.   

    --检查@@ERROR,如果是0则执行成功,否则执行出错
    --如:
    -- Execute the INSERT statement.
    INSERT INTO authors
    (au_id,  au_lname, au_fname, phone, address, 
     city, state, zip, contract) values
    (@au_id,@au_lname,@au_fname,@phone,@address,
     @city,@state,@zip,@contract)-- Test the error value.
    IF @@ERROR <> 0 
    BEGIN
       -- Return 99 to the calling program to indicate failure.
       PRINT "An error occurred loading the new author information"
       RETURN(99)
    END