--商品筛选时判断品牌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存储过程的更多相关文章

  1. sql自定义日期函数,返回范围内日期和星期数表。

    Create function [dbo].[FUN_GenerateTime] ( @begin_date datetime, -- 起始时间 @end_date datetime -- 结束时间 ...

  2. SQL Server如何定位自定义标量函数被那个SQL调用次数最多浅析

    前阵子遇到一个很是棘手的问题,监控系统DPA发现某个自定义标量函数被调用的次数非常高,高到一个离谱的程度.然后在Troubleshooting这个问题的时候,确实遇到了一些问题让我很是纠结,下文是解决 ...

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

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

  4. SQL中CONVERT()函数用法详解

    SQL中CONVERT函数格式: CONVERT(data_type,expression[,style]) 参数说明: expression 是任何有效的 Microsoft® SQL Server ...

  5. 仿Orm 自动生成分页SQL

    分页的写法 自从用上了Orm,分页这种事就是腰不酸腿不痛了.不过有时候想用纯粹的ado.net来操作,希望返回的数据是原生的DataTable或DbDataReader类似的东西,故研究下怎么生成分页 ...

  6. [Python自学] day-21 (1) (请求信息、html模板继承与导入、自定义模板函数、自定义分页)

    一.路由映射的参数 1.映射的一般使用 在app/urls.py中,我们定义URL与视图函数之间的映射: from django.contrib import admin from django.ur ...

  7. SQL Server 自定义聚合函数

    说明:本文依据网络转载整理而成,因为时间关系,其中原理暂时并未深入研究,只是整理备份留个记录而已. 目标:在SQL Server中自定义聚合函数,在Group BY语句中 ,不是单纯的SUM和MAX等 ...

  8. SQL自定义函数split分隔字符串

    SQL自定义函数split分隔字符串 一.F_Split:分割字符串拆分为数据表 Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max ...

  9. DB 查询分析器 方便地创建DB2自定义函数

    DB 查询分析器 方便地创建DB2自定义函数                           马根峰            (广东联合电子服务股份有限公司, 广州 510300) 摘要       ...

随机推荐

  1. [CF467D] Fedor and Essay

    After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying com ...

  2. .NET Core ORM 类库Petapoco中对分页Page添加Order By对查询的影响

    最近一直在使用Petapoco+Entity Framework Core结合开发一套系统. 使用EFCore进行Code First编码,使用PMC命令生成数据库表的信息. 使用Petapoco进行 ...

  3. Service Mesh 初体验

    前言 计算机软件技术发展到现在,软件架构的演进无不朝着让开发者能够更加轻松快捷地构建大型复杂应用的方向发展.容器技术最初是为了解决运行环境的不一致问题而产生的,随着不断地发展,围绕容器技术衍生出来越来 ...

  4. ASP.NET Core 3.0 : 二十八. 在Docker中的部署以及docker-compose的使用

    本文简要说一下ASP.NET Core 在Docker中部署以及docker-compose的使用  (ASP.NET Core 系列目录). 系统环境为CentOS 8 . 打个广告,求职中.. 一 ...

  5. PHP JSON乱码简洁的解决办法

    PHP JSON乱码简洁的解决办法 $arr = array('ret'=>400, 'msg'=>'服务器地址不允许', 'data'=>''); foreach ( $arr a ...

  6. codeblocks 调试不停止的解决办法。

    CB的工程路径不能有中文,也不能有空格. 所以一定要全英文路径,而且空格要用下划线代替. 否则,调试的时候,codeblocks不会在断点处停止.

  7. day34作业

    作业 查看岗位是teacher的员工姓名.年龄 select name,age from teacher where post='teacher'; 查看岗位是teacher且年龄大于30岁的员工姓名 ...

  8. Mysql数据库(八)存储过程与存储函数

    一.创建存储过程与存储函数 1.创建存储过程(实现统计tb_borrow1数据表中指定图书编号的图书的借阅次数) mysql> delimiter // mysql> CREATE PRO ...

  9. Spring框架学习笔记(5)——Spring Boot创建与使用

    Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...

  10. Cannot read property 'forEach' of undefined

    在singer-detail组件中,有一个_normalizeSongs()方法,遍历数组 _normalizeSongs(list) { let ret = []; list.forEach(ite ...