alter proc SP_SelectStockDL      
@F_Intype       int,
@F_Stocktype    int
as 
begin
  select * from 表A
end
GO例如有上面这存贮过程,我想当参数@F_Intype不为空时加入条件语句F_intype=@F_intype,当参数@F_Intype为空时就不加入此条件语句,当@F_Stocktype不为空时加入F_Stocktype=@F_Stocktype,为空时不加入

解决方案 »

  1.   

    用if 


    F_Intype=isnull(@F_Intype,F_Intype)
      

  2.   

    alter proc SP_SelectStockDL      
    @F_Intype       int,
    @F_Stocktype    int
    as 
    begin
    --只有一个判断一下就可以了:
    if @F_Stocktype is not null
     select * from 表A where F_intype=@F_intype
    else
    select * from 表A
    end
    GO
      

  3.   

    ALTER PROCEDURE SP_SelectStockDL      
    @F_Intype       INT,
    @F_Stocktype    INT
    AS 
    DECLARE @SQL NVARCHAR(300)
    SET @SQL='SELECT * FROM 表A'
    IF(@F_Intype IS NOT NULL)--不知道你说的空是Null还是0如果是0的话,那么@F_Intype <>0    BEGIN
            SET @SQL=@SQL+' WHERE F_Stocktype='+@F_Stocktype
        ENDEXECUTE (@SQL)
      

  4.   

    alter proc SP_SelectStockDL       
    @F_Intype       int, 
    @F_Stocktype    int 
    as  
    begin 
      select * from 表A where F_Intype=isnull(@F_Intype ,F_Intype) and F_Stocktype=isnull(@F_Stocktype,F_Stocktype)
    end 
    GO 
      

  5.   


    alter proc SP_SelectStockDL       
    @F_Intype       int, 
    @F_Stocktype    int 
    as  
    begin 
    declare @where varchar(100)
    set @where = ''
    set @where = @where+isnull(' and f_intype='+ltrim(@F_Intype),'')
    set @where = @where+isnull(' and f_intype='+ltrim(@F_Stocktype),'')
    exec('select * from 表A where 1=1 '+@where)
    end 
    GO 
      

  6.   

    我喜欢这个alter proc SP_SelectStockDL @F_Intype int, @F_Stocktype int as begin select * from 表A where F_Intype=isnull(@F_Intype ,F_Intype) and F_Stocktype=isnull(@F_Stocktype,F_Stocktype) end GO 
      

  7.   

    alert proc SP_SelectStockdl
    @F_IntType int,
    @F_StockType int
    AS
      BEGIN
      DECLARE @sql VARCHAR(100)
      SET @sql='SELECT * FROM 表A WHERE 2<>0 AND'
      SET @sql=@sql +ISNULL(' f_inttype='+LTRIM(@F_Inttype),'')
      SET @sql=@sql +ISNULL(' AND f_Stocktype='+LTRIM(@F_Stocktype),'')
      EXEC(@sql)
    END
    GO
     
      

  8.   

    --设置一个默认值
    alert proc SP_SelectStockdl
    @F_IntType int=0,
    @F_StockType int=0
    AS
      BEGIN
      DECLARE @sql VARCHAR(100)
      SET @sql='SELECT * FROM 表A WHERE 2<>0 '
      SET @sql=@sql +ISNULL(' AND f_inttype='+LTRIM(@F_Inttype),'')
      SET @sql=@sql +ISNULL(' AND f_Stocktype='+LTRIM(@F_Stocktype),'')
      EXEC(@sql)
    END
    GO