划重点:
显示游标对应于一个返回结果为多行多列的SELECT语句。
应用场景:
在判断节点配置页面实现删除功能,例如删除执行次序=3的数据,那么就需要同步更新删除前次序为4和5的执行次序。
实现过程:
1.前台:传递将要删除的数据的执行次序
1 delJudge: function () { 2 var that = modObj; 3 var indexNo = []; 4 var rows = that.uiStore.gvJudgeList.getCheckRows(); 5 if (rows.length == 0) { 6 alert("至少选择一条数据!"); 7 return false; 8 } 9 for (var i = 0; i < rows.length; i++) { 10 console.log(rows[i]); 11 indexNo.push(rows[i].INDEX_NO); 12 } 13 var param = { 14 reqType: "delJudgeByNode", 15 nodeID: that.dataStore.nodeID, 16 modelID: that.dataStore.modelID, 17 indexNo: indexNo.join(that.splitChar.splitStr1st)//被选中的多行数据的执行次序拼接成的字符串 18 } 19 that.ajaxDeal(that.dataStore.ajaxUrl, param, function (result) { 20 alert("删除成功"); 21 //重新加载数据 22 that.loadJudgeList(); 23 }); 24 }
2.AJAX层:
1 /// <summary> 2 /// 删除当前节点的判断检查配置情况 3 /// </summary> 4 /// <returns></returns> 5 private string delJudgeByNode() 6 { 7 var nodeID = GetQueryString("nodeID"); 8 var modelID = GetQueryString("modelID"); 9 var indexNo = GetQueryString("indexNo"); 10 11 Dictionary<string, string> param = new Dictionary<string, string>(); 12 param.Add("modelID", modelID); 13 param.Add("nodeID", nodeID); 14 15 FlowConfigService.GetService().delJudgeByNode(param, indexNo); 16 return new 17 { 18 state = "ok", 19 }.ToJson(); 20 }
3.在Service层拆分执行次序字符串,循环取出需要删除的执行次序
1 /// <summary> 2 /// 删除当前节点的判断检查配置情况 3 /// </summary> 4 /// <param name="param"></param> 5 /// <param name="indexNo"></param> 6 public void delJudgeByNode(Dictionary<string, string> param, string indexNo) 7 { 8 var indexNos = indexNo.Split(PublicValue.FirstSplitString); 9 foreach (var item in indexNos) 10 { 11 _flowConfigDao.delJudgeByNode(param, item); 12 } 13 }
4.接口层:
1 /// <summary> 2 /// 删除当前节点的判断检查配置情况 3 /// </summary> 4 /// <param name="param"></param> 5 /// <param name="indexNo"></param> 6 void delJudgeByNode(Dictionary<string, string> param, string indexNo);
5.Dao层:
1 /// <summary> 2 /// 删除当前节点的判断检查配置情况 3 /// </summary> 4 /// <param name="param"></param> 5 /// <param name="indexNo"></param> 6 public void delJudgeByNode(Dictionary<string, string> param, string indexNo) 7 { 8 BLLService.ExecuteNonQuery( 9 "PKG_TEST.JUDGE_LIST_DEL", 10 "P_WF_M_ID", param["modelID"], 11 "P_WF_M_NODE_ID", param["nodeID"], 12 "P_INDEX_NO", indexNo 13 ); 14 }
哎呀,重点终于来了。。。
看看怎么在存储过程里通过游标循环操作每一行数据
/*
功能描述:删除判断节点配置情况
创建人:mengmeng.chen 2018-11-22
*/
PROCEDURE JUDGE_LIST_DEL
(
P_WF_M_ID IN TWF_MODEL_NODE_JUDGE.WF_M_ID%TYPE,
P_WF_M_NODE_ID IN TWF_MODEL_NODE_JUDGE.WF_M_NODE_ID%TYPE,
P_INDEX_NO IN TWF_MODEL_NODE_JUDGE.INDEX_NO%TYPE --将要删除的数据的执行次序
) AS
V_COUNT INTEGER;
V_INDEX_NO INTEGER; --游标参数
1.声明显示游标
--声明格式: CURSOR 游标名[(参数1 数据类型[,参数2 数据类型...])] IS SELECT语句;
--参数是可选部分,所定义的参数可以出现在SELECT语句的WHERE子句中。
--如果定义了参数,则必须在打开游标时传递相应的实际参数。
CURSOR C_JUDGE (P_C_INDEX_NO IN INTEGER) IS
SELECT * FROM TWF_MODEL_NODE_JUDGE
WHERE INDEX_NO > P_C_INDEX_NO 12 ORDER BY INDEX_NO;
--将一行数据取到记录变量中,需要使用%ROWTYPE事先定义记录变量;
C_JUDE_ROW C_JUDGE%ROWTYPE; --定义游标变量
BEGIN
--判断该模板是否在使用中,若在使用中,不能删除
SELECT COUNT(1)
INTO V_COUNT
FROM TWF_INSTANCE
WHERE WF_M_ID = P_WF_M_ID;
IF V_COUNT = 0 THEN
--先删除检查配置
DELETE FROM TWF_MODEL_NODE_JUDGE
WHERE INDEX_NO = P_INDEX_NO AND
WF_M_ID = P_WF_M_ID AND
WF_M_NODE_ID = P_WF_M_NODE_ID;
COMMIT;
--更新删除后的数据的执行次序
V_INDEX_NO := P_INDEX_NO ; --给游标参数赋值
2.打开游标,SELECT语句的查询结果被传送到了游标工作区
OPEN C_JUDGE(V_INDEX_NO);
3.提取数据,将游标工作区中的数据取到变量中,用循环控制一行一行提取,并更新执行次序
LOOP
FETCH C_JUDGE INTO C_JUDE_ROW;
EXIT WHEN C_JUDGE%NOTFOUND;
4.对提取出的数据进行操作
UPDATE TWF_MODEL_NODE_JUDGE
SET INDEX_NO = C_JUDE_ROW.INDEX_NO - 1
WHERE TWF_MODEL_NODE_JUDGE.WF_M_NODE_CFG_ID = C_JUDE_ROW.WF_M_NODE_CFG_ID;
COMMIT;
5.结束循环
END LOOP;
END IF;
END JUDGE_LIST_DEL;
完整的存储过程:
/*
功能描述:删除判断节点配置情况
创建人:mengmeng.chen 2018-11-22
*/
1 PROCEDURE JUDGE_LIST_DEL
2 (
3 P_WF_M_ID IN TWF_MODEL_NODE_JUDGE.WF_M_ID%TYPE,
4 P_WF_M_NODE_ID IN TWF_MODEL_NODE_JUDGE.WF_M_NODE_ID%TYPE,
5 P_INDEX_NO IN TWF_MODEL_NODE_JUDGE.INDEX_NO%TYPE
6 ) AS
7 V_COUNT INTEGER;
8 V_INDEX_NO INTEGER;--游标参数
9 CURSOR C_JUDGE(P_C_INDEX_NO IN INTEGER) IS --声明显式游标
10 SELECT * FROM TWF_MODEL_NODE_JUDGE
11 WHERE INDEX_NO > P_C_INDEX_NO
12 ORDER BY INDEX_NO;
13 C_JUDE_ROW C_JUDGE%ROWTYPE; --定义游标变量
14 V_INDEX_NO_TEMP INTEGER;--临时次序
15 BEGIN
16
17 --判断该模板是否在使用中
18 SELECT COUNT(1)
19 INTO V_COUNT
20 FROM TWF_INSTANCE
21 WHERE WF_M_ID = P_WF_M_ID;
22
23 IF V_COUNT = 0 THEN
24 --删除检查配置
25 DELETE FROM TWF_MODEL_NODE_JUDGE
26 WHERE INDEX_NO = P_INDEX_NO AND
27 WF_M_ID = P_WF_M_ID AND
28 WF_M_NODE_ID = P_WF_M_NODE_ID;
29 COMMIT;
30 V_INDEX_NO := P_INDEX_NO ;
31 --打开游标
32 OPEN C_JUDGE(V_INDEX_NO);
33 LOOP
34 FETCH C_JUDGE INTO C_JUDE_ROW;
35 EXIT WHEN C_JUDGE%NOTFOUND;
36 UPDATE TWF_MODEL_NODE_JUDGE
37 SET INDEX_NO = C_JUDE_ROW.INDEX_NO - 1
38 WHERE TWF_MODEL_NODE_JUDGE.WF_M_NODE_CFG_ID = C_JUDE_ROW.WF_M_NODE_CFG_ID;
39 COMMIT;
40 END LOOP;
41
42 END IF;
43 END JUDGE_LIST_DEL;