Using a cursor throughout a procedure
I am somewhat new to PL/SQL, and am having a little trouble using a cursor that I created. I am writing a procedure that sends emails to particular pe开发者_如何学JAVAople. Here is the relevant code:
sql stmt := ' -- All my sql is in here -- ';
open email_cursor for sql stmt;
fetch email_cursor into term, award, desc, id, name, xmdt;
.....
if v_id is not null then
email_adr := schema1.get_email_adr(v_id); --This is partly where problem is.
if v_opt_ltr = 'Y' then --a variable that decides if an email should be sent
UTL_MAIL.SEND (sender => email_from,
recipients => email_adr,
subject => v_email_subject,
mime_type => 'text/html',
message => email_body );
END IF;
END IF;
Okay, so I dont get any errors or anything when I run this, but for some reason when I run the function for the email_adr variable it fails, and doesnt give me anything, and in turn wont send an email.
Another question (because I am new to cursors): The v_id in the cursor should have about 25 records, if I run the line "stu_email := schema1.get_email_adr(v_id);" will this give me all 25 records, and then will the "utl_mail.send" send an email to all the recipients?
As I understand it, you cursor is inside the get_email_adr
function, is that right? Althought none of those variables looks obviously like an email address, so I'm a little confused.
If so then it all depends on how the code inside the get_email_adr
function works. Every time you open
a cursor it "resets" it. So if you want to combine all 25 records you'll need to have some code for it. Something like this:
FUNCTION get_email_adr( f_id IN NUMBER ) RETURN VARCHAR2 IS
result VARCHAR2(32000);
CURSOR cur_email ( c_id IN NUMBER ) IS
SELECT email_address FROM some_table WHERE id = c_id;
BEGIN
FOR rec IN cur_email( f_id )
LOOP
IF( result IS NULL )
THEN
result := rec.email_address;
ELSE
result := result || ',' || rec.email_address;
END IF;
END LOOP;
RETURN result;
END get_email_adr;
Does that kind of answer you question or have I missed the point entirely?
精彩评论