在ORACLE中,他以一个语句块为一个默认的事务。也就是说,如果你就单单只执行一段ORACLE的语句块,他默认是以事务的形式执行的。
01 CREATE OR REPLACE PROCEDURE sp_EditInlayOut(
02 FID NUMBER, --修改记录的ID T_INLAYOUT表的主键
03 InlayBoxIDs varchar2, --修改的记录
04 BoxCount number, --装箱数量
05 ApplyUserID varchar2, --申请人编号
06 StoreUserID varchar2, --库管编号
07 ConfirmState char, --确认状态
08 ExistState char, --存在状态
09 strErr OUT varchar2 --存储过程执行结果。成功返回空,失败返回错误原因
10 )
11 AS
12 --定义变量
13 v_Now DATE;
14 v_Now2 date;
15 v_LogID number;
16 v_ChipID number;
17 v_sql varchar2(2000);
18 BEGIN
19
20 --记录日志
21 INSERT INTO T_InlayOut_Log(F_InlayBoxIDs,f_Boxcount,f_Applyuserid,f_Storeuserid,f_Addtime,f_Confirmstate
22 ,f_Existstate, f_modifyid, f_modifytime, f_modifyuserid )
23 ((SELECT F_InlayBoxIDs,f_Boxcount,f_Applyuserid,f_Storeuserid,f_Addtime,f_Confirmstate,f_Existstate
24 ,FID,SYSDATE,StoreUserID FROM T_InlayOut WHERE F_ID=FID));
25 --取刚插入记录的ID
26 select seq_t_inlayout_log.currval into v_LogID from dual;
27 --定义游标
28 DECLARE CURSOR myCusor IS SELECT F_ID FROM T_CHIP WHERE F_InlayBoxID IN (SELECT f_ID FROM
29 T_InlayBox where F_InlayOutID = FID);
30 --开始使用游标取数据
31 BEGIN
32 OPEN myCusor;
33
34 LOOP
35 FETCH myCusor INTO v_ChipID;
36 --游标取不到数据则退出
37 EXIT WHEN myCusor%NOTFOUND;
38
39 SELECT MIN(F_CurrentTime) INTO v_Now FROM t_Chipstatehistory WHERE
40 (F_HistoryState = 'Confirm_InlayIn') AND F_ChipID = v_ChipID;
41 --改变芯片表的状态
42 UPDATEt_chip SET f_State = 'Confirm_InlayIn',F_CompareTime = v_Now WHERE F_ID = v_ChipID;
43 --保存芯片状态历史记录
44 INSERT INTO T_CHIPSTATEHISTORY(f_chipid, f_Historystate,F_TABLEID,f_Currenttime,F_TABLENAME)
45 VALUES
46 (v_ChipID,'Confirm_InlayIn',v_LogID,SYSDATE,'T_InlayOut_Log');
47
48 END LOOP;
49 CLOSE myCusor;
50 END;
51
52 --选择最近芯片状态变更时间
53 --SELECT MIN(F_CURRENTTIME) INTO v_NOW FROM T_CHIPSTATEHISTORY WHERE F_HISTORYSTATE = 20
54 AND F_CHIPID IN (SELECT F_ID FROM T_CHIP WHERE F_InlayBoxID=(SELECT F_ID FROM T_InlayBox
55 WHERE F_InlayOutID=FID));
56
57 --将芯片表中芯片状态更新到以前状态
58 --UPDATE T_CHIP SET F_State=20,F_CompareTime=v_NOW WHERE F_InlayBoxID IN (SELECT F_ID FROM
59 T_InlayBox WHERE F_InlayOutID =FID);
60 --记录芯片状态变更日志
61 --INSERT INTO T_ChipStateHistory (F_ChipID,f_Historystate,f_Tableid,f_Currenttime,f_Tablename)VALUES
62 --((SELECT F_ID FROM T_CHIP WHERE F_InlayBoxID=(SELECT F_ID FROM T_InlayBox WHERE F_InlayOutID=FID)),
63 20,v_LogID,SYSDATE,'T_InlayOut_Log');
64
65
66 --将Inlay出库箱表中以前的数据更新到以前状态
67 UPDATE T_InlayBox SET F_State=2,F_InlayOutID=null WHERE F_InlayOutID =FID;
68
69 --编辑时将新的INLAY出库信息更新
70 UPDATE T_InlayOut SET F_InlayBoxIDs=InlayBoxIDs,f_Boxcount=BoxCount,f_Applyuserid=ApplyUserID,
71 f_Storeuserid=StoreUserID,f_Confirmstate=ConfirmState,F_ExistState=ExistState,F_ConfirmTime=null
72 WHERE F_ID=FID;
73
74 --更新T_InlayBox 新的状态
75 --UPDATE T_InlayBox SET F_State=3,F_InlayOutID=FID WHERE F_ID in (InlayBoxIDs);
76 v_sql := 'UPDATE T_InlayBox SET F_State=3,F_InlayOutID='||FID||' WHERE F_ID in ('||InlayBoxIDs||')';
77 --立即执行v_sql
78 EXECUTE IMMEDIATE v_sql;
79
80 SELECT SYSDATE INTO v_Now2 FROM DUAL;
81 --更新芯片表状态
82 UPDATE T_Chip SET F_State='No_Confirm_InlayOut',F_CompareTime=v_Now2 WHERE F_InlayBoxID IN
83 (SELECT F_ID FROM T_InlayBox WHERE F_InlayOutID=FID);
84 --记录当前操作日志
85 INSERT INTO T_ChipStateHistory (F_ChipID,f_Historystate,f_Tableid,f_Currenttime,f_Tablename)
86 SELECT F_ID,'No_Confirm_InlayOut',v_LogID,v_Now2,'T_InlayOut_Log' FROM T_CHIP WHERE F_InlayBoxID IN
87 (SELECT F_ID FROM T_InlayBox WHERE F_InlayOutID=FID);
88 --提交
89 COMMIT;
90 --发生异常时返回错误码
91 EXCEPTION
92 WHEN OTHERS THEN
93 strErr := substr(sqlerrm,1,100);
94 ROLLBACK;
95 END sp_EditInlayOut;
但是在SQLSERVER中,除非你将所有的T-SQL语句块以显示的方式【BEGIN TRANSACTION ....END TRANSACTION】申明在事务中,否则SQLSERVER会将语句块中的每一句作为一个单独的默认事务执行。
此外,游标是一种比较占I/O资源的操作,使用完后应该及时关闭,以释放系统资源。