产品列表分页提取:
根据任意大类获取叶结点类别列表
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
--用途:获取产品列表
--添加时间:2008/06/02
--修改时间:2008/07/27
--添加人:李雪侠
--查询所有产品
CREATE PROCEDURE [dbo].[Admin_Search_w_Product]
@IndexPage int=1, --页码
@PageSize int=10, --页大小
@RecordCount int=0 output, --数据总数
@RootCode nvarchar(20)='', --对应w_productsandcategorys的ParentId
@DirCode nvarchar(20)='', --对应ProductCode直接对应的PCID
@ProductID nvarchar(20)='', --产品编号
@ProductName nvarchar(50)='', --产品名称
@ProductBrand nvarchar(50)='', --产品?
@OrderField nvarchar(20)='a.AddTime', --排序域
@isDesc int=1 --是否降序
AS
declare @sql nvarchar(4000),
@sqlWhere_Src nvarchar(1000),
@sqlSort nvarchar(100),
@sqlSortDesc nvarchar(100),
@sqlFiled nvarchar(200),
@sqlPcid nvarchar(20),
@sqlWhere_Cate nvarchar(1000),
@sqlFinal nvarchar(4000),
@Sqlstr nvarchar(4000),
@sqlOrderStr nvarchar(1000)
begin
set @sql='SELECT ProductCode,ProductName,ForOrder,Storage,VPrice,Price,AddTime ';
set @sql=@sql+'FROM Product '
--搜索条件:产品代码
if(@ProductID!='')begin
set @sqlWhere_Src='WHERE ProductCode LIKE '''+@ProductID+'%'''
end
--搜索条件:产品名称
if(@ProductName!='')begin
if(@sqlWhere_Src!='')begin
set @sqlWhere_Src=@sqlWhere_Src+' AND ProductName LIKE '''+@ProductName+'%'''
end
else begin
set @sqlWhere_Src='WHERE ProductName LIKE '''+@ProductName+'%'''
end
end
--搜索条件:产品品牌
if(@ProductBrand!='')begin
if(@sqlWhere_Src!='')begin
set @sqlWhere_Src=@sqlWhere_Src+' AND ProductBrand LIKE '''+@ProductBrand+'%'''
end
else begin
set @sqlWhere_Src='WHERE ProductBrand LIKE '''+@ProductBrand+'%'''
end
end
--排序条件
if(@isDesc=1)begin
set @sqlOrderStr=' ORDER BY '+@OrderField+' DESC'
end
else begin
set @sqlWhere_Src=' ORDER BY '+@OrderField+' ASC'
end
--print(@sql+@sqlWhere_Src)
if(@sqlWhere_Src!='')begin
set @sql=@sql+@sqlWhere_Src
end
--exec(@sql)
--类别处理
if(@DirCode!='')begin
set @sqlWhere_Cate='SELECT A.* FROM ('+@sql+') A,w_ProductsAndCategorys B '
set @sqlWhere_Cate=@sqlWhere_Cate+'WHERE A.ProductCode=B.ProductCode AND B.PCID='
set @sqlWhere_Cate=@sqlWhere_Cate+@DirCode
end
else begin
if(@RootCode!='')begin
set @sqlWhere_Cate='SELECT A.* FROM ('+@sql+') A,'
set @sqlWhere_Cate=@sqlWhere_Cate+' (select PCID,ProductCode '
set @sqlWhere_Cate=@sqlWhere_Cate+' from w_productsandcategorys) B,'
set @sqlWhere_Cate=@sqlWhere_Cate+' (select A.w_P_PCID,B.CategoryName from '
set @sqlWhere_Cate=@sqlWhere_Cate+' ('
set @sqlWhere_Cate=@sqlWhere_Cate+' select A.PCID,A.w_P_PCID from '
set @sqlWhere_Cate=@sqlWhere_Cate+' w_CateSelect A,'
set @sqlWhere_Cate=@sqlWhere_Cate+' ('
set @sqlWhere_Cate=@sqlWhere_Cate+' select A.PCID,A.CategoryName From '
set @sqlWhere_Cate=@sqlWhere_Cate+' w_ProductCategorys A'
set @sqlWhere_Cate=@sqlWhere_Cate+' Left outer Join w_ProductCategorys B '
set @sqlWhere_Cate=@sqlWhere_Cate+' on A.PCID=B.ParentID where B.PCID is NULL'
set @sqlWhere_Cate=@sqlWhere_Cate+' ) B '
set @sqlWhere_Cate=@sqlWhere_Cate+' where A.w_P_PCID=B.PCID'
set @sqlWhere_Cate=@sqlWhere_Cate+' ) A,'
set @sqlWhere_Cate=@sqlWhere_Cate+' w_ProductCategorys B '
set @sqlWhere_Cate=@sqlWhere_Cate+' where A.w_P_PCID = b.PCID and A.PCID='
set @sqlWhere_Cate=@sqlWhere_Cate+ cast(@RootCode as nvarchar)
set @sqlWhere_Cate=@sqlWhere_Cate+' )C'
set @sqlWhere_Cate=@sqlWhere_Cate+' WHERE A.ProductCode=B.ProductCode '
set @sqlWhere_Cate=@sqlWhere_Cate+' AND B.PCID=C.w_P_PCID'
end
else begin
set @sqlWhere_Cate=''
end
end
--print(@sqlWhere_Cate)
--exec(@sqlWhere_Cate)
if(@IndexPage=1)begin
set @sqlFinal='SELECT * FROM (SELECT TOP '+cast(@PageSize as nvarchar)
set @sqlFinal=@sqlFinal+' ProductCode,ProductName,ForOrder,Storage,VPrice,Price,AddTime '
set @sqlFinal=@sqlFinal+' FROM ('+@sqlWhere_Cate+') D)A'
end
else begin
set @sqlFinal='SELECT * FROM (SELECT B.* FROM '
set @sqlFinal=@sqlFinal+' (SELECT TOP '+cast((@IndexPage-1)*@PageSize as nvarchar)
set @sqlFinal=@sqlFinal+' E.* FROM '
set @sqlFinal=@sqlFinal+' ('+@sqlWhere_Cate+') E) A'
set @sqlFinal=@sqlFinal+' RIGHT OUTER JOIN '
set @sqlFinal=@sqlFinal+' (SELECT TOP '+cast(@IndexPage*@PageSize as nvarchar)
set @sqlFinal=@sqlFinal+' F.* FROM '
set @sqlFinal=@sqlFinal+' ('+@sqlWhere_Cate+') F) B'
set @sqlFinal=@sqlFinal+' ON A.ProductCode=B.ProductCode WHERE A.ProductCode IS NULL) A'
end
set @sqlFinal=@sqlFinal+@sqlOrderStr
--print(@sqlFinal)
--获取记录总数
set @SQLstr='SELECT count(*) FROM ('+@sqlWhere_Cate+') A'
exec(@SQLstr)
exec(@sqlFinal)
exec('SELECT '''+@sqlFinal+'''')
end
set QUOTED_IDENTIFIER ON
go
--用途:获取产品列表
--添加时间:2008/06/02
--修改时间:2008/07/27
--添加人:李雪侠
--查询所有产品
CREATE PROCEDURE [dbo].[Admin_Search_w_Product]
@IndexPage int=1, --页码
@PageSize int=10, --页大小
@RecordCount int=0 output, --数据总数
@RootCode nvarchar(20)='', --对应w_productsandcategorys的ParentId
@DirCode nvarchar(20)='', --对应ProductCode直接对应的PCID
@ProductID nvarchar(20)='', --产品编号
@ProductName nvarchar(50)='', --产品名称
@ProductBrand nvarchar(50)='', --产品?
@OrderField nvarchar(20)='a.AddTime', --排序域
@isDesc int=1 --是否降序
AS
declare @sql nvarchar(4000),
@sqlWhere_Src nvarchar(1000),
@sqlSort nvarchar(100),
@sqlSortDesc nvarchar(100),
@sqlFiled nvarchar(200),
@sqlPcid nvarchar(20),
@sqlWhere_Cate nvarchar(1000),
@sqlFinal nvarchar(4000),
@Sqlstr nvarchar(4000),
@sqlOrderStr nvarchar(1000)
begin
set @sql='SELECT ProductCode,ProductName,ForOrder,Storage,VPrice,Price,AddTime ';
set @sql=@sql+'FROM Product '
--搜索条件:产品代码
if(@ProductID!='')begin
set @sqlWhere_Src='WHERE ProductCode LIKE '''+@ProductID+'%'''
end
--搜索条件:产品名称
if(@ProductName!='')begin
if(@sqlWhere_Src!='')begin
set @sqlWhere_Src=@sqlWhere_Src+' AND ProductName LIKE '''+@ProductName+'%'''
end
else begin
set @sqlWhere_Src='WHERE ProductName LIKE '''+@ProductName+'%'''
end
end
--搜索条件:产品品牌
if(@ProductBrand!='')begin
if(@sqlWhere_Src!='')begin
set @sqlWhere_Src=@sqlWhere_Src+' AND ProductBrand LIKE '''+@ProductBrand+'%'''
end
else begin
set @sqlWhere_Src='WHERE ProductBrand LIKE '''+@ProductBrand+'%'''
end
end
--排序条件
if(@isDesc=1)begin
set @sqlOrderStr=' ORDER BY '+@OrderField+' DESC'
end
else begin
set @sqlWhere_Src=' ORDER BY '+@OrderField+' ASC'
end
--print(@sql+@sqlWhere_Src)
if(@sqlWhere_Src!='')begin
set @sql=@sql+@sqlWhere_Src
end
--exec(@sql)
--类别处理
if(@DirCode!='')begin
set @sqlWhere_Cate='SELECT A.* FROM ('+@sql+') A,w_ProductsAndCategorys B '
set @sqlWhere_Cate=@sqlWhere_Cate+'WHERE A.ProductCode=B.ProductCode AND B.PCID='
set @sqlWhere_Cate=@sqlWhere_Cate+@DirCode
end
else begin
if(@RootCode!='')begin
set @sqlWhere_Cate='SELECT A.* FROM ('+@sql+') A,'
set @sqlWhere_Cate=@sqlWhere_Cate+' (select PCID,ProductCode '
set @sqlWhere_Cate=@sqlWhere_Cate+' from w_productsandcategorys) B,'
set @sqlWhere_Cate=@sqlWhere_Cate+' (select A.w_P_PCID,B.CategoryName from '
set @sqlWhere_Cate=@sqlWhere_Cate+' ('
set @sqlWhere_Cate=@sqlWhere_Cate+' select A.PCID,A.w_P_PCID from '
set @sqlWhere_Cate=@sqlWhere_Cate+' w_CateSelect A,'
set @sqlWhere_Cate=@sqlWhere_Cate+' ('
set @sqlWhere_Cate=@sqlWhere_Cate+' select A.PCID,A.CategoryName From '
set @sqlWhere_Cate=@sqlWhere_Cate+' w_ProductCategorys A'
set @sqlWhere_Cate=@sqlWhere_Cate+' Left outer Join w_ProductCategorys B '
set @sqlWhere_Cate=@sqlWhere_Cate+' on A.PCID=B.ParentID where B.PCID is NULL'
set @sqlWhere_Cate=@sqlWhere_Cate+' ) B '
set @sqlWhere_Cate=@sqlWhere_Cate+' where A.w_P_PCID=B.PCID'
set @sqlWhere_Cate=@sqlWhere_Cate+' ) A,'
set @sqlWhere_Cate=@sqlWhere_Cate+' w_ProductCategorys B '
set @sqlWhere_Cate=@sqlWhere_Cate+' where A.w_P_PCID = b.PCID and A.PCID='
set @sqlWhere_Cate=@sqlWhere_Cate+ cast(@RootCode as nvarchar)
set @sqlWhere_Cate=@sqlWhere_Cate+' )C'
set @sqlWhere_Cate=@sqlWhere_Cate+' WHERE A.ProductCode=B.ProductCode '
set @sqlWhere_Cate=@sqlWhere_Cate+' AND B.PCID=C.w_P_PCID'
end
else begin
set @sqlWhere_Cate=''
end
end
--print(@sqlWhere_Cate)
--exec(@sqlWhere_Cate)
if(@IndexPage=1)begin
set @sqlFinal='SELECT * FROM (SELECT TOP '+cast(@PageSize as nvarchar)
set @sqlFinal=@sqlFinal+' ProductCode,ProductName,ForOrder,Storage,VPrice,Price,AddTime '
set @sqlFinal=@sqlFinal+' FROM ('+@sqlWhere_Cate+') D)A'
end
else begin
set @sqlFinal='SELECT * FROM (SELECT B.* FROM '
set @sqlFinal=@sqlFinal+' (SELECT TOP '+cast((@IndexPage-1)*@PageSize as nvarchar)
set @sqlFinal=@sqlFinal+' E.* FROM '
set @sqlFinal=@sqlFinal+' ('+@sqlWhere_Cate+') E) A'
set @sqlFinal=@sqlFinal+' RIGHT OUTER JOIN '
set @sqlFinal=@sqlFinal+' (SELECT TOP '+cast(@IndexPage*@PageSize as nvarchar)
set @sqlFinal=@sqlFinal+' F.* FROM '
set @sqlFinal=@sqlFinal+' ('+@sqlWhere_Cate+') F) B'
set @sqlFinal=@sqlFinal+' ON A.ProductCode=B.ProductCode WHERE A.ProductCode IS NULL) A'
end
set @sqlFinal=@sqlFinal+@sqlOrderStr
--print(@sqlFinal)
--获取记录总数
set @SQLstr='SELECT count(*) FROM ('+@sqlWhere_Cate+') A'
exec(@SQLstr)
exec(@sqlFinal)
exec('SELECT '''+@sqlFinal+'''')
end