使用IF计算行并插入条件

时间:2014-12-06 11:37:19

标签: oracle

以下Oracle SQL不起作用。我究竟做错了什么?如何在不声明变量的情况下使其工作?

IF ((SELECT count(*) FROM IBS_GROUP g WHERE g.NAME = 'gmy-vp1_all') = 0) THEN 
  INSERT INTO IBS_GROUP(ID, GROUP_CONTEXT_ID, NAME, DESCRIPTION, ACTIVE) values (IBS_GROUP_SEQ.NEXTVAL, 6, 'gmy-vp1_all', 'gmy-vp1_all', 'Y')
END IF;

这是我得到的错误:

Error starting at line : 1 in command -
IF ((SELECT count(*) FROM IBS_GROUP g WHERE g.NAME = 'gmy-vp1_all') = 0) THEN 
Error report -
Unknown Command

Error starting at line : 2 in command -
INSERT INTO IBS_GROUP(ID, GROUP_CONTEXT_ID, NAME, DESCRIPTION, ACTIVE) values (IBS_GROUP_SEQ.NEXTVAL, 6, 'gmy-vp1_all', 'gmy-vp1_all', 'Y') 
END IF 
Error at Command Line : 3 Column : 1
Error report -
SQL Error: ORA-00933: SQL komutu tam doğru olarak sona ermedi
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

INSERT INTO IBS_GROUP(ID, GROUP_CONTEXT_ID, NAME, DESCRIPTION, ACTIVE) 
select IBS_GROUP_SEQ.NEXTVAL, 6, 'gmy-vp1_all', 'gmy-vp1_all', 'Y' 
from dual where (SELECT count(*) FROM IBS_GROUP g WHERE g.NAME = 'gmy-vp1_all') = 0;

不允许在IF语句中使用子查询。