在sql server中所有条件都没有触发?

时间:2013-11-04 09:18:05

标签: sql sql-server-2005

ALTER PROCEDURE [dbo].[K_HM_InsertSetterGetterAllocationDet]
@type varchar(50),
@name varchar(50),
@settingdate datetime,
@quantity int,
@Batchno varchar(50),
@pulloutdate datetime,

@supervisor varchar(50),
@updatedby varchar(50)

AS
BEGIN

SET NOCOUNT ON;

   if exists(select * from K_HM_SetterGetterAllocationDet where Name=@name and      [type]=@type and Attrited='false')
   begin
       select '1' as status
   end
   else 
      if exists (select * from K_HM_GetterSetterDet where Capacity >=@quantity)
      begin
         select '2' as status
      end
      else 
         insert into K_HM_SetterGetterAllocationDet (Type, Name, Settingdate, Quantity, batchno,pulloutdate,Supervisor,Attrited,Updatedby,Updatedon) 
         values (@type, @name,  @settingdate, @quantity, @Batchno, @pulloutdate, @supervisor, 'false',   @updatedby, getdate())   

   select '3' as status
END

在上述程序中,当上述两种情况为假且数量小于容量时,第三种情况不执行。数量小于或大于容量,它总是只进入第二种情况。我的手术有什么问题,请帮助我......

2 个答案:

答案 0 :(得分:0)

我在第3个声明中添加了Begin...End

IF EXISTS ( SELECT    *
              FROM      K_HM_SetterGetterAllocationDet
              WHERE     Name = @name
                        AND [type] = @type
                        AND Attrited = 'false' ) 
    BEGIN
        SELECT  '1' AS status
    END
  ELSE 
    IF EXISTS ( SELECT  * FROM    K_HM_GetterSetterDet WHERE   Capacity >= @quantity ) 
        BEGIN
            SELECT  '2' AS status
        END
    ELSE 
        BEGIN
            INSERT  INTO K_HM_SetterGetterAllocationDet
                    ( Type ,
                      Name ,
                      Settingdate ,
                      Quantity ,
                      batchno ,
                      pulloutdate ,
                      Supervisor ,
                      Attrited ,
                      Updatedby ,
                      Updatedon
                    )
            VALUES  ( @type ,
                      @name ,
                      @settingdate ,
                      @quantity ,
                      @Batchno ,
                      @pulloutdate ,
                      @supervisor ,
                      'false' ,
                      @updatedby ,
                      GETDATE()
                    )                         
            SELECT  '3' AS STATUS
        END 

编辑:根据评论...

接受每个查询..

SELECT  *
FROM    K_HM_SetterGetterAllocationDet
WHERE   Name = 'hardcoded name'
        AND [type] = 'hardcoded type'
        AND Attrited = 'false' 

如果有行,则status =1,否则运行以下内容...

SELECT  *
FROM    K_HM_GetterSetterDet
WHERE   Capacity >= hardcoded quantity

如果有行,则为status = 2,否则为status = 3

答案 1 :(得分:0)

ALTER PROCEDURE [dbo].[K_HM_InsertSetterGetterAllocationDet]
@type varchar(50),
@name varchar(50),
@settingdate datetime,
@quantity int,
@Batchno int,
@pulloutdate datetime,

@supervisor varchar(50),
@updatedby varchar(50)

AS
BEGIN

SET NOCOUNT ON;

  if exists(select * from K_HM_SetterGetterAllocationDet where Name=@name and [type]=@type and Attrited='false')
 begin 
 select '1' as status
 end





else  
begin 
if exists (select * from K_HM_GetterSetterDet where Capacity >@quantity  and Name=@name   ) 
begin
select '2' as status
insert into K_HM_SetterGetterAllocationDet       (Type,Name,Settingdate,Quantity,batchno,pulloutdate,Supervisor,Attrited,Updatedby,Updatedon)  values
 ( @type, @name, @settingdate, @quantity, @Batchno, @pulloutdate, @supervisor, 'false', @updatedby,  getdate())
end

else 
begin

select '3' as status
end
end
END

使用此过程正常工作....