我正在尝试将新数据插入表中,同时根据其中一个表中的“冲突”属性检查是否有任何数据(处理)彼此冲突。我不太确定我的PL / SQL实际上是否完全正确,但是当我尝试插入新数据时说“确切提取返回超过请求的行数”时,我当前收到错误。我无法弄清楚这究竟意味着什么。我的程序:
CREATE OR REPLACE PROCEDURE FPRESC (
FP_ID VARCHAR2,
FTREAT_ID VARCHAR2,
FCLIENT_ID VARCHAR2,
FDOC_ID VARCHAR2,
FP_DATE DATE)
AS
V_CLIENT_ID PRESCRIPTION.CLIENT_ID%TYPE;
V_CONFLICT TREATMENTS.CONFLICT%TYPE;
V_P_DATE PRESCRIPTION.P_DATE%TYPE;
V_TREAT_ID TREATMENTS.TREAT_ID%TYPE;
BEGIN
SELECT P.CLIENT_ID, T.CONFLICT, P.P_DATE, T.TREAT_ID
INTO V_CLIENT_ID, V_CONFLICT, V_P_DATE, V_TREAT_ID
FROM PRESCRIPTION P, TREATMENTS T
WHERE P.TREAT_ID=T.TREAT_ID;
IF FP_DATE = V_P_DATE AND FCLIENT_ID = V_CLIENT_ID AND FTREAT_ID = V_TREAT_ID AND V_CONFLICT != NULL THEN
DBMS_OUTPUT.PUT_LINE('CONFLICT');
ELSE
INSERT INTO PRESCRIPTION (P_ID, TREAT_ID, CLIENT_ID, DOC_ID, P_DATE)
VALUES (FP_ID, FTREAT_ID, FCLIENT_ID, FDOC_ID, FP_DATE);
END IF;
END FPRESC;
/
以下是我要插入的数据示例:
EXEC FPRESC ('P00011', 'T016', 'C00017', 'D006', '28-NOV-14');
此示例插入冲突的数据,因此我可以测试该过程是否能够识别冲突。
答案 0 :(得分:0)
SELECT P.CLIENT_ID, T.CONFLICT, P.P_DATE, T.TREAT_ID
INTO V_CLIENT_ID, V_CONFLICT, V_P_DATE, V_TREAT_ID
FROM PRESCRIPTION P, TREATMENTS T
WHERE P.TREAT_ID=T.TREAT_ID;
由于INTO子句,这个select应该只获取一个逻辑才能使逻辑工作。你得到的错误是因为你的select取了多于1行。修复你的逻辑,它应该工作..
答案 1 :(得分:0)
要继续使用@Shankar,您所拥有的将返回存在连接的表中的每一行。您需要将结果过滤到特定行。可能是这样的:
CREATE OR REPLACE PROCEDURE FPRESC (
FP_ID VARCHAR2,
FTREAT_ID VARCHAR2,
FCLIENT_ID VARCHAR2,
FDOC_ID VARCHAR2,
FP_DATE DATE)
AS
V_ROWCOUNT NUMBER;
V_CLIENT_ID PRESCRIPTION.CLIENT_ID%TYPE;
V_CONFLICT TREATMENTS.CONFLICT%TYPE;
V_P_DATE PRESCRIPTION.P_DATE%TYPE;
V_TREAT_ID TREATMENTS.TREAT_ID%TYPE;
BEGIN
SELECT COUNT(*)
INTO V_ROWCOUNT
FROM PRESCRIPTION P, TREATMENTS T
WHERE P.TREAT_ID=T.TREAT_ID
AND P.CLIENT_ID = FCLIENT_ID
AND P.P_DATE = FP_DATE
AND T.TREAT_ID = FTREAT_ID
AND V_CONFLICT != NULL;
IF (V_ROWCOUNT > 0) THEN
DBMS_OUTPUT.PUT_LINE('CONFLICT');
ELSE
INSERT INTO PRESCRIPTION (P_ID, TREAT_ID, CLIENT_ID, DOC_ID, P_DATE)
VALUES (FP_ID, FTREAT_ID, FCLIENT_ID, FDOC_ID, FP_DATE);
END IF;
END FPRESC;