SQL Server基本儲存過程分頁

2021-10-25 01:53:05 字數 3524 閱讀 6946

if (exists (select * from sys.objects where name = 'page_userinfo')) drop proc page_userinfo   --判斷儲存過程是否存在,存在則刪除然後重建。

gocreate proc page_userinfo --建立儲存過程

@name nvarchar(255),--使用者名稱

@pageindex int,--第幾頁

@pagesize int--一頁多少條

as set nocount on; --不返回計數,提高應用程式效能

begin --開始

declare @pagebefore int;--建立頁數

declare @pagerear int;--建立頁數

declare @condition nvarchar(2000); --建立where條件

set @pagebefore=@pagesize*@pageindex; --起始頁

set @pagerear=@pagebefore+@pagesize;--結束頁

set @condition=' where 1=1 ';

if(@name<>'')

set @condition=@condition+' and name like ''%'+@name+'%''';

--建立乙個虛擬表插入userinfo表資料

--獲取分頁資料

--獲取總數

exec('

declare @table table(

iid int identity,

id int,

name nvarchar(20),

*** int,

age int,

birthday datetime

)insert @table

select * from userinfo '+@condition+' order by id desc

select * from @table where iid>'+@pagebefore+' and iid<='+@pagerear+'

select count(*) as rows from @table;');

end;--結束

呼叫方式

exec page_userinfo '' ,1,10 (賦值:使用者名稱,頁數,一頁多少條)

use [newhrp]

go/****** object: storedprocedure [dbo].[pro_traderecord] script date: 11/10/2020 11:49:12 ******/

set ansi_nulls on

goset quoted_identifier on

goalter procedure pro_traderecord

@starttime datetime,@endtime datetime

asif @starttime=''

begin

set @starttime=dateadd(mm,datediff(mm,0,getdate()),0)

endif @endtime=''

begin

set @endtime=dateadd(mi,-1,dateadd(mm,datediff(mm,0,getdate())+1,0))

endelse

begin

set @endtime=convert(char(10),@endtime,23)+' 23:59:00'

endselect cs_trtradedate as 日期,zaocan as 早餐,zaocanrenshu as 早餐人數,zhongcan as 中餐,zhongcanrenshu as 中餐人數,wangcan as 晚餐,wangcanrenshu as 晚餐人數,zaocanrenshu+zhongcanrenshu+wangcanrenshu as 用餐人數統計,zaocan+zhongcan+wangcan as 用餐金額統計,chaoshijine as 超市金額,chaoshirenshu 超市人數,tanfenjine as 湯粉面檔金額,tanfenrenshu as 湯粉面檔人數,zaocan+zhongcan+wangcan+chaoshijine+tanfenjine as 合計

from

(select

convert(char(10),cs_trtradetime,23) as cs_trtradedate,

sum(case when a.cs_tdname = '早餐' and c.sys_rgname='飯堂' then cs_trtrademoney else 0 end) as zaocan,

sum(case when a.cs_tdname = '早餐'and c.sys_rgname='飯堂' then 1 else 0 end) as zaocanrenshu,

sum(case when a.cs_tdname = '中餐' and c.sys_rgname='飯堂' then cs_trtrademoney else 0 end) as zhongcan,

sum(case when a.cs_tdname = '中餐' and c.sys_rgname='飯堂' then 1 else 0 end) as zhongcanrenshu,

sum(case when a.cs_tdname = '晚餐' and c.sys_rgname='飯堂' then cs_trtrademoney else 0 end) as wangcan,

sum(case when a.cs_tdname = '晚餐' and c.sys_rgname='飯堂' then 1 else 0 end) as wangcanrenshu,

sum(case c.sys_rgname when '超市' then a.cs_trtrademoney else 0 end) as chaoshijine,

sum(case c.sys_rgname when '超市' then 1 else 0 end) as chaoshirenshu,

sum(case c.sys_rgname when '湯粉面檔' then a.cs_trtrademoney else 0 end) as tanfenjine,

sum(case c.sys_rgname when '湯粉面檔' then 1 else 0 end) as tanfenrenshu

from cs_traderecord a left join sys_equipment b on a.sys_equipmentid=b.sys_equipmentid

left join sys_region c on b.sys_regionid=c.sys_regionid

where cs_trtradetime between @starttime and @endtime

group by convert(char(10),cs_trtradetime,23)

)t

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...

SQLSERVER儲存過程

sqlserver儲存過程使用說明書 引言首先介紹一下什麼是儲存過程 儲存過程就是將常用的或很複雜的工作,預先用 sql語句寫好並用乙個指定的名稱儲存起來,並且這樣的語句是放在資料庫中的,還可以根據條件執行不同 sql語句,那麼以後要叫資料庫提供與已定義好的儲存過程的功能相同的服務時,只需呼叫 ex...

SQL Server 儲存過程

儲存過程概念 儲存過程優點 儲存過程的介面 儲存過程的解析 編譯過程 儲存過程安全性 檢視儲存過程 加密 解密儲存過程 儲存過程概念 儲存過程 stored procedure 是一組為了完成特定功能的sql語句集,經編譯後儲存在資料庫中。使用者通過指定儲存過程的名字並給出引數 帶參儲存過程 來執行...