带有分页的存储过程

-- =============================================

-- =============================================
CREATE PROCEDURE [control].[mp_GetSellerTerminalList_Temp]
@Svalidtime DATETIME, /*验证开始时间*/
@Evalidtime DATETIME, /*验证结束时间*/
@CurrentPage int, /*当前页数*/
@pageSize int, /*每页记录数*/
@UseStatus int,

@ProvinceNo VARCHAR(100), /**/
@CityNo varchar(30),
@DistributorName nvarchar(100),
@OrderType int,
@MobileNo nvarchar(100),

@SellerNO nvarchar(100),
@OrderNo nvarchar(100),
@StrSrcBatchNo nvarchar(100), /*交易流水*/
@GoodsName nvarchar(100),
@DeviceID nvarchar(100),

@EnterpriseName nvarchar(100),
@VoucherId nvarchar(100),
--加外部订单号
@ExOrderNo nvarchar(100)
AS
BEGIN
DECLARE @Where VARCHAR(MAX)
SET @Where=‘ DistributorName<>‘‘‘‘ ‘

--过滤主表中的数据
DECLARE @sqlC VARCHAR(MAX)
DECLARE @whereC VARCHAR(MAX)
SET @whereC = ‘ 1=1 ‘
IF @Svalidtime IS NOT NULL
BEGIN
SET @whereC [email protected]+ ‘ and convert(varchar(10),ValidTime,120)>=‘‘‘+convert(varchar(10),@Svalidtime,120)+‘‘‘‘
END
IF @Evalidtime IS NOT NULL
BEGIN
SET @whereC [email protected]+ ‘ and convert(varchar(10),ValidTime,120)<=‘‘‘+convert(varchar(10),@Evalidtime,120)+‘‘‘‘
END
IF @GoodsName IS NOT NULL AND @GoodsName != ‘‘
BEGIN
SET @whereC [email protected]+‘ and GoodsName like ‘‘%‘[email protected]+‘%‘‘‘
END
IF @DeviceID IS NOT NULL AND @DeviceID != ‘‘
BEGIN
SET @whereC [email protected]+‘ and DeviceID like ‘‘%‘[email protected]+‘%‘‘‘
END
IF @EnterpriseName IS NOT NULL AND @EnterpriseName != ‘‘
BEGIN
SET @whereC [email protected]+‘ and EnterpriseName like ‘‘%‘[email protected]+‘%‘‘‘
END
IF @VoucherId IS NOT NULL AND @VoucherId != ‘‘
BEGIN
SET @whereC [email protected]+‘ and VoucherId = ‘‘‘[email protected]+‘‘‘‘
END
IF @SellerNO IS NOT NULL AND @SellerNO !=‘‘
BEGIN
SET @whereC [email protected]+‘ and SellerNo=‘‘‘[email protected]+‘‘‘‘
END
IF @OrderType IS NOT NULL AND @OrderType !=-1
BEGIN
SET @whereC [email protected]+‘ and OrderType=‘‘‘+cast(@OrderType AS VARCHAR)+‘‘‘‘
END
IF @OrderNo IS NOT NULL AND @OrderNo !=‘‘
BEGIN
SET @whereC [email protected]+‘ and OrderNo=‘‘‘[email protected]+‘‘‘‘
END
IF @StrSrcBatchNo IS NOT NULL AND @StrSrcBatchNo!=‘‘
BEGIN
SET @whereC [email protected]+‘ and BatchNo+TradeNo = ‘‘‘[email protected]+‘‘‘‘
END
PRINT @whereC

--过滤出主表数据到临时表中

IF object_id(‘tempdb..#tbVoucherValidLog‘) is NOT NULL
BEGIN
DROP TABLE #tbVoucherValidLog
END

CREATE TABLE #tbVoucherValidLog
(
LogID BIGINT,
VoucherType TINYINT,
BatchNo VARCHAR(10),
TradeNo VARCHAR(10),
SrcBatchNo VARCHAR(10),
SrcTradeNo VARCHAR(10),
ConsumeAmount DECIMAL(8,2),
GoodsName VARCHAR(50),
SettlementPrice MONEY,
OrderNo VARCHAR(50),
VoucherId VARCHAR(50),
SellerNo VARCHAR(20),
GoodsID BIGINT,
GoodsAmount INT,
StoreName VARCHAR(50),
ValidTime DATETIME,
DeviceID VARCHAR(50),
VoucherNo VARCHAR(50),
ValidateType TINYINT,
SettlementAllPrice MONEY,
OrderType TINYINT,
EnterpriseNo VARCHAR(50),
EnterpriseName VARCHAR(50),
IsReSet BIT
)

EXEC(‘insert into #tbVoucherValidLog
select LogID,VoucherType,BatchNo,TradeNo,SrcBatchNo,SrcTradeNo,ConsumeAmount,GoodsName,SettlementPrice,OrderNo,VoucherId,SellerNo,GoodsID,GoodsAmount,StoreName,
ValidTime,DeviceID,VoucherNo,ValidateType,SettlementAllPrice,OrderType,EnterpriseNo,EnterpriseName,IsReSet
from control.tb_VoucherValidLog where ‘[email protected])

PRINT ‘1.Insert tbVoucherValidLog ‘+cast(GETDATE() AS VARCHAR)

--control.tb_Voucher
DECLARE @whereB VARCHAR(MAX)
SET @whereB = ‘ UseStatus<>5 and exists (select VoucherId from #tbVoucherValidLog b where b.VoucherId = a.VoucherId) ‘
PRINT @whereB
IF @UseStatus IS NOT NULL AND @UseStatus !=-1
BEGIN
SET @whereB [email protected]+‘ and UseStatus=‘+CAST(@UseStatus AS VARCHAR)
PRINT @whereB
SET @Where = @Where+‘ and UseStatus is not null‘
END
IF @MobileNo IS NOT NULL AND @MobileNo !=‘‘
BEGIN
SET @whereB [email protected]+‘ and MobileNo=‘‘‘[email protected]+‘‘‘‘
SET @Where = @Where+‘ and MobileNo is not null‘
END
PRINT @whereB
IF object_id(‘tempdb..#tbVoucher‘) is NOT NULL
BEGIN
DROP TABLE #tbVoucher
END

CREATE TABLE #tbVoucher
(
OrderType TINYINT,
CustomerName VARCHAR(50),
VoucherId VARCHAR(50),
VoucherType TINYINT,
MobileNo VARCHAR(20),
AllPointAmout DECIMAL(8,2),
UseStatus TINYINT,
ID BIGINT,
RePrintTimes INT,
VoucherNo VARCHAR(50)
)

EXEC(‘insert into #tbVoucher
select OrderType,CustomerName,VoucherId,VoucherType,MobileNo,AllPointAmout,UseStatus,ID,RePrintTimes,VoucherNo
from control.tb_Voucher a where ‘[email protected])
PRINT ‘2.Insert tbVoucher ‘+cast(GETDATE() AS VARCHAR)

--control.tb_VoucherDetails
DECLARE @whereA VARCHAR(MAX)
SET @whereA = ‘ exists (select VoucherNo from #tbVoucherValidLog b where b.VoucherNo = a.VoucherNo) ‘

IF @DistributorName IS NOT NULL AND @DistributorName !=‘‘
BEGIN
SET @DistributorName [email protected]+‘ and DistributorName=‘‘‘[email protected]+‘‘‘‘
END
PRINT @whereA
IF object_id(‘tempdb..#tbVoucherDetails‘) is NOT NULL
BEGIN
DROP TABLE #tbVoucherDetails
END

CREATE TABLE #tbVoucherDetails
(
DistributorName VARCHAR(50),
SinglePrice MONEY,
VoucherNo VARCHAR(50),
GoodsID BIGINT,
OrderNo VARCHAR(50)
)

EXEC(‘insert into #tbVoucherDetails
select distinct DistributorName,SinglePrice,VoucherNo,GoodsID,OrderNo
from control.tb_VoucherDetails a where ‘[email protected])
PRINT ‘3.Insert tbVoucherDetails ‘+cast(GETDATE() AS VARCHAR)
--control.tb_Order
DECLARE @whereO VARCHAR(MAX)
SET @whereO = ‘ exists (select OrderNo from #tbVoucherValidLog b where b.OrderNo = a.OrderNo)‘
IF object_id(‘tempdb..#tbOrder‘) is NOT NULL
BEGIN
DROP TABLE #tbOrder
END

CREATE TABLE #tbOrder
(
OrderNo VARCHAR(50),
ExOrderNo VARCHAR(50)
)

EXEC(‘insert into #tbOrder select OrderNo,ExOrderNo from control.tb_Order a where ‘[email protected])
PRINT ‘4.Insert tbOrder ‘+cast(GETDATE() AS VARCHAR)
--control.tb_MovieCard
-- DECLARE @whereM VARCHAR(MAX)
-- SET @whereM= ‘ exists (select VoucherNo from #tbVoucherValidLog b where b.VoucherNo = a.CardNo)‘
-- IF object_id(‘tempdb..##tbMovieCard‘) is NOT NULL
--BEGIN
--DROP TABLE ##tbMovieCard
--END
--EXEC(‘select * into ##tbMovieCard from control.tb_MovieCard a where ‘[email protected])
--PRINT ‘5.Insert tbMovieCard ‘+cast(GETDATE() AS VARCHAR)
--control.tb_CustomerOrder
DECLARE @whereCo VARCHAR(MAX)
SET @whereCo =‘ exists (select OrderNo from #tbVoucherValidLog b where b.OrderNo = a.OrderNo)‘
IF object_id(‘tempdb..#tbCustomerOrder‘) is NOT NULL
BEGIN
DROP TABLE #tbCustomerOrder
END

CREATE TABLE #tbCustomerOrder
(
OrderNo VARCHAR(50),
CustomerName VARCHAR(50),
SinglePrice MONEY
)

EXEC(‘insert into #tbCustomerOrder
select OrderNo,CustomerName,SinglePrice
from control.tb_CustomerOrder a where ‘[email protected])
PRINT ‘6.Insert tbCustomerOrder ‘+cast(GETDATE() AS VARCHAR)

--
IF @CityNo IS NOT NULL AND @CityNo!=‘‘
BEGIN
SET @Where = @Where + ‘ and CityNo=‘‘‘[email protected]+‘‘‘‘
END
IF @ExOrderNo IS NOT NULL AND @ExOrderNo !=‘‘
BEGIN
SET @Where = @Where + ‘ and ExOrderNo=‘‘‘[email protected]+‘‘‘‘
END

----------------------------------------------------------------------------------------------
CREATE TABLE #tb_GetSellerTerminalListTable
(
LogID INT,
DistributorName NVARCHAR(100),
OrderType1 NVARCHAR(20),
VoucherType1 NVARCHAR(20),
VoucherId1 NVARCHAR(30),
MobileNo NVARCHAR(30),
StrSrcBatchNo NVARCHAR(30),
StrSrcTradeNo NVARCHAR(30),
SinglePrice NVARCHAR(50),
TolPrice NVARCHAR(50),
GoodsName NVARCHAR(50),
SettlementPrice NVARCHAR(50),
OrderNo NVARCHAR(50),
UseStatus NVARCHAR(20),
VoucherId NVARCHAR(50),
ExOrderNo NVARCHAR(50),
SellerNo NVARCHAR(30),
GoodsAmount NVARCHAR(20),
StoreName NVARCHAR(40),
ValidTime DATETIME,
DeviceID NVARCHAR(30),
VoucherNo NVARCHAR(30),
SettlementAllPrice NVARCHAR(30),
GoodsNum1 NVARCHAR(30),
OrderType NVARCHAR(20),
EnterpriseName NVARCHAR(40),
VoucherType NVARCHAR(20),
ValidateType1 NVARCHAR(20),
ValidateType NVARCHAR(20),
ID NVARCHAR(30),
ValidNo NVARCHAR(20),
RePrintCount NVARCHAR(20),
GoodsNum NVARCHAR(30),
TradeNo NVARCHAR(20),
ProvinceNo NVARCHAR(20),
CityNo NVARCHAR(20),
AreaNo NVARCHAR(20),
AreaName NVARCHAR(40),
IsReset NVARCHAR(10)
)
CREATE INDEX index_validTime ON #tb_GetSellerTerminalListTable(OrderNo);

INSERT INTO #tb_GetSellerTerminalListTable
SELECT C.LogID,
CASE WHEN B.OrderType IN(4,5) AND C.VoucherType<>61 THEN B.CustomerName
WHEN C.VoucherType=61 THEN K.CustomerName
ELSE A.DistributorName
END AS DistrbutorName,
D.[name] as OrderType1,
F.[name] as VoucherType1,
CASE WHEN LEN(C.VoucherId)>0 THEN replace(C.VoucherId,substring(C.VoucherId,4,len(C.VoucherId)-7),‘*********‘)
ELSE ‘‘ END as VoucherId1,
(case when C.VoucherType=53 then replace(B.MobileNo,substring(B.MobileNo,4,len(B.MobileNo)-7),‘*********‘)
WHEN C.VoucherType =61 THEN ‘13888888888‘
else B.MobileNo end) as MobileNo,
C.BatchNo+C.TradeNo AS StrSrcBatchNo,
C.SrcBatchNo+C.SrcTradeNo AS StrSrcTradeNo,
Convert(decimal(18,2),
(CASE
WHEN B.VoucherType = 54 OR B.VoucherType = 60 THEN CASE
WHEN B.AllPointAmout
= 0 THEN
0
ELSE A.SinglePrice
/ B.AllPointAmout
END
WHEN C.VoucherType=61 THEN K.SinglePrice
ELSE A.SinglePrice
END)) AS SinglePrice,

Convert(decimal(18,2),
(case when C.ValidateType=2 then -(
(CASE
WHEN B.VoucherType = 54 OR B.VoucherType = 60 THEN CASE
WHEN B.AllPointAmout
= 0 THEN
0
ELSE C.ConsumeAmount
* (A.SinglePrice / B.AllPointAmout)
END
ELSE C.ConsumeAmount * A.SinglePrice END))
ELSE
(CASE
WHEN B.VoucherType = 54 OR B.VoucherType = 60 THEN CASE
WHEN B.AllPointAmout
= 0 THEN
0
ELSE C.ConsumeAmount
* (A.SinglePrice / B.AllPointAmout)
END
WHEN C.VoucherType = 61 THEN C.ConsumeAmount* K.SinglePrice
ELSE C.ConsumeAmount * A.SinglePrice
END)END)) AS TolPrice,

C.GoodsName,C.SettlementPrice,
C.OrderNo,
CASE WHEN C.VoucherType=61 THEN 3 ELSE B.UseStatus END UseStatus,
C.VoucherId AS VoucherId,M.ExOrderNo,C.SellerNo,C.GoodsAmount,C.StoreName,
C.ValidTime,C.DeviceID,C.VoucherNo,
(
CASE
WHEN C.ValidateType = 2 THEN -(C.SettlementAllPrice)
ELSE C.SettlementAllPrice
END
) AS SettlementAllPrice,
(
CASE
WHEN B.VoucherType = 55 THEN CONVERT(NVARCHAR(30), C.GoodsAmount)
WHEN (
B.VoucherType = 53
OR B.VoucherType = 54
OR B.VoucherType = 60
OR C.VoucherType = 61
) THEN CONVERT(NVARCHAR(30), C.ConsumeAmount)
WHEN (B.VoucherType = 51 OR B.VoucherType = 52) THEN CONVERT(NVARCHAR(30), C.GoodsAmount)
END
) AS GoodsNum1,
C.OrderType,C.EnterpriseName,C.VoucherType,E.[name] AS ValidateType1,C.ValidateType,
CASE WHEN C.VoucherType=61 THEN 1 ELSE B.ID END ID,C.TradeNo as ValidNo,
CASE WHEN C.VoucherType=61 THEN 0 ELSE B.RePrintTimes END AS RePrintCount,
(case when B.VoucherType=55 then convert(nvarchar(30),C.GoodsAmount)+‘ 份‘ when (B.VoucherType=53 or B.VoucherType=54 or B.VoucherType=60)
then convert(nvarchar(30),C.ConsumeAmount)+‘ 点‘ when (B.VoucherType=51 or B.VoucherType=52)
then convert(nvarchar(30),C.GoodsAmount)+‘ 张‘ when C.VoucherType=61 then convert(nvarchar(30),C.GoodsAmount)+‘ 次‘ end) as GoodsNum ,C.TradeNo,
G.ProvinceNo,G.CityNo,G.AreaNo,I.AreaName,C.IsReSet
FROM
#tbVoucherValidLog AS C
LEFT JOIN
#tbVoucher AS B ON C.VoucherId = B.VoucherId AND C.VoucherNo = B.VoucherNo
LEFT JOIN
#tbVoucherDetails AS A ON C.VoucherNo = A.VoucherNo AND C.GoodsID = A.GoodsID
--LEFT JOIN
--##tbMovieCard AS J ON C.VoucherNo = J.CardNo
LEFT JOIN
#tbCustomerOrder AS K ON C.OrderNo = K.OrderNo
LEFT JOIN base.tb_DictionaryData AS D ON D.dicId = 1086 AND d.[value] = C.OrderType
LEFT JOIN base.tb_DictionaryData AS E ON E.dicId = 1185 AND E.[value] = C.ValidateType
LEFT JOIN base.tb_DictionaryData AS F ON F.dicId = 1119 AND F.[value] = C.VoucherType
LEFT JOIN control.tb_Cinema AS H ON H.CinemaNo = C.EnterpriseNo
LEFT JOIN control.tb_Organization AS G ON G.OrganizationNo = H.OrganizationNo
LEFT JOIN #tbOrder AS M ON M.OrderNo = A.OrderNo
LEFT JOIN base.tb_Area AS I ON G.CityNo = I.AreaNo

PRINT @Where

---实现分页返回数据

DECLARE @sqlText VARCHAR(MAX)
declare @Sp nvarchar(100);
declare @Ep nvarchar(100);
set @Sp=((@CurrentPage-1)*@PageSize)+1;
set @[email protected]*@PageSize;

set @sqlText=‘select * from (select *, ROW_NUMBER() OVER(Order by ValidTime DESC) AS RowNumber from #tb_GetSellerTerminalListTable as a where ‘[email protected]+‘) as b where ‘;
set @[email protected]+‘ RowNumber BETWEEN ‘[email protected]+‘ and ‘[email protected];

EXEC(@SqlText)

--统计总金额总是
declare @TongJISum nvarchar(300);

set @TongJISum=‘select‘‘合计‘‘ as total, sum(Convert(decimal(18,2),SettlementAllPrice)) as AllSettlementAllPrice from #tb_GetSellerTerminalListTable where ‘[email protected];

EXEC(@TongJISum);

declare @conutT nvarchar(500);
set @conutT=‘select count(*) as PageCount from #tb_GetSellerTerminalListTable where ‘[email protected];
EXEC(@conutT)

DROP TABLE #tbVoucherValidLog
DROP TABLE #tbVoucher
DROP TABLE #tbVoucherDetails
DROP TABLE #tbCustomerOrder
DROP TABLE #tbOrder
END

时间: 2024-10-09 15:56:04

带有分页的存储过程的相关文章

创建sql自定义的函数及商品分页sql存储过程

--商品筛选时判断品牌ID是否存在 --select dbo.isValite(94,94)create function isValite(@brandId int,@bId int)returns intas begin Declare @rNumber int if @brandId = @bId set @rNumber = 1 else set @rNumber = 0 if @bId = 0 set @rNumber = 1 return @rNumberendgo --判断商品筛选

一个高效的数据分页的存储过程 可以轻松应付百万数据

一个高效的数据分页的存储过程 可以轻松应付百万数据 CREATE PROCEDURE pageTest --用于翻页的测试--需要把排序字段放在第一列 (@FirstID nvarchar(20)=null, --当前页面里的第一条记录的排序字段的值@LastID nvarchar(20)=null, --当前页面里的最后一条记录的排序字段的值@isNext bit=null, --true 1 :下一页:false 0:上一页@allCount int output, --返回总记录数@pag

SQLServer分页查询存储过程

项目中用到的SQLServer分页查询存储过程. [存储过程] create PROCEDURE prcPageResult -- 获得某一页的数据 -- @currPage int = 1,                         --当前页页码 (即Top currPage) @showColumn varchar(2000) = '*',           --需要得到的字段 (即 column1,column2,......) @strCondition varchar(200

T-SQL分页功能存储过程

分页功能存储过程 ALTER PROCEDURE [dbo].[P_SplitPagesQuery] @TablesName NVARCHAR(MAX),--表名或视图名(只能传单一表名) @PK NVARCHAR(MAX)='',--主键(可以为空!) @StrSort NVARCHAR(MAX)='',--排序字段(按照这个字段对查询结果进行排序),不能为空 @SortType INT =0,--排序方式,默认为0(升序),1为降序 @StrWhere NVARCHAR(MAX)='',--

oracle入门(8)——实战:支持可变参数、多种条件、多个参数排序、分页的存储过程查询组件

[本文介绍] 学了好几天,由于项目需要,忙活了两天,写出了个小组件,不过现在还只能支持单表操作.也没考虑算法上的优化,查询速度要比hibernate只快了一点点,可能是不涉及多表查询的缘故吧,多表的情况下才更快. 经非专业的测试,在有分页的情况下,在300万条数据里面查询的时间保持在0.1秒内.相同查询条件+分页的情况下,hibernate 用时0.3秒内. 不分页的条件下,查出来的数据越多,时间越长,时间长的话,跟hibernate相相比就没什么优势了. [思路] 我的思路是从java传来”字

asp.net中分页与存储过程的一些总结

一.接上文,使用的是jquery AJAX 进行分页 分页存储过程代码如下: ALTER PROCEDURE [dbo].[USP_GetAlbumByPage] @pageIndex int,--当前页 @pageSize int,--页容量 @totalCount int out--数据的总条数 AS BEGIN --定义开始和结束的下标 Declare @startIndex int,@endIndex int set @startIndex = (@pageIndex - 1 )*@pa

SQL Server 分页查询存储过程

1.低效的分页 SELECT TOP m-n+1 * FROM publish WHERE (id NOT IN (SELECT TOP n-1 id FROM publish)) 但这个存储过程有一个致命的缺点,就是它含有NOT IN字样.虽然我可以把它改造为: SELECT TOP 页大小 * FROM Table1 WHERE not exists (select * from (select top (页大小*页数) * from table1 order by id) b where

mybatis generator生成带有分页的Mybatis代码

MyBatis开发,最让人开心的就是可以随意写SQL,这样有多好的性能的SQL都可以进行调优. 但是MyBatis的优点也是它的缺点,不论什么项目都需要编写SQL,令人头疼的要命,一般业务(例如单表操作)的简单查询.修改.删除.插入,都需要自己手工去编写SQL. 还好有第三方的软件给我解决这些事情,可以像使用Hibernate一样使用MyBatis,当需要进行特殊定制的再进行修改. 1.      本文档主要描述 1.表格: 2.视图:3.存储过程:4.自定义的MyBatis 1.1.     

SQL Server分页的存储过程写法以及性能比较

------创建数据库data_Test ----- create database data_Test GO use data_Test GO create table tb_TestTable --创建表 ( id int identity(1,1) primary key, userName nvarchar(20) not null, userPWD nvarchar(20) not null, userEmail nvarchar(40) null ) GO ------插入数据---