1,创建数据表:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Demo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Demo]
GO
Create TABLE [dbo].[Demo] (
[DemoId] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[DemoName] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[ListPrice] [decimal](18, 0) NULL ,
[Quantity] [int] NULL ,
[LastUpdatedDate] [datetime] NULL
) ON [PRIMARY]
GO
Alter TABLE [dbo].[Demo] WITH NOCHECK ADD
CONSTRAINT [PK_ApplePie] PRIMARY KEY CLUSTERED
(
[DemoId]
) ON [PRIMARY]
GO
drop table [dbo].[Demo]
GO
Create TABLE [dbo].[Demo] (
[DemoId] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[DemoName] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[ListPrice] [decimal](18, 0) NULL ,
[Quantity] [int] NULL ,
[LastUpdatedDate] [datetime] NULL
) ON [PRIMARY]
GO
Alter TABLE [dbo].[Demo] WITH NOCHECK ADD
CONSTRAINT [PK_ApplePie] PRIMARY KEY CLUSTERED
(
[DemoId]
) ON [PRIMARY]
GO
2,创建存储过程:
Create 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) */
Set @DemoName='%'+RTIM(@DemoName)+'%'
Select DemoId, DemoName, ListPrice, Quantity, LastUpdatedDate FROM Demo
where 1=1
and (@DemoId is null or DemoId = @DemoId)
and (@DemoName is null or DemoName Like @DemoName)
and (@ListPrice is null or ListPrice = @ListPrice)
and (@Quantity is null or Quantity = @Quantity)
and (@LastUpdatedDate is null or LastUpdatedDate = @LastUpdatedDate)
and (@LastUpdatedDateBegin is null or LastUpdatedDate >= @LastUpdatedDateBegin)
and (@LastUpdatedDateEnd is null or LastUpdatedDate < @LastUpdatedDateEnd)
GO
@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) */
Set @DemoName='%'+RTIM(@DemoName)+'%'
Select DemoId, DemoName, ListPrice, Quantity, LastUpdatedDate FROM Demo
where 1=1
and (@DemoId is null or DemoId = @DemoId)
and (@DemoName is null or DemoName Like @DemoName)
and (@ListPrice is null or ListPrice = @ListPrice)
and (@Quantity is null or Quantity = @Quantity)
and (@LastUpdatedDate is null or LastUpdatedDate = @LastUpdatedDate)
and (@LastUpdatedDateBegin is null or LastUpdatedDate >= @LastUpdatedDateBegin)
and (@LastUpdatedDateEnd is null or LastUpdatedDate < @LastUpdatedDateEnd)
GO