MasteringVFP/4/9

出自VFP Wiki

在2006年6月2日 (五) 00:31由1149210855 (對話 | 貢獻)所做的修訂版本
跳轉到: 導航, 搜尋

個人是覺得 Trigger 很好用, 但是在不同的資料庫會有不同的語法,資料庫轉移上也會有問題, 而且也不是所有的資料庫都有 Trigger 功能, 例如 MySQL 就沒有(最新版有沒有不清楚, 有興趣至 MySQL 網站)。不少書藉不建議使用 Trigger, 不過如果只是小程式, 倒是可以寫寫看。 如果單純以 VFP 來說, Trigger 必須加進 DBC 內才有這功能。 一樣來舉個例子:

CREATE DATABASE 測試
CREATE TABLE 庫存 (No C(10),Qty I)
CREATE TABLE 進貨 (No C(10),Qty I)
CREATE TABLE 出貨 (No C(10),Qty I)

上述是建立一個 [測試.dbc]、[庫存.DBF]、[進貨.DBF]、[出貨.DBF], 接下來在 [測試.dbc] 內的 Store Procedure 輸入以下的 Code

* --------------------------------------------------
* 以下程式碼不考慮錯誤處理問題
* --------------------------------------------------
* 進貨時的 Insert Trigger
* --------------------------------------------------
FUNCTION 進貨_insert()
	SELECT 庫存
	LOCATE FOR No=進貨.No
	IF !FOUND()
		INSERT INTO 庫存 (No,Qty) VALUES (進貨.No,進貨.Qty)
	ELSE
		Replace Qty WITH 庫存.Qty+進貨.Qty IN 庫存
	ENDIF
	SELECT 進貨
ENDFUNC
* --------------------------------------------------
* 出貨時的 Insert Trigger
* --------------------------------------------------
FUNCTION 出貨_insert()
	SELECT 庫存
	LOCATE FOR No=出貨.No
	IF FOUND()
		Replace Qty WITH 庫存.Qty-出貨.Qty IN 庫存
	ENDIF
	SELECT 出貨
ENDFUNC

將 Store Procedure 存檔後在下一些命令

* -- 在進貨檔建立 Insert Trigger
CREATE TRIGGER ON 進貨 FOR INSERT as 進貨_insert()


* -- 在出貨檔建立 Insert Trigger
CREATE TRIGGER ON 出貨 FOR INSERT as 出貨_insert()

* -- 新增進貨檔一筆記錄: 產品代號為 A12345, 數量 10
INSERT INTO 進貨 (No,Qty) VALUES ("A12345",10)

* -- 新增出貨檔一筆記錄: 產品代號為 A12345, 數量 3
INSERT INTO 出貨 (No,Qty) VALUES ("A12345",3)

* -- 查看庫存檔
Select 庫存
Brow

您可以在庫存檔內看到 A12345 的數量剩 7

Trigger 的用途就是只要有 Insert、Update、Delete 的 Trigger Function, 針對這個資料做增刪修的命令即觸發這些 Function 如果要在 Trigger 處理不符合 Rule 的動作時, 下 Return .F., 這樣子配合 Tableupdate 時會回應 False











[We are delicate. We do not delete your content.] [l_sp23]


Formal Prom Dresses Sexy Prom Dress cocktail dresses flyfone voip Hooters Casino Las Vegas grand casino mille lacs las vegas casino coupons online poker aide pechanga casino grand victoria casino ball gowns rtg casino bonus

rtg casino bonus grand casino coushatta grand casino hinckley isle of capri casino mohegan sun casino pala casino roulette wheels winstar casino Cheap Prom Dresses informal wedding dresses oscar dresses evening gowns

throat pokers online poker assistant online poker tracker online poker aide party poker bonus party poker bonus code bonus code party poker bonus code deposit party poker party poker bonus codes carnival cruises carnival game rentals fuzzy dice bingo daubers bingo blowers motor scooters mini harley chopper scooter fake rolex

bextra vioxx zocor zithromax bextra lawyer new hampshire akane soma kyo soma prozac nation prozac withdrawal sim slots pachislo slot machines program cherries wild slot machine slot car racing ho slot cars

bingo cages bingo daubers bingo blowers bingo bags baccarat crystal nexium online pai gow poker leigh keno

tramadol 50mg tramadol saturday delivery tramadol er tramadol sales tramadol hcl 50 mg 120 tramadol acetaminophen hcl par tramadol plus size mother of the bride dresses cheap flower girl dresses discount flower girl dresses infant flower girl dresses bridal flower girl dresses

discount bridesmaid dresses junior bridesmaid dresses cheap bridesmaid dresses plus size bridesmaid dresses wedding bridesmaid dresses maternity bridesmaid dresses

Rascal Flatts Me And My Gang Ne-Yo In My Own Words Kelly Clarkson Breakaway

biaxin biaxin xl atenolol side effects flexeril diflucan glucophage tenuate dospan zanaflex

acyclovir zovirax cyclobenzaprine hcl clonazepam side effects testosterone cream celebrex vs vioxx infant flower girl dresses junior bridesmaid dresses