@@error是系统函数,当没有发生错误时返回0,如果发生错误时@@error<>0,并返回错误号,每个SQL语句执行完,@@error值都会变。

@@error只记录当前错误,如果存储过程执行多个操作,就要每次都判读@@error是否出现错误,比较麻烦。
今天就遇到这个问题了,下面是一个简单的例子,就可以解决这个问题。

create  proc proc_Test
--根据ID修改表
@id int ,          --id
@name varchar(20)   --名字
as
begin transaction
declare @errors int        --错误数

--根据id修改
 update table1  set name=@name where id=@id
--设置接受错误数
set @errors=@errors+@@error

r

--查询销售id
select  @xsbfid =id from receipt2 where sn=@sn
set @errors=@errors+@@error

--插入样机表
insert into table2
(name )values(@name)
set @errors=@errors+@@error

if (@errors<>0)
begin
    rollback  transaction 
end
else
begin
    commit transaction 
end

只是声明了一个变量,每次用它来累积错误值。嘻嘻,问题就解决了。

http://blog.163.com/happy_2010_zyj/blog/static/151148756201041044646221/

SQL Server @@ERROR 用法的更多相关文章

  1. MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems

          早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...

  2. Invalid object name ‘sys.configurations’. (Microsoft SQL Server, Error: 208)

    http://blogs.msdn.com/b/ramaprasanna/archive/2009/09/16/invalid-object-name-sys-configurations-micro ...

  3. SQL Server error

    原因:文件没有权限 出错: TITLE: Microsoft SQL Server Management Studio------------------------------ Attach dat ...

  4. [SQL SERVER] The CHECK_POLICY and CHECK_EXPIRATION options cannot be turned OFF when MUST_CHANGE is ON. (Microsoft SQL Server, Error: 15128)

    The CHECK_POLICY and CHECK_EXPIRATION options cannot be turned OFF when MUST_CHANGE is ON. (Microsof ...

  5. SQL Server在本地计算机上用SSMS(SQL Server Management Studio)登录不上,错误消息:(Microsoft SQL Server, Error: 18456)

    今天遇到了一个奇怪的问题,公司目前在SQL Server上都采用AD域账号登录,由于账号人数众多,所以我们建立了一个AD Group(域组),将大家的AD账号加入了这个AD Group,然后我们将这个 ...

  6. SQL server Error Number

    描述 HY000 所有绑定列都是只读的. 必须是可升级的列,以使用 SQLSetPos 或 SQLBulkOperations 更改或插入行. HY000 已检测到一个旧 netlib (%s).请删 ...

  7. SQL SERVER各种用法总结

    sql创建临时表http://www.cnblogs.com/jeffwongishandsome/archive/2009/08/05/1526466.html sqlserver更改表名 EXEC ...

  8. SQL Server error "Xml data type is not supported in distributed queries" and workaround for it

    Recently while working with data migration,got an error while running a following query where Server ...

  9. SQL Server特殊用法笔记

    1. MERGE用法:关联两表,有则改,无则加 SQL语句: create table #AAA(id int,A int,AA int,AAA int,B int) create table #BB ...

随机推荐

  1. (中等) CF 585D Lizard Era: Beginning,中途相遇。

    In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...

  2. sublime text 主题推荐

    Soda Spacegray Flatland Tomorrow Base 16 Solarized Predawn itg.flat 其他所有的配色方案和主题.

  3. [转]hibernate三种状态详解

    本文来自 http://blog.sina.com.cn/u/2924525911 hibernate 三种状态详解 (2013-04-15 21:24:23) 转载▼   分类: hibernate ...

  4. Struts2---声明式异常处理

    在service方法里 throw抛出一个异常, 然后再方法声明上加上throws: public List<Category> list() throws SQLException{ C ...

  5. 单向链表(C#)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. utf8_unicode_ci与utf8_general_ci

    下面摘录一下Mysql 5.1中文手册中关于utf8_unicode_ci与utf8_general_ci的说明: 当前,utf8_unicode_ci校对规则仅部分支持Unicode校对规则算法.一 ...

  7. 决策树ID3算法的java实现

    决策树的分类过程和人的决策过程比较相似,就是先挑“权重”最大的那个考虑,然后再往下细分.比如你去看医生,症状是流鼻涕,咳嗽等,那么医生就会根据你的流鼻涕这个权重最大的症状先认为你是感冒,接着再根据你咳 ...

  8. HDU 5620 KK's Steel

    想了一下发现是斐波那契数列.....水题 #include <stdio.h> #include <algorithm> #include <string.h> # ...

  9. UVA 11149 Power of Matrix

    矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...

  10. Http协议与TCP协议理解

    TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通 ...