DB2_自治事務
目的:
測試DB2自治事務,本指令碼摘錄自DB2安裝目錄admin_scripts/autonomous_transaction.db2。
版本:Windows DB2 Express-C V9.7
使用者要求:
作業系統使用者mh有DBADM,SECADM許可權,mh1234;作業系統使用者bob,密碼bob1234;作業系統使用者pat,密碼pat1234。
說明:
1,為了在CLP互動模式下支援多行輸入,可以使用"db2cmd db2 -td@"進入互動模式。
2,由於Express-C V9.7版本不支援DBMS_OUTPUT,呼叫時報錯,沒有測試report_generate()過程:
db2 => set serveroutput on;
SQL8004N 找不到所請求功能的有效許可證金鑰。
操作步驟:
1,設定測試環境
2,測試
3,清理測試環境
1,設定測試環境
CONNECT TO sample user mh using mh1234@
CREATE TABLE temp_employee(
empno CHAR(6),
empname VARCHAR(10),
lastname VARCHAR(10),
workdept CHAR(3),
bonus DECIMAL(9,2),
hiredate DATE)@
CREATE TABLE temp_payroll(
empno CHAR(6),
salary DECIMAL(9,2))@
CREATE TABLE event_log(
user_name VARCHAR(10),
event VARCHAR(65),
event_time TIME,
event_date DATE)@
-- Insert data into temp_employee table from employee table
INSERT INTO temp_employee VALUES
('000010', 'CHRISTINE', 'HAAS', 'A00', 1000.00, '01/01/1995')@
INSERT INTO temp_employee VALUES
('000020', 'MICHAEL', 'THOMPSON', 'B01', 800.00, '10/10/2003')@
INSERT INTO temp_employee VALUES
('000030', 'SALLY', 'KWAN', 'C01', 800.00, '04/05/2005')@
INSERT INTO temp_employee VALUES
('000050', 'JACK', 'GEYER', 'E01', 800.00, '08/17/1979')@
INSERT INTO temp_employee VALUES
('000060', 'IRVING', 'STERN', 'D11', 500.00, '09/14/2003')@
INSERT INTO temp_employee VALUES
('000070', 'EVA', 'PULASKI', 'D21', 700.00, '09/30/2005')@
INSERT INTO temp_employee VALUES
('000090', 'EILEEN', 'HENDERSON', 'E11', 600.00, '08/15/2000')@
INSERT INTO temp_employee VALUES
('000100', 'THEODORE', 'SPENSER', 'E21', 500.00, '06/19/2000')@
INSERT INTO temp_employee VALUES
('000110', 'VINCENZO', 'LUCCHESSI', 'A00', 900.00, '05/16/1988')@
-- Insert data into temp_payroll table
INSERT INTO temp_payroll VALUES
('000010', 10000.500)@
INSERT INTO temp_payroll VALUES
('000020', 12000.430)@
INSERT INTO temp_payroll VALUES
('000030', 11600.600)@
INSERT INTO temp_payroll VALUES
('000050', 10560.450)@
INSERT INTO temp_payroll VALUES
('000060', 13000.500)@
INSERT INTO temp_payroll VALUES
('000070', 11640.600)@
INSERT INTO temp_payroll VALUES
('000090', 12560.450)@
INSERT INTO temp_payroll VALUES
('000100', 13894.556)@
commit@
-- Fetch data from temp_employee
SELECT * FROM temp_employee@
-- Fetch data from temp_payroll
SELECT * FROM temp_payroll@
CREATE PROCEDURE event_log(IN event CHAR(1))
AUTONOMOUS
LANGUAGE SQL
BEGIN
CASE event
WHEN 'U'
THEN INSERT INTO event_log
VALUES(SESSION_USER,
'CALLING salary_update PROCEDURE TO UPDATE THE SALARY',
CURRENT TIME,
CURRENT DATE);
WHEN 'S'
THEN INSERT INTO event_log
VALUES(SESSION_USER,
'CALLING report_generate PROCEDURE TO VIEW EMPLOYEES SALARY',
CURRENT TIME,
CURRENT DATE);
END CASE;
END@
CREATE PROCEDURE update_salary
(IN exp INTEGER,
IN workdpt CHAR(3),
IN new_salary INTEGER)
LANGUAGE SQL
BEGIN
CALL event_log('U');
UPDATE temp_payroll
SET salary = salary + new_salary
WHERE empno =
(SELECT empno FROM temp_employee WHERE workdept = workdpt
AND (CURRENT DATE - hiredate) > exp);
IF (USER <> 'BOB')
THEN
ROLLBACK;
END IF;
END@
-- Grant execute privilege to user BOB on procedure update_salary
GRANT EXECUTE ON PROCEDURE update_salary TO USER bob@
-- Grant execute privilege to user PAT on procedure update_salary
GRANT EXECUTE ON PROCEDURE update_salary TO USER pat@
2,測試:
2.1初始資料
CONNECT RESET@
CONNECT TO sample USER mh USING mh1234@
SELECT salary FROM temp_payroll
WHERE empno = (SELECT empno FROM temp_employee
WHERE workdept = 'D11'
AND (CURRENT DATE - hiredate) > 5)@
SALARY
-----------
13000.50
1 條記錄已選擇。
2.2 BOB呼叫更新操作
CONNECT RESET@
CONNECT TO sample user bob using bob1234@
CALL mh.update_salary(5, 'D11', 2000)@
CONNECT RESET@
CONNECT TO sample user mh using mh1234@
SELECT salary FROM temp_payroll
WHERE empno = (SELECT empno FROM temp_employee
WHERE workdept = 'D11'
AND (CURRENT DATE - hiredate) > 5)@
SALARY
-----------
15000.50
1 條記錄已選擇。
2.3 PAT呼叫更新操作
CONNECT RESET@
CONNECT TO sample user pat using pat1234@
CALL mh.update_salary(5, 'D11', 2000)@
CONNECT RESET@
CONNECT TO sample user mh using mh1234@
SELECT salary FROM temp_payroll
WHERE empno = (SELECT empno FROM temp_employee
WHERE workdept = 'D11'
AND (CURRENT DATE - hiredate) > 5)@
SALARY
-----------
15000.50
1 條記錄已選擇。
select * from event_log@
USER_NAME EVENT EVENT_TIME EVENT_DATE
---------- ----------------------------------------------------- ---------- ----------
BOB CALLING salary_update PROCEDURE TO UPDATE THE SALARY 09:48:05 2011-07-13
PAT CALLING salary_update PROCEDURE TO UPDATE THE SALARY 09:52:41 2011-07-13
2 條記錄已選擇。
3,清理測試環境
CONNECT RESET@
CONNECT TO sample USER mh USING mh1234@
DROP TABLE temp_employee@
DROP TABLE temp_payroll@
DROP TABLE event_log@
DROP PROCEDURE update_salary@
DROP PROCEDURE event_log@
CONNECT RESET@
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18922393/viewspace-702049/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle 自治事務Oracle
- 瞭解Oracle自治事務Oracle
- PLSQL Language Referenc-PL/SQL靜態SQL-自治事務-控制自治事務SQL
- Oracle 巢狀事務 VS 自治事務Oracle巢狀
- Oracle自治事務autonomous_transaction用法Oracle
- oracle自治事務(PRAGMA AUTONOMOUS_TRANSACTION)Oracle
- oracle自治事務引起的死鎖Oracle
- Oracle中的自治事務(Autonomous Transaction)Oracle
- Oracle PL/SQL 自治事務的說明OracleSQL
- PostgreSQL DBA(68) - 使用DBLink實現自治事務SQL
- 【DDL】DDL的隱式commit和自治事務MIT
- PLSQL Language Referenc-PL/SQL靜態SQL-自治事務(二)SQL
- DB2_安全DB2
- 使用自治事務在觸發器中執行DDL語句示例觸發器
- 在Oracle中使用自治事務儲存日誌表條目(轉)Oracle
- DB2_建庫DB2
- DB2_審計DB2
- DB2_命令列工具DB2命令列
- DB2_使用別名DB2
- DB2_行壓縮DB2
- DB2_建立備份恢復使用自動儲存的資料庫DB2資料庫
- DB2_簡單命令列DB2命令列
- DB2_更新SQL欄位DB2SQL
- db2_查詢鎖方法DB2
- DB2_資料庫角色DB2資料庫
- DB2_全域性變數DB2變數
- DB2_狀態監視DB2
- DB2_建立重組索引DB2索引
- DB2_獲取健康設定DB2
- DB2_使用大表空間DB2
- DB2_線上裝載資料DB2
- DB2_收縮表空間DB2
- DB2_獲取診斷日誌DB2
- DB2_使用事件監視器DB2事件
- DB2_收集表統計資料DB2
- DB2_獲取系統引數狀態DB2
- DB2_多維叢集表索引延遲清理DB2索引
- DB2_基於標籤的訪問控制LBACDB2