今天寫了乙個通過多個引數查詢列表記錄的儲存過程,查詢和排序的時候涉及了多個表和字段,寫下來記錄一下.也給需要的同僚借鑑一下.
正文如下:
use [dbname]
go/****** object: storedprocedure [dbo].[getproductlistbymanyparams] script date: 07/20/2012 11:55:43 ******/
set ansi_nulls on
goset quoted_identifier on
go--exec getproductlistbymanyparams 100,0,1,-1,-1,-1,'',1
create procedure [dbo].[getproductlistbymanyparams]
@pagesize int, --每頁記錄數
@pageindex int, --當前頁碼
@productbuytype int, --商品型別:1,一口價.2,競拍.3,**; -1,代表所有
@prodcutcategoryid int, --商品類別. -1,代表所有
@provinceid int, --店鋪所在省 -1,代表所有
@cityid int, --店鋪所在市 -1,代表所有
@productname varchar(50), --產品名稱(搜尋中的關鍵字,可以模糊查詢)
@ordertype int --排序方式(-1.不限,1.按**從高到低,2.按**從低到高,3.按賣家信用從高到低,4.按銷量從高到低,5.按銷量從低到高;)
as begin
declare @sql varchar(2000)
declare @sqlcount varchar(2000)
declare @where varchar(2000)
declare @order varchar(200)
set @sql=''
set @where='where p.memberid=m.id and p.issale=1 and p.isaudit=1'
if(@productbuytype<>-1)
begin
set @where=@where+' and p.productbuytype='+cast(@productbuytype as varchar)
endif(@prodcutcategoryid<>-1)
begin
declare @isfirst int
set @isfirst=(select dbo.getproductcategoryparentid(@prodcutcategoryid))--//獲取分類的父類id(此處用函式寫的)
if(@isfirst<>0)--如果分類的父類id是0則說明是一級分類
begin
set @where=@where+' and p.categoryid='+cast(@prodcutcategoryid as varchar)
endelse--需要查詢該分類以及其下子分類的產品
begin
set @where=@where+' and (p.categoryid in (select elementid from site_category where parentid='+cast(@prodcutcategoryid as varchar)+') or p.categoryid='+cast(@prodcutcategoryid as varchar)+')'
endend
if(@provinceid<>-1)
begin
set @where=@where+' and p.shopid in(select id from shop_info where provinceid='+cast(@provinceid as varchar)+')'
endif(@cityid<>-1)
begin
set @where=@where+' and p.shopid in(select id from shop_info where cityid='+cast(@cityid as varchar)+')'
endif(@productname<>'')
begin
set @where=@where+' and p.productname like ''%'+@productname+'%'''
endif(@ordertype<>-1)
begin
if(@ordertype=1)
begin
if(@productbuytype=1)
begin
set @order=' order by p.pbuy1price desc'
endelse if(@productbuytype=2)
begin
set @order=' order by p.pbuy2saleprice desc'
endelse if(@productbuytype=3)
begin
set @order=' order by p.pbuy3saleprice desc'
endelse
begin
set @order=' order by p.id desc'
endend
else if(@ordertype=2)
begin
if(@productbuytype=1)
begin
set @order=' order by p.pbuy1price asc'
endelse if(@productbuytype=2)
begin
set @order=' order by p.pbuy2saleprice asc'
endelse if(@productbuytype=3)
begin
set @order=' order by p.pbuy3saleprice asc'
endelse
begin
set @order=' order by p.id desc'
endend
else if(@ordertype=3)
begin
set @order=' order by m.sellcredit desc'
endelse if(@ordertype=4)
begin
set @order=' order by (p.productnum-p.productleftnum) desc'
endelse if(@ordertype=5)
begin
set @order=' order by (p.productnum-p.productleftnum) asc'
endend
else
begin
set @order=' order by p.id desc'
endset @sql = ' select * from ( select row_number() over('+ @order +') as rownum,
p.* from product p,member_info m '
+ @where +' )as d
where rownum >= '+convert(varchar,(@pageindex+1-1)*@pagesize+1)
+' and rownum<='+convert(varchar,(@pageindex+1)*@pagesize)--分頁語句
set @sqlcount='select count(*) as recordcount from product p,member_info m '+@where
exec (@sql)
exec (@sqlcount)
print @sql
print @sqlcount
end用到的函式:
use [dbname]
go/****** object: userdefinedfunction [dbo].[getproductcategoryparentid] script date: 07/20/2012 15:09:54 ******/
set ansi_nulls on
goset quoted_identifier on
gocreate function [dbo].[getproductcategoryparentid]
(@categoryid [int]
)returns int
asbegin
declare @parentid int
declare @sql varchar(2000)
select @parentid = parentid from dbo.site_category
where
elementid = @categoryid
return @parentid
end
呼叫函式的個數:select dbo.函式名(引數);注: 一定要加dbo. 否則會報錯:函式不是可以識別的 內建函式名稱。
儲存過程 IN 多個引數解決
今天寫儲存過程的時候,遇到了傳入多個引數,不能用in匹配的問題。例如 傳入引數 b a b c pl sql語句 select from table where var in b 不能匹配,因為pl sql把b當做乙個完整的字串,select from table where var in a b ...
sql儲存過程in 多個引數
首先要建立乙個擷取字串的函式,新建乙個查詢,把下面 複製進去執行。函式sqlitin的第乙個引數是儲存過程要in的字串,第二個引數是分隔符 create function splitin c varchar 200 split varchar 2 returns t table col varcha...
儲存過程的引數
儲存過程的引數 儲存過程如何與呼叫者進行互動呢,互動方式之一就是引數。注意 宣告變數的那句話就不需要了!引數 名字 型別 查詢 set uname call test4 2,uname select uname as myunameaa 結果 其實 end前面的select my uname 也是不...