做購物車系統(tǒng)時利用到得幾個sqlserver 存儲過程
更新時間:2009年12月15日 23:04:26 作者:
最近使用asp.net+sql2000開始開發(fā)一個小型商城系統(tǒng),其中涉及到得購物車功能主要是仿照淘寶實現(xiàn)的.
即以游客身份登錄網(wǎng)站時以cookie的方式存儲購物車,而以登錄用戶的身份進(jìn)入時將購物車信息存儲到數(shù)據(jù)庫中去,若是先以游客身份完成購物再登錄繼續(xù)購物,則將cookies購物車存入數(shù)據(jù)庫;
其中涉及到的存儲過程主要如下:
一:已登錄會員添加商品到購物車功能:
/* @store_sum表示要添加的商品數(shù)量,添加同時確認(rèn)購物車中自己已有的數(shù)量與將要加入的數(shù)量之和是否超過庫存 */
CREATE proc ncp_Cart_Add
(
@store_id int,
@store_sum int=1,
@member_id int
)
as
DECLARE @Amount int
DECLARE @NowAmount int
Begin
select @Amount=(select amount from ncp_store where id=@store_id)
IF EXISTS(SELECT 1 FROM [ncp_cart] WHERE store_id=@store_id and member_id=@member_id)
Begin
select @NowAmount=(select store_sum+@store_sum from ncp_cart WHERE store_id=@store_id and member_id=@member_id)
if @NowAmount>@Amount
return 0
else
UPDATE [ncp_cart] SET store_sum=store_sum+@store_sum,addtime=getDate() where store_id=@store_id and member_id=@member_id
return 1
End
ELSE
Begin
select @NowAmount=(select store_sum from ncp_cart WHERE store_id=@store_id and member_id=@member_id)
if @NowAmount>@Amount
return 0
else
INSERT INTO [ncp_cart](store_id,store_sum,member_id) values(@store_id,@store_sum,@member_id)
return 1
END
End
GO
二:購物車的刪除功能
/* type 為1是全部刪 0時只刪一個 */
CREATE PROCEDURE ncp_Cart_Del
@type int=0,
@store_id int ,
@member_id int
AS
begin
if @type=0
delete from [ncp_cart] where store_id=@store_id and member_id=@member_id
else
delete from [ncp_cart] where member_id=@member_id
End
GO
其中涉及到的存儲過程主要如下:
一:已登錄會員添加商品到購物車功能:
復(fù)制代碼 代碼如下:
/* @store_sum表示要添加的商品數(shù)量,添加同時確認(rèn)購物車中自己已有的數(shù)量與將要加入的數(shù)量之和是否超過庫存 */
CREATE proc ncp_Cart_Add
(
@store_id int,
@store_sum int=1,
@member_id int
)
as
DECLARE @Amount int
DECLARE @NowAmount int
Begin
select @Amount=(select amount from ncp_store where id=@store_id)
IF EXISTS(SELECT 1 FROM [ncp_cart] WHERE store_id=@store_id and member_id=@member_id)
Begin
select @NowAmount=(select store_sum+@store_sum from ncp_cart WHERE store_id=@store_id and member_id=@member_id)
if @NowAmount>@Amount
return 0
else
UPDATE [ncp_cart] SET store_sum=store_sum+@store_sum,addtime=getDate() where store_id=@store_id and member_id=@member_id
return 1
End
ELSE
Begin
select @NowAmount=(select store_sum from ncp_cart WHERE store_id=@store_id and member_id=@member_id)
if @NowAmount>@Amount
return 0
else
INSERT INTO [ncp_cart](store_id,store_sum,member_id) values(@store_id,@store_sum,@member_id)
return 1
END
End
GO
二:購物車的刪除功能
復(fù)制代碼 代碼如下:
/* type 為1是全部刪 0時只刪一個 */
CREATE PROCEDURE ncp_Cart_Del
@type int=0,
@store_id int ,
@member_id int
AS
begin
if @type=0
delete from [ncp_cart] where store_id=@store_id and member_id=@member_id
else
delete from [ncp_cart] where member_id=@member_id
End
GO
相關(guān)文章
SQL Server數(shù)據(jù)庫開發(fā)的二十一條法則
如果你正在負(fù)責(zé)一個基于SQL Server的項目,或者你剛剛接觸SQL Server,你都有可能要面臨一些數(shù)據(jù)庫性能的問題,這篇文章會為你提供一些有用的指導(dǎo)(其中大多數(shù)也可以用于其它的DBMS)。2010-06-06
SQL語句練習(xí)實例之一——找出最近的兩次晉升日期與工資額
程序員們在編寫一個雇員報表,他們需要得到每個雇員當(dāng)前及歷史工資狀態(tài)的信息,以便生成報表。報表需要顯示每個人的晉升日期和工資數(shù)目。2011-10-10
解析關(guān)于SQL語句Count的一點細(xì)節(jié)
本篇文章是對關(guān)于SQL語句Count的一點細(xì)節(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
只有mdf文件的數(shù)據(jù)庫附加失敗的修復(fù)方法分享(置疑、只讀)
有時候因為某些原因數(shù)據(jù)庫只有mdf文件了,需要附加數(shù)據(jù)庫,會出現(xiàn)一些問題,大家可以參考下面的方法試試2012-02-02
總結(jié)SQL執(zhí)行進(jìn)展優(yōu)化方法
談到優(yōu)化就必然要涉及索引,就像要講鎖必然要說事務(wù)一樣,建議讀者先了解一下索引。2015-08-08

