创建商品目录:之数据层
先看看数据库结构吧。
存储过程:
GetDepartmentDetails:
Code
Create PROCEDURE GetDepartmentDetails
(@DepartmentID int)
AS
Select Name,Description
from Department
Where DepartmentID=@DepartmentID
这里定义一个参数@Department,意思是执行存储过程时传入一个参数,在SQL语句中查询符合Where DepartmentID=@DepartmentID条件的门类信息。
同理GetCategoryDetails,GetProductDetails,GetCategoriesIndepartment都是相同,就不说了。
下面看几个比较复杂点的:
GetProductsOnCatalogPronotion:
Code
ALTER PROCEDURE GetProductsOnCatalogPromotion
(@DescriptionLength INT,
@PageNumber int,
@ProductsPerPage int,
@HowManyProducts int output)
AS
--Declare a new table variable
Declare @Products Table
(RowNumber int,
ProductID int,
Name varchar(50),
Description varchar(5000),
Price money,
Image1FileName varchar(50),
Image2FileName varchar(50),
OnDepartmentPromotion bit,
OnCatalogPromotion bit)
Insert into @Products
Select ROW_NUMBER() Over (Order by Product.ProductID),
ProductID,Name,
SUBSTRING(Description,1,@DescriptionLength)+'' As Description,Price,
Image1FileName,Image2FileName,OnDepartmentPromotion,OnCatalogPromotion
from Product
Where OnCatalogPromotion=1
Select @HowManyProducts=Count(ProductID) from @Products
select ProductID,Name,Description,Price,Image1FileName,
Image2FileName,OnDepartmentPromotion,OnCatalogPromotion
from @Products
where RowNumber>(@PageNumber-1)*@ProductsPerPage
And RowNumber<=@PageNumber*@ProductsPerPage
解释:先定义几个变量,然后定义一个临时表@Products里面列与数据库中的Product表相同,然后填充这个表。这里我们要看看Sql2005的分页技术。
对于SQL2000而言,最主要的问题是结果集总是被认为是一个整体,其中的单行并没有以任何方式进行编号。其结果是,它们没有只用直观的方法表示“我想要商品列表中的地6~10条记录”,因为数据库实际上并不知道这些记录在哪。
Sql2005中有一个Row_Number函数,它针对Select语句返回的每一行,从一开始编号,赋予其连续编号。因为在查询上应用了一个排序标准后,只有通过编号才能保证其顺序是一致的。
如上面:Select ROW_NUMBER() Over (Order by Product.ProductID)。
然后获取表中商品数目,最后实现查询,记住是从临时表中进行查询的。
看Where子句中 编号>(当前页数-1)*每页数目且 编号<=当前页数*每页数目
比如每页5个,第二页就是6~10
Where 编号>(2-1)*5(6) and 编号<=2*5(10)
出来了,很有意思!
GetProductsInCategory:
Code
Create PROCEDURE GetProductsInCategory
(@CategoryID int,
@DescriptionLength int,
@PageNumber int,
@ProductsPerPage int,
@HowManyProducts int output)
AS
declare @Products table
(RowNumber int,
ProductID int,
Name varchar(50),
Description varchar(5000),
Price money,
Image1FileName varchar(50),
Image2FileName varchar(50),
OnDepartmentPromotion bit,
OnCatalogPromotion bit)
Insert into @Products
Select ROW_NUMBER() Over (Order by Product.ProductID),
Product.ProductID, Name,
SUBSTRING( Description, 1, @DescriptionLength ) + '' As Description, Price,
Image1FileName, Image2FileName, OnDepartmentPromotion, OnCatalogPromotion
from Product inner join ProductCategory
on Product.ProductID = ProductCategory.ProductID
where ProductCategory.CategoryID=@CategoryID
Select @HowManyProducts=Count(ProductID) from @Products
select ProductID,Name,Description,Price,Image1FileName,
Image2FileName,OnDepartmentPromotion,OnCatalogPromotion
from @Products
where RowNumber>(@PageNumber-1)*@ProductsPerPage
And RowNumber<=@PageNumber*@ProductsPerPage
这里与上个存储过程不一样的就是多了一个inner join
搜搜数据库联结的知识,看的有点晕。自己好好体会吧!
GetProductsOnDepartmentPromotion:
Code
ALTER PROCEDURE GetProductsOnDepartmentPromotion
(@DepartmentID int,
@DescriptionLength int,
@PageNumber int,
@ProductsPerPage int,
@HowManyProducts int output)
AS
Declare @Products Table
(RowNumber int,
ProductID int,
Name varchar(50),
Description varchar(5000),
Price money,
Image1FileName varchar(50),
Image2FileName varchar(50),
OnDepartmentPromotion bit,
OnCatalogPromotion bit)
Insert into @Products
Select Row_Number() Over (Order by ProductID ) As Row,
ProductID, Name, SUBSTRING(Description, 1, @DescriptionLength)
+ '' As Description,
Price, Image1FileName, Image2FileName, OnDepartmentPromotion,
OnCatalogPromotion
From
( Select Distinct Product.ProductID, Product.Name,
SUBSTRING(Product.Description,1,@DescriptionLength)+ '' As Description,
Price,Image1FileName,Image2FileName,OnDepartmentPromotion,OnCatalogPromotion
from Product inner join ProductCategory
on Product.ProductID=ProductCategory.ProductID
inner join Category
on ProductCategory.CategoryID=Category.CategoryID
Where Product.OnDepartmentPromotion=1
And Category.DepartmentID=@DepartmentID
)AS ProductOnDepr
Select @HowManyProducts=Count(ProductID) from @Products
select ProductID,Name,Description,Price,Image1FileName,Image2FileName,OnDepartmentPromotion,OnCatalogPromotion
From @Products
where RowNumber > (@PageNumber-1)*@ProductsPerPage
And RowNumber <= @PageNumber*@ProductsPerPage
这个就更复杂一点,就是填充临时表的时候复杂点。其他也就没有什么新的东西!
好了,存储过程就这么多了。总结一下新学到的东西有分页,写复杂点的存储过程,还有数据库里面的联接,以前学习的时候看理论可能记不住,拿个实例看看不错!影响深刻!
东西比较多。下次说明业务层的代码!