about pl/sql - help me please...urgent needed [message #200192] |
Sun, 29 October 2006 01:03 |
hiip
Messages: 78 Registered: October 2006
|
Member |
|
|
hi everyone, hope can give me some idea or correct my error...
this is what i want to create..
procedure list_campaigns(cname in campaign.companyname%type)
for a given company, this procedure prints out information on all the campaigns run for that company, for each campaign, it should print out as a minimum the campaign title and the estimated and the actual cost of the campaign. must use a cursor for loop
and this is my sample relational data model
campaign(title, campaignfinishdate, estimatedcost, actualcost,datepaid, companyname, managerstaffno)
primary key : title
foreign key : companyname refers to companyname in client
foreign key : managerstaffno refers to staffno in staff..
and this is my code
--Procedure list_campaigns(cname in campaign.companyname%Type)--
CREATE OR REPLACE PROCEDURE list_campaign
(cname IN campaign.companyname%Type)
IS
CURSOR list_campaign_cur IS
SELECT title, estimatedcost, actualcost
FROM campaign;
BEGIN
FOR list_campaign_rec IN list_campaign_cur LOOP
IF cname = campaign.companyname THEN
DBMS_OUTPUT.PUT_LINE( list_campaign_cur.TITLE || ' ' || 'Estimated Cost: ' || list_campaign_cur.estimatedcost || ' ' ||
' Actual Cost: ' || list_campaign_cur.actualcost );
END IF;
END LOOP;
END;
/
|
|
|
|
|
|
|
|
Re: about pl/sql - help me please...urgent needed [message #200483 is a reply to message #200199] |
Tue, 31 October 2006 01:43 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
An improvement, given that you've got a requirement to use a cursor for loop, would be this (Based on @Littlefoot's code)
CREATE OR REPLACE PROCEDURE list_campaign (cname IN campaign.companyname%TYPE) IS
CURSOR list_campaign_cur (p_company IN campaign,companyname%type) IS
SELECT title, estimatedcost, actualcost, companyname
FROM campaign
WHERE companyname = p_company;
BEGIN
FOR list_campaign_rec IN list_campaign_cur(cname) LOOP
DBMS_OUTPUT.put_line ( list_campaign_rec.title
|| ' '
|| 'Estimated Cost: '
|| list_campaign_rec.estimatedcost
|| ' '
|| ' Actual Cost: '
|| list_campaign_rec.actualcost
);
END LOOP;
END;
You spend less time retrieving records from the database that you aren't going to output if you move the check on company name up into the cursor.
|
|
|