如果条件为

时间:2016-06-27 09:03:56

标签: sql tsql

我的id没有在else if条件中返回。它显示“位置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没有返回。

1 个答案:

答案 0 :(得分: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()