将数据类型Number转换为varchar2

时间:2015-12-05 01:21:14

标签: oracle plsql case

我目前正致力于在PL SQL中创建存储过程,将数据类型编号4转换为varchar2到A.

  SELECT s.sname, g.sid, s.sid, c.cid, g.cid,
  CAST(CASE g.grade
      WHEN 4 THEN 'A'
      WHEN 3 THEN 'B'
      WHEN 2 THEN 'C'
      WHEN 1 THEN 'D'
      WHEN 0 THEN 'F'
      ELSE 'No Value'
      END AS varchar2(55)) AS Grades
      FROM student s, grades g, class c
      WHERE c.cid = g.cid
  AND g.sid = s.sid
  AND g.sid = s.sid;

这样可以正常工作,但是一旦我在创建过程中添加它,就会将其误认为是意外错误           错误代码-6502:ORA-06502:PL / SQL:数字或值错误:字符到数字转换错误。

另外,我尝试在过程中添加以下内容并返回相同的错误。

EXIT WHEN c1%NOTFOUND; -- exit check
    dbms_output.put_line('Student Name: '|| s_sname); 
    dbms_output.put_line('Class Name: ' || c_cname);
    dbms_output.put_line('Grade: ' || to_char(g_grade, 'ABCDF');
    dbms_output.put_line('-----------------');

在上面我试图打印字母等级而不是数字等级。

1 个答案:

答案 0 :(得分:1)

您需要粘贴整个代码。它对我有用。

create table testing(i int);
insert into testing values (0);
insert into testing values (1);
insert into testing values (2);
insert into testing values (3);

create or replace procedure p
is
begin
  for i in (select CAST(CASE g.grade
   WHEN 4 THEN 'A'
   WHEN 3 THEN 'B'
   WHEN 2 THEN 'C'
   WHEN 1 THEN 'D'
   WHEN 0 THEN 'F'
   ELSE 'No Value'
   END AS varchar2(55)) AS Grades
 from testing
 )
 loop
   dbms_output.put_line(i.Grades);
 end loop;
end;
/

set serveroutput on;

begin
  p;
end;
/

D
C
F
B

PL/SQL procedure successfully completed.