SQL - 如果另一个表中存在值,则插入表中

时间:2011-12-02 12:12:33

标签: sql oracle toad

我正在尝试让下面的sql查询工作但我收到错误,我的问题如下:

我需要从select语句循环一个结果集(这部分很好)。在每行的循环中,我需要检查tableA中是否存在URL。如果是,则将映射插入tableB,否则将新行插入tableC。

这就是我所拥有的,但是当我尝试执行时,我在IF上输入错误说ORA-06550:第8行,第15列:PLS-00103:当期待其中一个时遇到符号“SELECT”以下:( - + case mod new no .....

DECLARE 
STANDARD_LINK_ID TABLEB.LINK_ID%type;

BEGIN   
      FOR LINK_ROW IN ( SELECT LINKTEXT, LINKURL, CORPID FROM OLD_DATA)
      LOOP
          IF (SELECT COUNT(URL) FROM TABLEA WHERE URL = LINK_ROW.LINKURL) = 1
          THEN
            SELECT LINKID INTO STANDARD_LINK_ID FROM TABLEA WHERE URL = LINK_ROW.URL;
            INSERT INTO TABLEB(LINK_ID, CORP_ID) 
            VALUES (STANDARD_LINK_ID,  LINK_ROW.CORPID);
          ELSE
            INSERT INTO TABLEB(LINK_ID, LINK_NAME, URL, CORP_ID) 
            VALUES (SEQ_LINK.NEXTVAL, LINK_ROW.LINKTEXT, LINK_ROW.LINKURL, 
                                 LINK_ROW.CORP_ID);
          END IF;                 
      END LOOP;
    COMMIT;
END;

3 个答案:

答案 0 :(得分:2)

我怀疑你的if语句是罪魁祸首,因为你想要实现的目标绝对是可能的。你可以试试以下代码:

DECLARE 
STANDARD_LINK_ID TABLEB.LINK_ID%type;
URL_COUNT NUMBER(10);

BEGIN   
      FOR LINK_ROW IN ( SELECT LINKTEXT, LINKURL, CORPID FROM OLD_DATA)
      LOOP
          SELECT COUNT(URL) INTO URL_COUNT FROM TABLEA WHERE URL = LINK_ROW.LINKURL;
          IF URL_COUNT = 1 THEN
            SELECT LINKID INTO STANDARD_LINK_ID FROM TABLEA WHERE URL = LINK_ROW.URL;
            INSERT INTO TABLEB(LINK_ID, CORP_ID) 
            VALUES (STANDARD_LINK_ID,  LINK_ROW.CORPID);
          ELSE
            INSERT INTO TABLEB(LINK_ID, LINK_NAME, URL, CORP_ID) 
            VALUES (SEQ_LINK.NEXTVAL, LINK_ROW.LINKTEXT, LINK_ROW.LINKURL, 
                                 LINK_ROW.CORP_ID);
          END IF;                 
      END LOOP;
    COMMIT;
END;

希望它可以帮助您找出问题所在。

答案 1 :(得分:2)

我写的很快,但我认为这两个查询将解决你的问题没有循环(这比一次插入慢):

-- insert to tableb when exists in tablea
insert into tableb(link_id, corp_id) 
select a.linkid, o.corpid
from old_data o
join tablea a on o.url = a.url


-- insert to tablec when not exists in tablea
insert into tablec(link_id, link_name, url, corp_id) 
select seq_link.nextval, o.linktext, o.linkurl, o.corp_id
from old_data o
where not exists(select 1 from tablea a where o.url = a.url)

PS。你不要忘记tablec中的else吗?

答案 2 :(得分:1)

我已经看到过这样做了;但不是条件

中嵌入了返回值的select

https://forums.oracle.com/forums/thread.jspa?threadID=177035

DECLARE 
STANDARD_LINK_ID TABLEB.LINK_ID%type;
DECLARE W_LINK_COUNT NUMBER (1,0);

BEGIN   
      FOR LINK_ROW IN ( SELECT LINKTEXT, LINKURL, CORPID FROM OLD_DATA)
      LOOP
          SELECT COUNT(URL) INTO W_LINK_COUNT FROM TABLEA WHERE URL = LINK_ROW.LINKURL;
          IF  W_LINK_COUNT = 1
          THEN
            SELECT LINKID INTO STANDARD_LINK_ID FROM TABLEA WHERE URL = LINK_ROW.URL;
            INSERT INTO TABLEB(LINK_ID, CORP_ID) 
            VALUES (STANDARD_LINK_ID,  LINK_ROW.CORPID);
          ELSE
            INSERT INTO TABLEB(LINK_ID, LINK_NAME, URL, CORP_ID) 
            VALUES (SEQ_LINK.NEXTVAL, LINK_ROW.LINKTEXT, LINK_ROW.LINKURL, 
                                 LINK_ROW.CORP_ID);
          END IF;                 
      END LOOP;
    COMMIT;
END;