简体   繁体   中英

PL/SQL Procedure with cursor “anonymous block completed”

I've been trying to get this procedure working for the last few hours:

CREATE OR REPLACE PROCEDURE Search_Testimonials(keyword VARCHAR2)
AS
l_cursor_1 SYS_REFCURSOR;
Temp_Content VARCHAR(255);
BEGIN
   OPEN l_cursor_1 FOR
      SELECT T_Content
      INTO Temp_Content
      FROM Testimonial
      WHERE T_Content LIKE '%' || Keyword || '%';
      dbms_output.put_line(Temp_Content);
   DBMS_SQL.RETURN_RESULT(l_cursor_1);
END;

It's pretty much supposed to run through the testimonials table and output every row that has an instance of the keyword in the parameter. It compiles with no errors but when i execute like so:

EXECUTE Search_Testimonials('someword');

I get this error: "anonymous block completed". Does anyone know what's going on? I'm new to PL/SQL and am running out of resources on the internet or just don't understand what I'm reading.

-I'm running this all in oracle sql developer.

You are mixing metaphors here. You either need to use all dynamic SQL syntax here or none at all.

However you do not really need dynamic SQL for what you are doing, just make the l_cursor_1 type as an out parameter.

Try something like this:

<code>
CREATE OR REPLACE PROCEDURE Search_Testimonials(keyword VARCHAR2 IN,
                                                oResults IN OUT l_cursor_1,
                                                oStatus     OUT VARCHAR)
IS
type l_cursor_1 is REF CURSOR;
-- Temp_Content VARCHAR(255);
err_code                     varchar2(30);
err_msg                      varchar2(200);
BEGIN
   oStatus := "1";
   OPEN oResults FOR
      SELECT T_Content
      FROM Testimonial
      WHERE T_Content LIKE '%' || Keyword || '%';
      oStatus := "0";
EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
         oStatus := "2";
         err_code := SQLCODE;
         err_msg := SUBSTR (SQLERRM, 1, 200);
      DBMS_OUTPUT.PUT_LINE('ERROR: '|| err_code || ' : ' || err_msg ); 
--       RAISE;
END Search_Testimonials;
</code>

Check the oStatus before processing the output if it's 2 you have an error or if 1 no data.

You can extend the Exception processing by inserting the error code, message, proc name into an error table.

Also for performance reasons I would not use the %Keyword% construct by default. use Keyword% as default and pass "% some keyword" to do the same. If you have a index on the column it will never be used with %Keyword% construct ....

Hope this helps

Nick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM