分页且带条件的存储过程

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

 

CREATE PROC [dbo].[user_index]
(
@PageIndex int,
@PageSize int,
@education_id nvarchar(200)=null,
@city_id nvarchar(200)=null,
@language_id nvarchar(200)=null,
@key nvarchar(200)=null,
@sex nvarchar(200)=null,
@height1 nvarchar(200)=null,
@height2 nvarchar(200)=null,
@age1 nvarchar(200)=null,
@age2 nvarchar(200)=null
)             
as

create table  #table
(id int,name nvarchar(100),education nvarchar(100),city nvarchar(100),sex nvarchar(100),age nvarchar(100),height nvarchar(100),
uid uniqueidentifier)

declare @Where nvarchar(4000)       
set @Where=''     
IF @education_id IS NOT null   AND  @education_id <>''     
BEGIN       
select @Where=@Where + ' AND general_education.general_education_id >= '+@education_id+''     
END   

IF @city_id IS NOT null   AND  @city_id <>''     
BEGIN       
select @Where=@Where + ' AND city.place_id = '+@city_id+''     
END    

IF @language_id IS NOT null   AND  @language_id <>''     
BEGIN       
select @Where=@Where + ' AND lang.language_id = '+@language_id+''     
END
 
IF @sex IS NOT null   AND  @sex <>''     
BEGIN
set @Where=@Where+' AND sex = '''+@sex+''''       
END    

IF @height1 IS NOT null   AND  @height1 <>'' and @height2 IS NOT null   AND  @height2 <>''    
BEGIN       
select @Where=@Where + ' and (height between '+@height1+' and '+@height2+')'     
END  

IF @age1 IS NOT null   AND  @age1 <>'' and @age2 IS NOT null   AND  @age2 <>''    
BEGIN       
select @Where=@Where + ' and (datediff(year,birthday,getdate()) between '+@age1+' and '+@age2+')'     
END     
       
IF @key IS NOT  null   AND @key <>''     
BEGIN       
set @Where=@Where + ' and user_work_exp.work_exp_description LIKE ''%'+@key+'%'''       
END
print @where         
       
declare @sql nvarchar(4000)  
set @sql='insert into #table select distinct user_profile_id,name,general_education.generl_education_desc,city.reg_name,sex,
datediff(year,birthday,getdate())as age,height,user_profile.uid
from user_profile left join general_education on user_profile.general_education_id=general_education.general_education_id
left join user_work_exp on user_work_exp.uid=user_profile.uid left join
(select location_id,location.place_id,reg_name from location left join location_place on location.place_id=location_place.place_id)as city
on city.location_id=user_profile.location_id left join
(select user_language_skill.language_id,user_language_skill.uid,user_language_type.language_name from
user_language_skill left join user_language_type on user_language_skill.language_id=user_language_type.language_id) as lang
on lang.uid=user_profile.uid where 1=1 '+ @Where
print @sql
exec(@sql)


if(@PageIndex=1)
begin
select top (@PageSize) * from #table
end
else
begin
select top (@PageSize) * from #table where id not in(select top(@PageSize*(@PageIndex-1)) id from #table)
end

posted on   秋天  阅读(779)  评论(1编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2008年2月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示