CREATE PROCEDURE [dbo].[Get_Result]
(
@Classify nvarchar(50),--产品分类
@ProductName nvarchar(50),--产品名称
@minPrice decimal,--价格范围
@maxPrice decimal--价格范围
)
AS
BEGIN
DECLARE @strsql varchar(2000)
SET @strsql= 'select * from t_Products where 1=1'
IF @ProductName IS NOT NULL AND @ProductName!=''
BEGIN
SET @strsql = @strsql + ' and ProductName like ''%' + @ProductName + '%'''
END
IF @Classify IS NOT NULL AND @Classify!=''
BEGIN
SET @strsql = @strsql + ' and Classify=''' + @Classify + ''''
END
IF @minPrice IS NOT NULL AND @minPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price>=''' + CONVERT(varchar(30),@minPrice) + ''''
END
IF @maxPrice IS NOT NULL AND @maxPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price<=''' + CONVERT(varchar(30),@maxPrice) + ''''
END
PRINT @strsql
EXEC(@strsql)
END怎么把这两个存储过程写成一个?
达到的效果就是传入
(
@startIndex int,--起始索引
@endIndex int,--结束索引
@docount bit,--标记,0:查出详细信息,1:查出符合条件的产品总数
@Classify nvarchar(50),--产品分类
@ProductName nvarchar(50),--产品名称(用like模糊查询)
@minPrice decimal,--价格范围
@maxPrice decimal--价格范围
)
查询出符合条件,且在[startIndex ,endIndex]范围内的数据求指导,谢谢。。
(
@Classify nvarchar(50),--产品分类
@ProductName nvarchar(50),--产品名称
@minPrice decimal,--价格范围
@maxPrice decimal--价格范围
)
AS
BEGIN
DECLARE @strsql varchar(2000)
SET @strsql= 'select * from t_Products where 1=1'
IF @ProductName IS NOT NULL AND @ProductName!=''
BEGIN
SET @strsql = @strsql + ' and ProductName like ''%' + @ProductName + '%'''
END
IF @Classify IS NOT NULL AND @Classify!=''
BEGIN
SET @strsql = @strsql + ' and Classify=''' + @Classify + ''''
END
IF @minPrice IS NOT NULL AND @minPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price>=''' + CONVERT(varchar(30),@minPrice) + ''''
END
IF @maxPrice IS NOT NULL AND @maxPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price<=''' + CONVERT(varchar(30),@maxPrice) + ''''
END
PRINT @strsql
EXEC(@strsql)
END怎么把这两个存储过程写成一个?
达到的效果就是传入
(
@startIndex int,--起始索引
@endIndex int,--结束索引
@docount bit,--标记,0:查出详细信息,1:查出符合条件的产品总数
@Classify nvarchar(50),--产品分类
@ProductName nvarchar(50),--产品名称(用like模糊查询)
@minPrice decimal,--价格范围
@maxPrice decimal--价格范围
)
查询出符合条件,且在[startIndex ,endIndex]范围内的数据求指导,谢谢。。
CREATE PROCEDURE [dbo].[Get_Result]
(
@startIndex int,--起始索引
@endIndex int,--结束索引
@docount bit,--标记。0:查出符合条件的产品的详细信息;1:查出符合条件的产品的总数
)
as
set nocount on
if(@docount=1)
select count(*) AS Counts from t_Products
else
begin
declare @indextable table(id int IDENTITY(1,1) ,nid nvarchar(50))
set rowcount @endIndex
insert into @indextable(nid) select ProductID from t_Products order by ProductID
select * from t_Products O,@indextable t where O.ProductID=t.nid
and t.id between @startIndex and @endIndex order by t.id
end
set nocount off
CREATE PROCEDURE [dbo].[Get_Result]
(
@Classify nvarchar(50),--产品分类
@ProductName nvarchar(50),--产品名称
@minPrice decimal,--价格范围
@maxPrice decimal--价格范围
)
AS
BEGIN
DECLARE @strsql varchar(2000)
SET @strsql= 'select * from t_Products where 1=1'
IF @ProductName IS NOT NULL AND @ProductName!=''
BEGIN
SET @strsql = @strsql + ' and ProductName like ''%' + @ProductName + '%'''
END
IF @Classify IS NOT NULL AND @Classify!=''
BEGIN
SET @strsql = @strsql + ' and Classify=''' + @Classify + ''''
END
IF @minPrice IS NOT NULL AND @minPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price>=''' + CONVERT(varchar(30),@minPrice) + ''''
END
IF @maxPrice IS NOT NULL AND @maxPrice!=0
BEGIN
SET @strsql = @strsql + ' and Price<=''' + CONVERT(varchar(30),@maxPrice) + ''''
END
PRINT @strsql
EXEC(@strsql)
END怎么把这两个存储过程写成一个?
达到的效果就是传入
(
@startIndex int,--起始索引
@endIndex int,--结束索引
@docount bit,--标记,0:查出详细信息,1:查出符合条件的产品总数
@Classify nvarchar(50),--产品分类
@ProductName nvarchar(50),--产品名称(用like模糊查询)
@minPrice decimal,--价格范围
@maxPrice decimal--价格范围
)
查询出符合条件,且在[startIndex ,endIndex]范围内的数据求指导,谢谢。。
如果有多表,表之间如何关联?
发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
抱歉,太急了,没写清楚。CREATE TABLE [dbo].[t_Products]
(
[ProductID] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ProductName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Producer] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[AddTime] [datetime] NOT NULL,
[Classify] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Price] [decimal](10, 2) NOT NULL
)
表t_Products,要按条件查出符合的产品,并分页。
希望会的朋友帮帮忙,麻烦了。
http://topic.csdn.net/u/20100203/17/8F916471-597D-481A-B170-83BCEFE3B199.html应一个朋友的要求,贴上收藏的SQL常用分页的办法~~ 表中主键必须为标识列,[ID] int IDENTITY (1,1) 1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式:
SELECT TOP 页记录数量 *
FROM 表名
WHERE (ID NOT IN
(SELECT TOP (每页行数*(页数-1)) ID
FROM 表名
ORDER BY ID))
ORDER BY ID
//自己还可以加上一些查询条件
例:
select top 2 *
from Sys_Material_Type
where (MT_ID not in
(select top (2*(3-1)) MT_ID from Sys_Material_Type order by MT_ID))
order by MT_ID 2.分页方案二:(利用ID大于多少和SELECT TOP分页) 语句形式:
SELECT TOP 每页记录数量 *
FROM 表名
WHERE (ID >
(SELECT MAX(id)
FROM (SELECT TOP 每页行数*页数 id FROM 表
ORDER BY id) AS T)
)
ORDER BY ID 例:
SELECT TOP 2 *
FROM Sys_Material_Type
WHERE (MT_ID >
(SELECT MAX(MT_ID)
FROM (SELECT TOP (2*(3-1)) MT_ID
FROM Sys_Material_Type
ORDER BY MT_ID) AS T))
ORDER BY MT_ID 3.分页方案三:(利用SQL的游标存储过程分页)
create procedure SqlPager
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int --每页行数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1, @rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off 4.总结:
其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。
建议优化的时候,加上主键和索引,查询效率会提高。 通过SQL 查询分析器,显示比较:我的结论是:
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句
分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用