存储过程

白昼怎懂夜的黑 提交于 2020-01-10 11:43:24

create table City
(
Cid int identity,
CName varchar(50),
Pid int
)
create table Users
(
Id int identity(1001,1),
UserName varchar(50),
Provinceid int,
cityid int,
district int,
townid int,
sites varchar(100)
)

 


create view vw_cha
as
select us.*,c1.CName as 'ProvinceidName',c2.CName as 'cityidName',c3.CName as 'districtName',c4.CName 'townidName',ROW_NUMBER() over(order by Id) as rid
from City c1 join Users us on c1.Cid=us.Provinceid
join City c2 on us.cityid=c2.Cid
join City c3 on us.district=c3.Cid
join City c4 on us.townid=c4.Cid

 

 

if OBJECT_ID('proc_fenye') is not null
drop proc proc_fenye
go
create proc proc_fenye
@Provinceid int=0,
@cityid int=0,
@district int=0,
@townid int=0,
@UserName varchar(50)=null,
@pageindex int=0,
@pagesize int=0,
@pagecount int out
as
declare @sql varchar(max),
@where varchar(max),
@rid int,
@tountcount nvarchar(max)
set @sql='';
set @rid=(@pageindex-1)*@pagesize;
set @where=' where 1=1';
set @tountcount='select @tourt=count(*) from vw_cha as vw_cha';
--对省进行查询
if @Provinceid!=0
set @where+=' and vw_cha.Provinceid='+str(@Provinceid);
--对市进行查询
if @cityid!=0
set @where+=' and vw_cha.cityid='+str(@cityid);
--对县进行查询
if @district!=0
set @where+=' and vw_cha.district='+str(@district);
--对镇进行查询
if @townid!=0
set @where+=' and vw_cha.townid='+str(@townid);
if @UserName is not null
set @where+=' and vw_cha.UserName like ''%'+@UserName+'%''';
--拼接查询总条数带条件查询
set @tountcount+=@where;
--执行总条数查询
exec sp_executesql @tountcount,N'@tourt int out',@tourt=@pagecount out
--拼接分页查询sql语句
set @sql='select top '+str(@pagesize)+' * from vw_cha '+@where+' and vw_cha.rid>'+str(@rid);
exec (@sql)


-----------------------------------------------------------------

create table AddUser
(
UserId int identity,
UserName varchar(100),--姓名
ProinceId int,--省
CityId int,--市
TownId int,--区
ZhenId int,--镇
UserAddr varchar(200)--地址
)
create table Addr
(
AId int identity,
AddressName varchar(100),
PId int
)
create view Vw_PP
as
select au.UserId,au.UserName,ad.AddressName as s1,aa.AddressName as s2,dd.AddressName as s3,aw.AddressName,au.UserAddr
from AddUser au join Addr ad on au.ProinceId=ad.AId join Addr aa
on au.CityId=aa.AId join Addr dd on au.TownId=dd.AId join Addr aw
on au.ZhenId=aw.AId

create view Ss_oo
as
select adu.UserId,adu.UserName,ad.AddressName as as1,ad.AddressName as2,ad2.AddressName as as3,ad3.AddressName,ad4.AddressName as as4 ,ROW_NUMBER()over(order by UserId)as rid
from AddUser adu join Addr ad on adu.ProinceId=ad.AId
join Addr ad2 on adu.CityId = ad2.AId
join Addr ad3 on adu.TownId = ad3.AId
join Addr ad4 on adu.ZhenId = ad4.AId
select * from Ss_oo
select * from Vw_PP

create view Ss_oo
as
select adu.UserId,adu.UserName,ad.AddressName as as1,ad.AddressName as2,ad2.AddressName as as3,ad3.AddressName,ad4.AddressName as as4 ,ROW_NUMBER()over(order by UserId)as rid
from AddUser adu join Addr ad on adu.ProinceId=ad.AId
join Addr ad2 on adu.CityId = ad2.AId
join Addr ad3 on adu.TownId = ad3.AId
join Addr ad4 on adu.ZhenId = ad4.AId
select * from Ss_oo

if OBJECT_ID('sp_Xianshi') is not null
drop proc sp_Xianshi
go
create proc sp_Xianshi
@ProinceId int=0,--省
@CityId int=0,--市
@TownId int=0,--区
@ZhenId int=0,--镇
@UserName varchar(50)=null,
@pageindex int=0,
@pagesize int=0,
@pagecount int out--输出总条数
as
declare @sql varchar(max),
@where varchar(max),
@rid varchar(max),
@toucount nvarchar(max)
set @sql=''
set @where = ' where 1=1 '
set @rid = (@pageindex-1)*@pagesize
set @toucount = 'select @to= COUNT(1) from Ss_oo'

set @toucount+=@where;
exec sp_executesql @toucount ,N'@to int out',@to=@pagecount out


set @rid ='select top '+STR(@pagesize)+'* from Ss_oo'+@where+'and Ss_oo.rid>'+STR(@rid)
exec(@rid)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!