You have some issues in your syntax:
- the
DECLARE
is wrong in a procedure definition; you would use a DECLARE
, for example, to build an anonymous block where you need to declare variables (DECLARE ... BEGIN ... END
) or in a trigger definition.
- you need to specify a type for the variable
recCount
, and the variable definition goes before the BEGIN
- you have an unnecessary
BEGIN...END
block; this is not an error, but the block is unuseful here; it only would be useful to add exception handling to a specific part of code
I would even consider adding some exception handling; here I give an example with OTHERS
, but you should better specify the exceptions to handle depending on your code.
Also, as already noticed in MT0's comment, the COMMIT could not be a great idea.
CREATE OR REPLACE PROCEDURE MyProcedure(cName NUMBER, cStatus VARCHAR2) AS
recCount NUMBER;
BEGIN
SELECT COUNT(*)
INTO recCount
FROM myTable
WHERE colName = cName
AND colStatus = cStatus;
IF recCount = 0
THEN
-- your code
ELSE
-- your code
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- exception handling
END;
If you only need to check if a row exists, no matter how many rows match the conditions, you can avoid counting all the matching rows, by adding a condition to only get one row:
SELECT COUNT(*)
INTO recCount
FROM myTable
WHERE colName = cName
AND colStatus = cStatus
AND rownum = 1;
You could even use this to build an example of a block within a procedure, with exception handling ( I only post this as an example, I do not like this approach):
BEGIN
SELECT 1
INTO recCount
FROM myTable
WHERE colName = cName
AND colStatus = cStatus
AND rownum = 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
recCount := 0;
END;