--商品筛选时判断品牌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

转载于:https://www.cnblogs.com/simpleBlue3/p/3879491.html

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

  1. sprk sql自定义UDF函数

    1 自定义UDF函数与scala定义函数的方式是一样的(也可以定义方法,调用的时候转换为函数即可) 2 自定义的函数需要注册后才能使用,注册的方式为 session.udf.register(自定义名 ...

  2. spark sql自定义UDF函数-java语言

    背景说明 基于spark sql开发过程中,需要一些类似与官网提供的 int().from_json()等自定函数处理数据.下属将简单讲解通过java如何实现spark sql自定义函数 官方UDF接 ...

  3. oracle sql常用的函数,界别Oracle和SQL Server常用函数

    区分Oracle和SQL Server常用函数 一.数学函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) ...

  4. 95-910-148-源码-FlinkSQL-Flink SQL自定义聚合函数

    1.美图 2.基本使用 ​ Flink Table/SQL Api中自带了一些常见的聚合函数,例如sum.min.max等,但是在实际开发中需要自定义符合业务需求的聚合函数,先从一个实际案例入手:设备 ...

  5. Flink SQL自定义聚合函数

    <2021年最新版大数据面试题全面开启更新> 本篇幅介绍Flink Table/SQL中如何自定义一个聚合函数,介绍其基本用法.撤回定义以及与源码结合分析每个方法的调用位置. 基本使用 F ...

  6. php 自带sql防注入函数,php 最简单sql防注入函数与方法_PHP教程

    mysql教程_real_escape_string - 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集. 但是注意:该函数并不转义 % 和 _.另外,最好不要对整条sql语句 ...

  7. oracle SQL以结尾函数,Oracle学习笔记--SQL查询和SQL函数(转) -- 迷失de天空 -- 编程爱好者......

    A: 数据定义语言: 用于改变数据库结构,包括创建,修改,删除数据库对象 创建表: Create Table 表名 ( 列名 列类型, 列名 列类型, -- ) 例: Create Table ven ...

  8. Spark SQL自定义函数

    文章目录 自定义函数分类 自定义UDF 自定义UDAF[了解] 自定义函数分类 类似于hive当中的自定义函数, spark同样可以使用自定义函数来实现新的功能. spark中的自定义函数有如下3类 ...

  9. EntityFramework Core 2.0自定义标量函数两种方式

    前言 上一节我们讲完原始查询如何防止SQL注入问题同时并提供了几种方式.本节我们继续来讲讲EF Core 2.0中的新特性自定义标量函数. 自定义标量函数两种方式 在EF Core 2.0中我们可以将 ...

最新文章

  1. python【力扣LeetCode算法题库】695- 岛屿的最大面积(深搜)
  2. 【云隐】STM32F103C8T6实现串口IAP方式升级固件
  3. kmean python实现
  4. 详解:Linux Chrony 设置服务器集群同步时间
  5. I/O多路转接之poll,epoll
  6. html设置按钮样式变为椭圆,css border-radius圆形变为椭圆形,位置:绝对
  7. 快速入门PyTorch(3)--训练一个图片分类器和多 GPUs 训练
  8. linux 磁盘被挂载2个目录,Linux检测并挂载第二块硬盘的步骤
  9. GTK+图形化应用程序开发学习笔记(四)—容器、构件
  10. 多机能同时制造时(对于发现的问题, 【使用】 并记入 【 问题一览 】)。
  11. Springboot之Thymeleaf 表单提交
  12. 关于MacBook电池的正确使用方法,和保养技巧
  13. 倍福PLC选型--如何看电机是多圈绝对值还是单圈绝对值编码器
  14. 面试必问之JVM原理 1
  15. ARFoundation从零开始3-创建ARFoundation项目
  16. SPAN_EXCLUSIVE_INCLUSIVE用法、区别
  17. acrobat 打印PDF时错误:Error: typecheck; OffendingCommand: show
  18. BGP高防服务器是什么?要怎么选?
  19. CEF与Off-Screen Rendering与Transparent Background
  20. HR面试题(史上最全、持续更新、吐血推荐)

热门文章

  1. 年度最理性 AI 分析文章:预测 AI 未来,大部分人陷入了 7 大误区
  2. 单手也能创奇迹!独臂博士单手敲代码获奖 30 余项:感恩所有的善意
  3. 1 分钟带你认识从 � 到 锟斤拷
  4. 腾讯云发布微瓴开放平台LinkBase,助力智慧建筑产业生态全面升级
  5. mysql5.7 生成列 generated column
  6. React 的性能优化(一)当 PureComponent 遇上 ImmutableJS
  7. 使用Feign时如何设置Feign的Header信息
  8. JavaScript原型-进阶者指南
  9. HTTP Slow Attack测试工具SlowHTTPTest
  10. JavaScript 日期联动选择器