The code below may return more than one row. Will sql%rowcount return the number of rows fetched?
select * from emp where empname = 'Justin' and dept='IT'
if sql%rowcount>0
...
This is my sample proc; am I using sql%rowcount in correct way?
CREATE PROCEDURE Procn(in_Hid IN VARCHAR2,outInststatus OUT VARCHAR2,outSockid IN NUMBER,outport OUT VARCHAR2,outIP OUT VARCHAR2,outretvalue OUT NUMBER)
AS
BEGIN
select INST_STATUS into outInststatus from TINST_child where INST_ID = in_Hid and INST_STATUS = 'Y';
if outInststatus = 'Y' then
select PORT_NUMBER,STATIC_IP into outport,outIP from TINST where INST_ID = in_Hid and IP_PORT_STATUS = 'Y';
if sql%rowcount >= 1 then
select SOCK_ID into outSockid from TINST where PORT_NUMBER = outport AND STATIC_IP = outIP;
outretvalue := 0;
else
outretvalue := -12;
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN
outretvalue := -13;
end if;
END;
no_data_foundexception if the row count was zero, ortoo many rowsif it was greater than 1, so you could use exception handling to deal with this; or be using an explicit cursor. I think more context is needed.select ... into(unless you havebulk collect intoas Codo suggests) will always return exactly one row, or raise a exception. Imagine there were two matching rows; which of the two values forstatic_ipwould be inoutIP? Your code will work as long as there is one row, but fail when there isn't. Is it valid for there to be more than one row, and what should happen if there is?outInstatuscan only beYwhen you check it - if there is no matching rows from your firstselectthanno_data_foundwill be raised before you get to theif. And although the construct isn't right anyway, ifsql%rowcountwas zero - which is can't be, you'd getno_data_foundthere instead - then you aren't settingoutretvalue. I'm not entirely sure if you need to hit the table three times; that may suggest that the thirdselectcould return multiple rows too. I think you need to clarify what's supposed to be happening in various scenarios.