2016-07-24 146 views
-2

有些東西關閉了但找不到我所缺少的東西?嘗試創建PL/SQL過程時出現ORA-06550錯誤

select 
     grade.sectionid, 
     grade.studentid, 
     course.courseid 
    FROM section 
     INNER JOIN grade 
       ON grade.sectionid = section.sectionid 
     INNER JOIN course 
       ON course.courseid = section.courseid; 




DECLARE 
    CURSOR mycursor is 
     select 
     sectionid, studentid, courseid, coursename 
     FROM grade, course, section;  
    var_secID NUMBER(10); 
    var_studentID NUMBER (10); 
    var_gradeLetter CHAR(1); 
    var_coursetitle VARCHAR2(25); 


BEGIN 
    OPEN mycursor; 
LOOP 
     FETCH mycursor 
     INTO var_secID, var_studentID, var_gradeLetter, var_coursetitle; 
     EXIT WHEN mycursor%NOTFOUND; 
     If var_coursetitle(coursename,1) BETWEEN A AND F then 
     UPDATE grade 
     SET grade = 'A' 
     WHERE sectionid; 
     END IF; 
END LOOP; 
CLOSE mycursor; 
END; 
/

不斷收到此錯誤:

錯誤在第21行:

ORA-06550:第21行,列18:

PL/SQL:ORA-00920:無效的關係運算符

ORA-06550:線19,第4欄:

PL/SQL:SQL語句忽略

+1

在遊標定義'.......從等級,課程,節; '創建三個表的交叉連接(從這三個表中生成所有行的所有可能的組合),所以我不太確定你想用這個查詢來實現什麼。 – krokodilko

+0

我已經創建了一個觸發器將成績填充到我的GRADE表中。我希望程序查看學生課程名稱的最後一個字母,並根據最後一個字母分配一個等級(A-F)。這是否有助於澄清?(這是我第一次使用數據庫或SQL進行工作,所以我仍然很新,並試圖弄清楚)。 – ecooper10

+1

走到最後你有'......更新等級...... WHERE sectionid;'這是什麼意思?在哪裏sectionid **什麼**?那裏丟失了一些東西。 – mathguy

回答

1
BETWEEN A AND F then 

也許應該是:

BETWEEN 'A' and 'F' then 
0

你可以做到這一點沒有任何PL/SQL,如:

update grade g set grade = 'A' 
where g.sectionid in 
     (select s.sectionid from section s 
       join course c on c.courseid = s.courseid 
     where substr(c.coursename,-1, 1) between 'A' and 'F'); 

但是作爲一個PL/SQL學習鍛鍊,你可以這樣做它沒有任何變量或光標管理:

begin 
    for r in (
     select g.sectionid, g.studentid, c.courseid, c.coursename 
      , substr(c.coursename,-1, 1) as course_lastchar 
     from grade g 
       join section s on s.sectionid = g.sectionid 
       join course c on c.courseid = s.courseid 
     for update of grade 
    ) 
    loop 
     if r.course_lastchar between 'A' and 'F' then 
      update grade set grade = 'A' where sectionid = r.sectionid; 
     end if; 
    end loop; 
end; 

(順便說一句,這是值得養成一致的縮進尺寸整齊地鋪設代碼的習慣。)

相關問題