为什么Ident_Current返回null但是Scope_Identity呢?

时间:2014-09-24 07:59:48

标签: sql sql-server tsql

它返回null,为什么? IDENT_CURRENT部分,但与Scope_Identity配合得很好,为什么?

ALTER PROCEDURE [dbo]. [InsertComplaints] 
    @ComplaintCode varchar(50),
    @ComplaintType_ID smallint,
    @RecievingMode_ID smallint,
    @Subject varchar(100),
    @ComplainantID smallint,
    @District_ID smallint,
    @AddressedTo varchar(50),
    @DiaryNo varchar(50),
    @User_ID int,
    @Status_ID smallint,
    @RecievedDate smalldatetime,
    @IGRemarks varchar(MAX) = null,
    @PsoRemarks varchar(MAX) =null,
    @FinalDecision varchar(250)=null,
    @AgainstDist_ID smallint,
    @HomePS_ID smallint,
    @AgainstPS_ID smallint, 
    @Name varchar(75),
    @DesigID int,
    @ForwardedBy smallint,
    @SMS_ID int = 0,
    @result bit output,
    @ID int output
AS
BEGIN
   Begin Try
        insert into dbo.Complaints 
        values (
        @ComplaintCode,
        @ComplaintType_ID,
        @RecievingMode_ID ,
        @Subject,
        @ComplainantID ,
        @District_ID ,
        @AddressedTo ,
        @DiaryNo,
        @User_ID,
        @Status_ID,
        @RecievedDate,
        @IGRemarks,
        @PsoRemarks,
        @FinalDecision,
        @AgainstDist_ID,
        @HomePS_ID,
        @AgainstPS_ID,
        @Name ,
        @DesigID,
        @ForwardedBy,
        @SMS_ID
        ) 

        Set @result = @@ROWCOUNT 

        Set @ID = IDENT_CURRENT('ComplaintID') --SCOPE_IDENTITY() 
        Select @ID
   End Try
   Begin Catch
        Set @result=0
   End Catch
END 

我想从特定的表中获取最后一个插入的id,以便来自投诉表的complaintID,但它不返回任何空值。帮助!

1 个答案:

答案 0 :(得分:0)

根据msdn,如果主叫用户对此对象没有足够的权限,IDENT_CURRENT函数将返回null。
另一种可能是您的对象名称错误,我看到您正在使用dbo前缀进行插入。也许这会有所帮助。