创建sql自定义的函数及商品分页sql存储过程
--商品筛选时判断品牌ID是否存在
--select dbo.isValite(94,94)
create function isValite(@brandId int,@bId int)
returns int
as
begin
Declare @rNumber int
if @brandId = @bId
set @rNumber = 1
else
set @rNumber = 0
if @bId = 0
set @rNumber = 1
return @rNumber
end
go
--判断商品筛选输入价格是否有
--select dbo.comparePrice(269.00,100,300)
create function comparePrice(@price decimal(8,2),@priceA decimal(8,2), @priceB decimal(8,2))
returns int
as
begin
declare @j int
if @price >= @priceA and @price <= @priceB
set @j = 1
else
set @j = 0
if @priceA = 0 and @priceB = 0
set @j = 1
return @j
end
go
--函数相当于split
create function f_split(@SourceSql varchar(1000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
as
begin
declare @i int
set @SourceSql = RTRIM(LTRIM(@SourceSql))
set @i = charindex(@StrSeprate,@SourceSql)--CHARINDEX 返回字符串中指定表达式的起始位置。
while @i >= 1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,LEN(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>'\'
insert @temp values(@SourceSql)
return
end
go
-- select * from dbo.f_split('198,199,204,210',',')
--函数判断sku是否包含该输入的筛选属性值串
create function isExists(@stringCode varchar(500),@numbers varchar(500))
returns int
as
begin
declare @returnValue int
if @numbers = '0'
set @returnValue = 1
else if (select count(a) from dbo.f_split(@stringCode,',')) < (select count(a) from dbo.f_split(@numbers,','))
set @returnValue = 0
else if (select count(j.a) from (select a from dbo.f_split(@stringCode,',')) j inner join (select a from dbo.f_split(@numbers,',')) s on j.a = s.a ) = (select count(a) from dbo.f_split(@numbers,','))
set @returnValue = 1
else
set @returnValue = 0
return @returnValue
end
go
----商品分页刷新筛选
create proc GetExtractCommodityRefresh
(
@SortId int,--商品类别Id
@BrandId int,--品牌ID
@PriceA decimal(8,2),--查询价格A
@PriceB decimal(8,2),--查询价格B
@PageNumber int,--页数
@CommoditiesPerPage int,
@StrSelectionValueId varchar(500),--商品筛选属性值Id
@orderAse int,-- 排序 销量 价格 评分 上架时间 @orderAse = 3 为降序(desc)
@HowManyCommodities int output
)
as
declare @Commodity table
(
RowNumber int,
CommodityId int,
CommodityName varChar(300),
MarketPrice decimal(8,2),
CommodityAddTime DateTime,
BrandId int,
SortId int,
CSelectionAttribute varchar(300),
SkuImg varchar(300),
SkuPrice decimal(8,2),
SkuQuanlity int
)
if @orderAse = 1
insert into @Commodity
select ROW_NUMBER() over(order by cc.commodityId),cc.CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg, SkuPrice,SkuQuanlity from (Select distinct c.CommodityId,c.CommodityName,c.MarketPrice,c.CommodityAddTime,c.BrandId,c.SortId,c.CSelectionAttribute,s.SkuImg, s.SkuPrice,s.SkuQuanlity from Commodity c left join sku s on c.CommodityId = s.CommodityId ) as cc
where SortId = @SortId and 1 = (select dbo.isValite(BrandId,@BrandId))and 1= (select dbo.isExists(CSelectionAttribute,@StrSelectionValueId))and 1 = (select dbo.comparePrice(SkuPrice,@PriceA,@PriceB))
else if @orderAse = 2
insert into @Commodity
select ROW_NUMBER() over(order by cc.SkuPrice),cc.CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg, SkuPrice,SkuQuanlity from (Select distinct c.CommodityId,c.CommodityName,c.MarketPrice,c.CommodityAddTime,c.BrandId,c.SortId,c.CSelectionAttribute,s.SkuImg, s.SkuPrice,s.SkuQuanlity from Commodity c left join sku s on c.CommodityId = s.CommodityId ) as cc
where SortId = @SortId and 1 = (select dbo.isValite(BrandId,@BrandId))and 1= (select dbo.isExists(CSelectionAttribute,@StrSelectionValueId))and 1 = (select dbo.comparePrice(SkuPrice,@PriceA,@PriceB))
else if @orderAse = 3
insert into @Commodity
select ROW_NUMBER() over(order by cc.SkuPrice desc),cc.CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg, SkuPrice,SkuQuanlity from (Select distinct c.CommodityId,c.CommodityName,c.MarketPrice,c.CommodityAddTime,c.BrandId,c.SortId,c.CSelectionAttribute,s.SkuImg, s.SkuPrice,s.SkuQuanlity from Commodity c left join sku s on c.CommodityId = s.CommodityId ) as cc
where SortId = @SortId and 1 = (select dbo.isValite(BrandId,@BrandId))and 1= (select dbo.isExists(CSelectionAttribute,@StrSelectionValueId))and 1 = (select dbo.comparePrice(SkuPrice,@PriceA,@PriceB))
else if @orderAse = 4
insert into @Commodity
select ROW_NUMBER() over(order by cc.commodityId desc),cc.CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg, SkuPrice,SkuQuanlity from (Select distinct c.CommodityId,c.CommodityName,c.MarketPrice,c.CommodityAddTime,c.BrandId,c.SortId,c.CSelectionAttribute,s.SkuImg, s.SkuPrice, s.SkuQuanlity from Commodity c left join sku s on c.CommodityId = s.CommodityId ) as cc
where SortId = @SortId and 1 = (select dbo.isValite(BrandId,@BrandId))and 1= (select dbo.isExists(CSelectionAttribute,@StrSelectionValueId))and 1 = (select dbo.comparePrice(SkuPrice,@PriceA,@PriceB))
else if @orderAse = 5
insert into @Commodity
select ROW_NUMBER() over(order by cc.CommodityAddTime desc),cc.CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg, SkuPrice,SkuQuanlity from (Select distinct c.CommodityId,c.CommodityName,c.MarketPrice,c.CommodityAddTime,c.BrandId,c.SortId,c.CSelectionAttribute,s.SkuImg, s.SkuPrice ,s.SkuQuanlity from Commodity c left join sku s on c.CommodityId = s.CommodityId ) as cc
where SortId = @SortId and 1 = (select dbo.isValite(BrandId,@BrandId))and 1= (select dbo.isExists(CSelectionAttribute,@StrSelectionValueId))and 1 = (select dbo.comparePrice(SkuPrice,@PriceA,@PriceB))
select @HowManyCommodities = count(*) from @Commodity
select CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg,SkuPrice,SkuQuanlity from @Commodity where RowNumber > (@PageNumber - 1) * @CommoditiesPerPage
and RowNumber <= @PageNumber * @CommoditiesPerPage
go
declare @HowManyCommodities real
exec GetExtractCommodityRefresh 19,0,0,1000,1,20,'0',1,@HowManyCommodities output
select @HowManyCommodities
go
----通过商品筛选条件提取商品condition
create proc GetSelectionConditionCommodities
(
@SortId int,
@BrandId int,
@PriceA decimal(8,2),
@PriceB decimal(8,2),
@StrSelectionValueId varchar(500),--商品筛选属性值Id
@HowManyCommodities int output
)
as
declare @Commodity table
(
RowNumber int,
CommodityId int,
CommodityName varChar(300),
MarketPrice decimal(8,2),
CommodityAddTime DateTime,
BrandId int,
SortId int,
CSelectionAttribute varchar(300),
SkuImg varchar(300),
SkuPrice decimal(8,2),
SkuQuanlity int
)
insert into @Commodity
select ROW_NUMBER() over(order by cc.commodityId),cc.CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg, SkuPrice,SkuQuanlity from (Select distinct c.CommodityId,c.CommodityName,c.MarketPrice,c.CommodityAddTime,c.BrandId,c.SortId,c.CSelectionAttribute,s.SkuImg, s.SkuPrice,s.SkuQuanlity from Commodity c left join sku s on c.CommodityId = s.CommodityId ) as cc
where SortId = @SortId and 1 = (select dbo.isValite(BrandId,@BrandId))and 1= (select dbo.isExists(CSelectionAttribute,@StrSelectionValueId))and 1 = (select dbo.comparePrice(SkuPrice,@PriceA,@PriceB))
select @HowManyCommodities = count(*) from @Commodity
select CommodityId,CommodityName,MarketPrice,CommodityAddTime,BrandId,SortId,CSelectionAttribute,SkuImg,SkuPrice,SkuQuanlity from @Commodity
go
declare @HowManyCommodities real
exec GetSelectionConditionCommodities 19,0,0,0,'0',@HowManyCommodities output
select @HowManyCommodities
go
创建sql自定义的函数及商品分页sql存储过程的更多相关文章
- sql自定义日期函数,返回范围内日期和星期数表。
Create function [dbo].[FUN_GenerateTime] ( @begin_date datetime, -- 起始时间 @end_date datetime -- 结束时间 ...
- SQL Server如何定位自定义标量函数被那个SQL调用次数最多浅析
前阵子遇到一个很是棘手的问题,监控系统DPA发现某个自定义标量函数被调用的次数非常高,高到一个离谱的程度.然后在Troubleshooting这个问题的时候,确实遇到了一些问题让我很是纠结,下文是解决 ...
- EntityFramework Core 2.0自定义标量函数两种方式
前言 上一节我们讲完原始查询如何防止SQL注入问题同时并提供了几种方式.本节我们继续来讲讲EF Core 2.0中的新特性自定义标量函数. 自定义标量函数两种方式 在EF Core 2.0中我们可以将 ...
- SQL中CONVERT()函数用法详解
SQL中CONVERT函数格式: CONVERT(data_type,expression[,style]) 参数说明: expression 是任何有效的 Microsoft® SQL Server ...
- 仿Orm 自动生成分页SQL
分页的写法 自从用上了Orm,分页这种事就是腰不酸腿不痛了.不过有时候想用纯粹的ado.net来操作,希望返回的数据是原生的DataTable或DbDataReader类似的东西,故研究下怎么生成分页 ...
- [Python自学] day-21 (1) (请求信息、html模板继承与导入、自定义模板函数、自定义分页)
一.路由映射的参数 1.映射的一般使用 在app/urls.py中,我们定义URL与视图函数之间的映射: from django.contrib import admin from django.ur ...
- SQL Server 自定义聚合函数
说明:本文依据网络转载整理而成,因为时间关系,其中原理暂时并未深入研究,只是整理备份留个记录而已. 目标:在SQL Server中自定义聚合函数,在Group BY语句中 ,不是单纯的SUM和MAX等 ...
- SQL自定义函数split分隔字符串
SQL自定义函数split分隔字符串 一.F_Split:分割字符串拆分为数据表 Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max ...
- DB 查询分析器 方便地创建DB2自定义函数
DB 查询分析器 方便地创建DB2自定义函数 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要 ...
随机推荐
- [Luogu3065][USACO12DEC]第一!First!
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- [Luogu2455] [SDOI2006]线性方程组
题目描述 已知n元线性一次方程组. 其中:n<=50, 系数是[b][color=red]整数<=100(有负数),bi的值都是整数且<300(有负数)(特别感谢U14968 mmq ...
- mfc字符转码
std::wstring UTF8ToUnicode(const std::string& utf8string) { , utf8string.c_str(), -, NULL, ); ...
- VMware安装和linux(centos7)系统安装
下载centos系统ISO镜像 安装linux系统和winsdows安装系统一样,需要系统文件.浏览器访问centos官网进行下载,http://www.centos.org,因为是国外网站所有下载速 ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
- SpringBoot学习(一)基础篇
目录 关于Springboot Springboot优势 快速入门 关于SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭 ...
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)
84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...
- PCES - alpha阶段测试报告
测试计划 测试目的 本测试目的在于测试项目完成情况,以及分析测试结果,为下一轮开发提供解决方案 测试项目 学生用户登录测试 课程信息检索测试 服务器测试 在测试过程中出现的Bug 用户界面间的跳转逻辑 ...
- "Dependency on app with no migrations: %s" % key[0]
问题描述:我在model中建好模型类,运行的控制台就报错误: 解决方法:1,首先需要在setting中重载AUTH_USER_MODEL AUTH_USER_MODEL = 'users.UserPr ...
- 前端与算法 leetcode 26. 删除排序数组中的重复项
目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...