【原创源码】(02):通用查询存储过程,可应用于多表关联的动态条件查询。
【原创源码】(02):通用查询存储过程,可应用于多表关联的动态条件查询。
欢迎大家发表意见(漏洞,性能等)。在博客园社区以外转载,请注明作者和出处。谢谢!
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

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) */
SELECT DemoId, DemoName, ListPrice, Quantity, LastUpdatedDate FROM Demo
where 1=1
and (@DemoId is null or DemoId = @DemoId)
and (@DemoName is null or DemoName = @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

3,查询方法:
查询所有记录:usp_selectDemo
查询DemoId='1'的记录:usp_selectDemo '1'
查询DemoName='maxdemo'的记录:usp_selectDemo null,'maxdemo'
等等。
4,Sorry~! 临时修改了5处Bug
change '+@ListPrice+'' to mailto:'+@ListPrice+''
change mailto:'+@ListPrice+'' to mailto:'+@ListPrice+''
change '''+@LastUpdatedDate+''' to mailto:'+@ListPrice+''
change '''+@LastUpdatedDateBegin+''' to mailto:'+@ListPrice+''
change '''+@LastUpdatedDateEnd+''' to mailto:'+@ListPrice+''
汗~~,类型转换写的好差,不知哪位高手愿意补完美。谢谢!
ps:为什么上面的字都是当成蓝色的email了,不晓得怎么去掉蓝色和底线,郁闷中。
2006-10-18
5,个人感觉是比较好的一个思路,特大胆放到首页,希望大家讨论哈,优缺点。谢谢!
灵感来自于master数据库中的存储过程“sp_tables”
2006-10-19
6,添加一段数据访问层的应用代码SQLServerDAL,PetShop4.0结构
2006-10-19
7,存储过程添加了/* Powered by 江千帆(cnblogs.com) */的方法(默认方法),
因为感觉比我的要优秀,感谢江千帆的意见!2006-10-19
注意!在博客园社区以外转载,必须注明:
作者:Clark Chan
和原文出处:http://clarkchan.cnblogs.com/
否则谢绝转载!
posted on 2006-10-18 15:27 Clark Chan 阅读(3116) 评论(14) 收藏 举报



}
浙公网安备 33010602011771号