2016-06-27 86 views
1

我的身份證沒有在else條件中返回。它顯示'位置0沒有行'。我的身份證沒有在其他條件下返回如果條件爲

USE [ctsdev] 
    GO 

    SET ANSI_NULLS ON 
    GO 

    SET QUOTED_IDENTIFIER ON 
    GO 

    alter PROCEDURE [dbo].[usp_incen] 
    (     

    @ConsultantName varchar(50) ,      
    @ClientName varchar(50) ,   
    @StartDate varchar(50),  
    @PositionName varchar(20) ,  
    @Location varchar(20) ,   
    @Job_Status varchar (20),       
    @BenchMarketing varchar(1) ,      
    @Placement varchar(1),  
    @CompanyName varchar(20),   
    @Durations varchar(20),    
    @DurationofProject varchar(10), 
    @Last_Updated_Date nvarchar(50), 
    @Rec_Name varchar(50), 
    @id int output 
    )  
    AS     
    BEGIN  

    SET NOCOUNT ON 

    /* checking whether the row with same session name and some id and updated date with remaining every fields as NULL exists*/ 

    if (SELECT COUNT(*) as cnt from tbl_Empincentivenew1 WHERE 
    id <> '' AND 
    Rec_Name = @Rec_Name and 
    Last_Updated_Date <> '' and 
    ConsultantName IS NULL and 
    ClientName IS NULL and 
    DurationofProject IS NULL and 
    Durations IS NULL and 
    StartDate IS NULL and 
    Location IS NULL and 
    BenchMarketing IS NULL and 
    Placement IS NULL and 
    CompanyName IS NULL)=0 



     BEGIN 

     /*if not then id field,recruitername and updated date is inserted*/ 

     INSERT INTO [tbl_Empincentivenew1](ConsultantName,ClientName,PositionName,CompanyName,Location,DurationofProject,Durations,BenchMarketing,Placement,Job_Status,Last_Updated_Date,StartDate,Rec_Name)      
    OUTPUT INSERTED.id 
    values(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,@Last_Updated_Date,NULL,@Rec_Name) 

    SET @id=SCOPE_IDENTITY() 
    RETURN @id /*that id is returned to the front end*/ 
     ENd 
     /*if the id with rec name,updated date with remaining all fiels null exist return that particular id to front end*/ 
    ELSe if(SELECT COUNT(*) as cnt from tbl_Empincentivenew1 WHERE 
    id <> '' AND 
    Rec_Name = @Rec_Name and 
    Last_Updated_Date <> '' and 
    ConsultantName IS NULL and 
    ClientName IS NULL and 
    DurationofProject IS NULL and 
    Durations IS NULL and 
    StartDate IS NULL and 
    Location IS NULL and 
    BenchMarketing IS NULL and 
    Placement IS NULL and 
    CompanyName IS NULL)=1 


     BEGIN 
    SET @id=SCOPE_IDENTITY() /*return that existing id,instead for again inserting null values*/ 
    RETURN @id 
    END    

     END 

    GO 

這是我的第一塊是越來越插入id的值,第二次時,他不允許添加新的空記錄相同的用戶登錄,但ID爲null記錄,不返回代碼。

回答

1

SCOPE_IDENTITY()返回上次生成的ID。在您的ELSE分支中,您不會創建任何新記錄,因此SCOPE_IDENTITY()仍然爲NULL

你ELSE語句可以是這樣的:

ELSE 
    SELECT @id = id 
    FROM tbl_Empincentivenew1 
    WHERE 
     Rec_Name = @Rec_Name and 
     Last_Updated_Date <> '' and 
     ConsultantName IS NULL and 
     ClientName IS NULL and 
     DurationofProject IS NULL and 
     Durations IS NULL and 
     StartDate IS NULL and 
     Location IS NULL and 
     BenchMarketing IS NULL and 
     Placement IS NULL and 
     CompanyName IS NULL 

雖然我會先運行此SELECT然後如果@id仍然NULL - 運行INSERT並返回@id = SCOPE_IDENTITY()

+0

謝謝@YB thankz很多.....你建議我用正確的解決方案..我明白了 – hari