create table a1(id int primary key,name int)
insert into a1 select 1,1
insert into a1 select 2,1go
alter proc lintest
as
begin
insert into a1 select 1,1
if @@error<>0     --如何得到此error的值
return 1
end
go
exec lintest
go
drop table a1幫忙修改下語句  ,這個算是否算是SQL SERVER 錯誤,可以返回系統的ERROR數字代碼嗎?
剛才那帖子有問題。。不好意思。。

解决方案 »

  1.   

    @@error 好像返回的是错误号
    select * from sys.messages 
      

  2.   

    create table a1(id int primary key,name int) 
    insert into a1 select 1,1 
    insert into a1 select 2,1 go 
    create proc lintest 
    as 
    begin 
    declare @errcode int
    insert into a1 select 1,1 
    set @errcode = @@error
    if @errcode <>0    --如何得到此error的值 
    select @errcode as errcode
    end 
    go 
    exec lintest 
    go 
    drop table a1 
    drop procedure lintest--测试结果:
    /*
    errcode
    -----------
    2627
    */
      

  3.   

    create table a1(id int primary key,name int) 
    insert into a1 select 1,1 
    insert into a1 select 2,1 go 
    create proc lintest 
    as 
    begin 
        declare @errcode int
        insert into a1 select 1,1 
        set @errcode = @@error
        if @errcode <>0    --如何得到此error的值 
            select @errcode as errcode
    end 
    go 
    exec lintest 
    go 
    drop table a1 
    drop procedure lintest
    /*(1 行受影响)(1 行受影响)
    消息 2627,级别 14,状态 1,过程 lintest,第 5 行
    违反了 PRIMARY KEY 约束 'PK__a1__5165187F'。不能在对象 'dbo.a1' 中插入重复键。
    语句已终止。
    errcode
    -----------
    2627(1 行受影响)*/
      

  4.   

    樓主要的是錯誤號
    不是錯誤描述信息
    不知道樓上的
    errcode
    -----------
    2627
    是真的運行結果,還是手動打上去的
    索性到錯誤描述中提取第一個數字算了
      

  5.   

    create table a1(id int primary key,name int) 
    insert into a1 select 1,1 
    insert into a1 select 2,1 
    alter proc lintest 
    as 
    begin 
        insert into a1 select 1,1 
        if @@error <>0    --如何得到此error的值 
            select @@error 
    end exec lintest 消息 2627,级别 14,状态 1,过程 lintest,第 4 行
    违反了 PRIMARY KEY 约束 'PK__a1__7C8480AE'。不能在对象 'dbo.a1' 中插入重复键。
    语句已终止。(1 行受影响)
      

  6.   

    我的就是2005
    得到的結果是
    /*
    訊息 2627,層級 14,狀態 1,程序 lintest,行 5
    違反 PRIMARY KEY 條件約束 'PK__a1__76177A41'。無法在物件 'dbo.a1' 中插入重複的索引鍵。
    陳述式已經結束。(1 個資料列受到影響)
    */
    而不是
    /*(1 行受影响)(1 行受影响)
    消息 2627,级别 14,状态 1,过程 lintest,第 5 行
    违反了 PRIMARY KEY 约束 'PK__a1__5165187F'。不能在对象 'dbo.a1' 中插入重复键。
    语句已终止。
    errcode
    -----------
    2627(1 行受影响)*/我只是好奇
    errcode
    -----------
    2627
    真的會帶出來嗎
      

  7.   

    看到了
    樓主的是希望得到
    一個數值型@@ERROR