PLSQL计数和过程

时间:2018-03-06 18:44:44

标签: oracle plsql oracle10g

目前正在尝试创建PL / SQL过程。我是PL / SQL的完整菜鸟,你可以告诉我们!

我们必须使用SQL创建一个表,我们希望用一个过程自动更新表。如果客户要求超过4个工作,我们希望将他们的详细信息作为常客输入此表。

我目前有:

 CREATE TABLE PublisherDetails
 (PublisherName VARCHAR2 (40),
 City VARCHAR2 (20) ,
 PhoneNo NUMBER (11),
 jobNo NUMBER (10),
  startDate DATE,
 completionDate DATE)
 ;

 SELECT Publisher.Name AS PublisherName,
 Publisher.City, Publisher.PhoneNo,
 COUNT (*) AS PublisherJobCount
 FROM Publisher
 INNER JOIN PrintJob
 ON Publisher.Name = PrintJob.PublisherName
 GROUP BY Publisher.Name, Publisher.City, Publisher.PhoneNo;

Create or replace procedure Task3
IS CountPublisherJobs NUMBER;
DECLARE No_data_Found EXCEPTION
BEGIN
SELECT count(*) INTO CountPublisherJobs 
OPEN Task3;
LOOP
IF PublisherJobCount < 3
THEN INSERT INTO PublisherDetails (PublisherName, City, PhoneNo)
FROM Publisher
WHERE PublisherName = publisher.name
Else
            Insert Into PublisherDetails (JobNo, StartDate, CompletionDate )
            SELECT jobNo, startDate, completionDate
            FROM PrintJob
            Where PublisherName = publishers.name
  FETCH Task3 INTO PublisherDetails, publishername, city, phoneNo;
  EXIT WHEN c1%NOTFOUND;  

    INSERT INTO temp VALUES (PublisherName, City, PhoneNo, JobNo, StartDate, CompletionDate);
END IF;
    COMMIT;
END LOOP;
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        dbms_output.put_line('Sorry no data found');
    END;
    /

它引起错误,我不知道为什么。任何帮助一如既往地表示赞赏。

1 个答案:

答案 0 :(得分:1)

您的程序语法有许多不正确之处。

存储过程的基本格式是

Create {Or Replace} Procedure PROCEDURE_NAME {(i_param IN datatype)}
Is
  <<Declaration Section>>
Begin
  <<code section>>
Exception
  <<Exceptions>>
End PROCEDURE_NAME;

根据您的描述,您希望在另一个表中满足条件时将记录插入表中。

要实现这一点,我需要查看基础数据结构,您提供的内容并不显示数据当前所在的表(例如,是否有JOB表?一个Customer表?)。

不需要声明NO_DATA_FOUND异常,它是Oracle异常

您的Select Count(*) Into CountJobs缺少From TABLE以及您要添加的任何谓词,但我不确定您要尝试用此方法完成的任务。

您正在尝试在过程名称上打开游标。您尚未定义名为Task3

的游标

您尚未声明CountPublisherJobs变量

我建议也许重新审视存储过程的基本结构。

修改

根据您的回复,您可以使用以下内容获得结果:

Create Or Replace Procedure addFrequentPublisher
Is
  Cursor frequentPublishers Is 
    Select  PUBLISHER_ID
    From    JOB
    Group By 
            PUBLISHER_ID
    Having  Count(*) >= 4;
Begin
 For i In frequentPublishers
 Loop
   Insert Into FREQUENT_CUSTOMER ...
 End Loop;
End;