ALTER procedure usp_selectDemo @DemoId varchar(50) =null, @DemoName varchar(50) =null, @ListPrice decimal=null, @Quantity int=null, @LastUpdatedDate datetime =null, @LastUpdatedDateBegin datetime =null, @LastUpdatedDateEnd datetime =null as --select * from demo --usp_selectDemo '1' /**//**//**//* Powered by taeheelive@hotmail.com declare @sql varchar(500) set @sql = ' select DemoId, DemoName, ListPrice, Quantity, LastUpdatedDate from Demo where 1=1' if @DemoId is not null begin set @sql = @sql + ' AND DemoId = '''+@DemoId+'''' end if @DemoName is not null begin set @sql = @sql + ' AND DemoName = '''+@DemoName+'''' end if @ListPrice is not null begin set @sql = @sql + ' AND ListPrice = '+convert(varchar(10),@ListPrice)+'' end if @Quantity is not null begin set @sql = @sql + ' AND Quantity = '+convert(varchar(10),@Quantity)+'' end if @LastUpdatedDate is not null begin set @sql = @sql + ' AND LastUpdatedDate = '''+convert(varchar(10),@LastUpdatedDate,120)++''' ' end if @LastUpdatedDateBegin is not null begin set @sql = @sql + ' AND LastUpdatedDate >= '''+convert(varchar(10),@LastUpdatedDateBegin,120)++''' ' end if @LastUpdatedDateEnd is not null begin set @sql = @sql + ' AND LastUpdatedDate < '''+convert(varchar(10),@LastUpdatedDateEnd,120)+''' ' end --print (@sql) exec (@sql) */ /**//**//**//* Powered by 江千帆(cnblogs.com) */ SELECT * FROM Demo where1=1 and (@DemoId isnull or DemoId = @DemoId) and (@DemoName isnull or DemoName = @DemoName) and (@ListPrice isnull or ListPrice = @ListPrice) and (@Quantity isnull or Quantity = @Quantity) and (@LastUpdatedDate isnull or LastUpdatedDate = @LastUpdatedDate) and (@LastUpdatedDateBegin isnull or LastUpdatedDate >= @LastUpdatedDateBegin) and (@LastUpdatedDateEnd isnull or LastUpdatedDate < @LastUpdatedDateEnd) RETURN