日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

一个复杂的存储过程

發(fā)布時間:2023/11/27 生活经验 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个复杂的存储过程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先說明一下我這個存儲過程的功能:

根據(jù)不同的查詢條件組合進行查詢數(shù)據(jù),數(shù)據(jù)庫中有項目信息表Project

有項目區(qū)域表ProjectArea

項目信息表Project和項目區(qū)域表的關聯(lián)是通過ProjectArea和AreaID進行一對一關聯(lián),項目區(qū)域信息中的信息有所屬關系,我們可以把它理解成一個樹形結(jié)構(gòu)

下面是存儲過程對應的查詢界面和結(jié)果展示界面:查詢條件有項目編號、項目名稱、業(yè)務類型、項目區(qū)域、項目起止時間

下面是存儲過程

看了之后是否覺得很復雜,這就是我今天寫的一個比較難的存儲過程

?

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

-- Function: Select a record from table PropertyT

-- Date Created: 2015年月日

-- Created By:?? lijinchang

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

--這些是查詢條件(綠色部分)

ALTER PROCEDURE [dbo].[Lijc_GetProjectInfor]

??? @PageSize int,

??? @PageIndex int,

??? @recordCount int output,

??? @ProjectNo varchar(100)=null,

??? @ProjectName varchar(100)=null,

??? @ProjectArea varchar(100) =NULL,

??? @ProjectTimeS varchar(100) =NULL,

??? @ProjectTimeE varchar(100) =NULL,

??? @BusinessType varchar(100) =NULL,

??? @ProjectType varchar(100) =NULL

???

AS

--這里用一個sql判斷數(shù)據(jù)庫中臨時表是否存在,此臨時表用來--存儲遞歸查詢項目區(qū)域的信息,如果表存在則每查詢一次清空表數(shù)據(jù),如果表不存在則創(chuàng)建表結(jié)構(gòu)

if not exists(select 1 from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#Area') and type='U')

begin

??? create table #Area(AreaID varchar(20),AreaName varchar(200),ParentName varchar(200))

??? if @ProjectArea is null or @ProjectArea=''

??? with AreaTable(AreaID,AreaName,ParentName) as

??? ? (

?????? select AreaID,AreaName,ParentName from ProjectArea)

?????? insert #Area select * from AreaTable

??? else

??? begin

??? with AreaTable(AreaID,AreaName,ParentName) as

??? ? (

?????? select AreaID,AreaName,ParentName from ProjectArea a where a.AreaID=@ProjectArea

?????? union all

?????? select b.AreaID,b.AreaName,b.ParentName from ProjectArea b,AreaTable c where b.ParentID=c.AreaID

??? ? )

??? ? insert #Area select * from AreaTable

??? ? end

end

else

begin

??? truncate table #Area

??? if @ProjectArea is null or @ProjectArea=''

??? begin

??? with AreaTable(AreaID,AreaName,ParentName) as

??? ? (

?????? select AreaID,AreaName,ParentName from ProjectArea)

?????? insert #Area select * from AreaTable

??? end

??? else

??? begin

??? with AreaTable(AreaID,AreaName,ParentName) as

??? ? (

?????? select AreaID,AreaName,ParentName from ProjectArea a where a.AreaID=@ProjectArea

?????? union all

?????? select b.AreaID,b.AreaName,b.ParentName from ProjectArea b,AreaTable c where b.ParentID=c.AreaID

??? ? )

??? ?insert #Area select * from AreaTable

??? ?end

end

--下面這部分是組合查詢條件的過程

?DECLARE @sqlfilter? nVARCHAR(max)

?SET @sqlfilter =''

?IF(@ProjectNo IS NOT NULL and @ProjectNo<>'')

?set @sqlfilter=@sqlfilter+' and ProjectNo='''+@ProjectNo+''''

?IF(@ProjectName IS NOT NULL and @ProjectName<>'')

?set @sqlfilter=@sqlfilter+' and ProjectName like ''%'+@ProjectName+'%'''

?IF(@BusinessType IS NOT NULL and @BusinessType<>'')

?set @sqlfilter=@sqlfilter+' and BusinessType = '''+@BusinessType+''''

?IF(@ProjectTimeS IS NOT NULL and @ProjectTimeS<>'')

?set @sqlfilter=@sqlfilter+' and ProjectTime >= '''+@ProjectTimeS+''''

?IF(@ProjectTimeE IS NOT NULL and @ProjectTimeE<>'')

?set @sqlfilter=@sqlfilter+' and ProjectTime <= '''+@ProjectTimeE+''''

?

?

?DECLARE @sqlMain nvarchar(max)--一定要聲明為nvarchar類型

?set @sqlMain=''

?set @sqlMain=@sqlMain+'select @recordCount=count(1) from? ProjectArea A ,Project B where B.ProjectArea=A.AreaID and A.AreaID in(

? select areaID from? #Area) ' +@sqlfilter

?--exec sp_executesql @sqlMain N'@recordCount int output'? @recordCount output

--為輸出參數(shù)賦值

?exec sp_executesql@sqlMain,N'@recordCount int output', @recordCount output? ?--select @recordCount

--下面就是復雜的查詢過程

?set @sqlMain=''

?set @sqlMain='select tb.* from (select A.AreaName,B.*,row_Number() over(order by B.ProjectID) as rowIndex from ProjectArea A ,Project B where B.ProjectArea=A.AreaID and A.AreaID in(

? select areaID from? #Area) '+@sqlfilter+') tb'+' where tb.rowIndex between '+cast((@PageSize*@PageIndex+1) as nvarchar)+' and '+cast(@PageSize*@PageIndex+@PageSize as nvarchar)

?exec(@sqlMain)

?????????????????

轉(zhuǎn)載于:https://www.cnblogs.com/lijinchang/p/4255854.html

總結(jié)

以上是生活随笔為你收集整理的一个复杂的存储过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。