如何在PL / SQL中使用`SELECT COUNT(*)`作为条件语句?

时间:2012-07-26 13:28:27

标签: sql plsql count

我有一个存储过程可以更新记录(如果存在),或者添加新记录(如果它们不存在)。

SQL:

CREATE OR REPLACE PROCEDURE ADDRECORD(ihost VARCHAR, iip VARCHAR)
AS 
   rc VARCHAR(4000);
   ROWCOUNT NUMBER;
BEGIN
   rc := 'select count(0) from myTable where physical_host =  ihost and primary_ip = iip';
   ROWCOUNT := to_number(rc, '99');

   IF ROWCOUNT = 1 THEN
      UPSERTRECORD(ihost, iip);
   ELSE
      INSERT INTO myTable(PHYSICAL_HOST, PRIMARY_IP)
      VALUES (ihost, iip);
      INSERT INTO IP (IP, IP_IND) VALUES (iip, 'V');
   END IF;    
END ADDRECORD;

UPSERTRECORD是另一个被调用的存储过程。它工作正常。实际上,错误发生在包含to_number的行上。错误是:

ORA-06502: PL/SQL: numeric or value error: character to number conversion error 

还有其他办法吗?对不起,我对SQL不是很有经验,但我需要弄明白这一点。

1 个答案:

答案 0 :(得分:2)

待办事项

DECLARE rowcount As Number(38)
select count(*) INTO rowcount from myTable 
where physical_host =  ihost and primary_ip = iip