这个程序有什么问题?

时间:2011-06-03 11:56:27

标签: oracle plsql

我正在写一个小程序。我有一个名为c1的游标,以及一个如下所示的变量v_c1v_c1 c1%ROWTYPE。将光标提取到其中。

然而问题是从光标提取的值不是数字。我真的很困惑,因为它是表中的一个数字,它在打印时看起来像一个数字。这是程序。

create or replace
PROCEDURE mini
IS
  CURSOR c1
  IS
    SELECT * FROM utslipp;

  v_c1 c1%ROWTYPE;
  v_co2_utslipp NUMBER;
  v_kildenavn VARCHAR2(200);
  v_temp NUMBER;

BEGIN
  OPEN c1;
  FETCH c1 INTO v_c1;
  v_co2_utslipp := v_c1.co2_utslipp;
  v_co2_utslipp := v_c1.co2_utslipp;

  LOOP
    FETCH c1 INTO v_c1;
    EXIT WHEN c1%NOTFOUND;

    v_temp := v_c1.co2_utslipp;

    IF v_temp < v_co2_utslipp THEN
      v_co2_utslipp := v_c1.co2_utslipp;
      v_kildenavn := v_c1.kildenavn;

    -- According to SQL Developer the error is here.
    ELSIF v_temp = v_co2_utslipp THEN

      -- ...When the actual error is here.      
      v_co2_utslipp := v_co2_utslipp || ', ' || v_c1.co2_utslipp;
      v_kildenavn := v_kildenavn || v_c1.kildenavn;
    END IF;    
  END LOOP;

  DBMS_OUTPUT.PUT_LINE(v_kildenavn || ', ' || v_co2_utslipp);
END; 

更新:以下是脚本:

alter table utslipp
 drop primary key cascade ;
alter table produkt
 drop primary key cascade ;
alter table fylke
 drop primary key cascade ;
alter table eieren
 drop primary key cascade ;
drop table utslipp ;
drop table produkt ;
drop table fylke ;
drop table eieren ;



create table produkt
(
  pid number(2) primary key,
  produktnavn varchar2(20)
) ;

create table fylke
(
 fid number(2) primary key ,
 fylkesnavn varchar2(20)
) ;

create table eieren
(
 eid number(2) primary key,
 eiernavn varchar2(20)
) ;

create table utslipp
(
  kid number(2) ,
  kildenavn varchar2(20),
  eid number(2),
  pid number(2),
  fid number(2),
  aar number(4),
  co2_utslipp number(4),
  constraint utslipp_pk primary key(kid,aar) ,
  constraint eieren_fk
   FOREIGN KEY (eid)
   REFERENCES eieren,
  constraint produkt_fk
   FOREIGN KEY (pid) 
   REFERENCES produkt,
  constraint fylke_fk
   FOREIGN KEY (fid)
   REFERENCES fylke 
) ;

insert into eieren values(1,'Statoil');
insert into eieren values(2,'Hydro');
insert into eieren values(3,'Yara');
insert into eieren values(4,'Esso');
insert into eieren values(5,'Norcem') ;
insert into eieren values(6,'Alcoa') ;
insert into eieren values(7,'Eramet') ;
insert into eieren values(8,'Gassco') ;
insert into eieren values(9,'Philips') ;
insert into eieren values(10, 'Noretyl') ;

insert into produkt values(1,'Olje');
insert into produkt values(2,'Naturgass');
insert into produkt values(3,'Aluminium');
insert into produkt values(4,'Kunstgjødsel');
insert into produkt values(5,'Etylen');
insert into produkt values(6,'Stål');
insert into produkt values(7,'Bensin');
insert into produkt values(8,'Sement') ;

insert into fylke values(1, 'Finm') ;
insert into fylke values(2, 'Troms') ;
insert into fylke values(3, 'Nordland') ;
insert into fylke values(4, 'Nord-Trøndelag') ;
insert into fylke values(5, 'Sør-Trøndelag') ;
insert into fylke values(6, 'Møre-Romsdag') ;
insert into fylke values(7, 'Sogn-Fjordane') ;
insert into fylke values(8, 'Hordaland') ;
insert into fylke values(9, 'Rogaland') ;
insert into fylke values(10, 'Vest-Agder') ;
insert into fylke values(11, 'Aust-Agder') ;
insert into fylke values(12, 'Telemark') ;
insert into fylke values(13, 'Vestfold') ;
insert into fylke values(14, 'Buskrud') ;
insert into fylke values(15, 'Akershus') ;
insert into fylke values(16, 'Oslo') ;
insert into fylke values(17, 'Østfold') ;
insert into fylke values(18, 'Oppland') ;
insert into fylke values(19, 'Hedmark') ;
insert into fylke values(20, 'Offshore') ;

insert into utslipp values(1,'Snøhvit',1,1,1,2009,805);
insert into utslipp values(2,'Kjøpsvik',5,8,3,2009,409);
insert into utslipp values(3,'Mosjøen',6,3,3,2009,312);
insert into utslipp values(4,'Heidrun',1,1,20,2009,410);
insert into utslipp values(5,'Åsgard',1,1,20,2009,1041);
insert into utslipp values(6,'Tjeldbergodden',1,3,5,2009,315);
insert into utslipp values(7,'Sunndal',2,3,6,2009,528);
insert into utslipp values(8,'Snorre',1,1,20,2009,585);
insert into utslipp values(9,'Statfjord',1,1,20,2009,576);
insert into utslipp values(10,'Gullfaks',1,1,20,2009,979);
insert into utslipp values(11,'Troll',1,2,20,2009,625);
insert into utslipp values(12,'Mongstad',1,2,7,2009,1549);
insert into utslipp values(13,'Oseberg',1,1,20,2009,1143);
insert into utslipp values(14,'Odda',7,8,8,2009,328);
insert into utslipp values(15,'Karmøy',2,3,9,2009,317);
insert into utslipp values(16,'Kollsnes',8,2,9,2009,1129);
insert into utslipp values(17,'Sleipner',1,1,20,2009,865);
insert into utslipp values(18,'Ekkofisk',9,1,20,2009,1023);
insert into utslipp values(19,'Porsgrunn',3,4,12,2009,629);
insert into utslipp values(20,'Brevik',5,8,12,2009,825);
insert into utslipp values(21,'Bamble',10,5,12,2009,459);

-- Here is the newest insert.
INSERT INTO utslipp VALUES(2, 'foobar', 3, 1, 2, 2008, 312);    
insert into utslipp values(1,'Snøhvit',1,1,1,2008,1300);
insert into utslipp values(1,'Snøhvit',1,1,1,2007,1600);

很抱歉它的长度,但我不想删除任何可能影响结果的内容。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

问题不在于比较数字时。它位于下一行 - 您尝试将包含逗号(,)的字符串分配给数字变量v_co2_utslipp。

你完全误导我错误的“错误”行:(。