我有两个表(MST_IP和MST_ROUTER),如下所示:
MST_IP
SEQ IP_NUMBER STATUS
1 12.123.0.0 0
2 12.123.0.1 0
3 12.104.0.1 0
4 12.104.0.2 0
5 13.123.0.1 0
6 13.123.0.2 0
7 11.555.8.1 0
8 11.555.8.2 0
我想从MST_IP到MST_ROUTER(IP_NUMBER和REMOTE_NUMBER)插入IP_NUMBER。我想要的数据如下所示。
MST_ROUTER
USER_ID IP_NUMBER REMOTE_NUMBER
AA 12.123.0.0 12.123.0.1
BB 12.104.0.1 12.104.0.2
CC 13.123.0.1 13.123.0.2
DD 11.555.8.1 11.555.8.2
下面是我的代码。
/* Formatted on 9/28/2017 11:44:11 AM (QP5 v5.115.810.9015) */ SET SERVEROUTPUT ON
DECLARE INS VARCHAR2 (100); INS2 VARCHAR2 (100); SEQ_R INTEGER; BEGIN SEQ_R := 1;
SELECT IP_NUMBER
INTO INS
FROM ( SELECT *
FROM MST_IP
WHERE STATUS = 0
ORDER BY SEQ ASC)
WHERE ROWNUM = 1;
SELECT IP_NUMBER
INTO INS2
FROM ( SELECT *
FROM MST_IP
WHERE STATUS = 0 AND SEQ = SEQ_R + 1
ORDER BY SEQ ASC)
WHERE ROWNUM = 1;
INSERT INTO MST_ROUTER (USER_ID, IP_NUMBER)
VALUES ('HMJ-BKS', INS);
COMMIT;
UPDATE MST_IP
SET STATUS = 1
WHERE IP_NUMBER = INS;
COMMIT;
UPDATE MST_ROUTER
SET REMOTE_NUMBER = INS2
WHERE USER_ID = 'HMJ-BKS';
COMMIT;
UPDATE MST_IP
SET STATUS = 1
WHERE IP_NUMBER = INS2;
COMMIT;
DBMS_OUTPUT.PUT_LINE (INS); END; /
在我的代码中,如果数据已插入MST_ROUTER,我在MST_IP = 1上更改STATUS。插入第二行后我的代码无效的问题。任何评论将不胜感激。
答案 0 :(得分:1)
使本地和远程IP地址相互一致的一个技巧是将本地地址的子查询加入其远程对应项,然后将此结果插入MST_ROUTER
表。
INSERT INTO MST_ROUTER (USER_ID, IP_NUMBER, REMOTE_NUMBER)
SELECT
CAST(t1.SEQ / 2 AS UNSIGNED) AS SEQ, t1.IP_NUMBER, t2.IP_NUMBER
FROM
(
SELECT SEQ - 1 AS SEQ, IP_NUMBER
FROM MST_IP
WHERE MOD((SEQ - 1), 2) = 0
) t1
LEFT JOIN MST_IP t2
ON t1.SEQ = t2.SEQ - 2
以下是演示的链接,该演示显示了我的查询的选择组件的输出。
<强>输出:强>
SEQ IP_NUMBER IP_NUMBER
0 12.123.0.0 12.123.0.1
1 12.104.0.1 12.104.0.2
2 13.123.0.1 13.123.0.2
3 11.555.8.1 11.555.8.2