2005년 10월 24일 월요일

[펌] oracle column 길이 구하는 방법

 

1. 보통의 varchar

select length( content ) ... ;

2. BLOB type

SELECT Dbms_Lob.getlength(CONTENT_BLOB) as CONTENT_SIZE, ...


3. LONG RAW type

select length(content) 로는 ORA-00932: inconsistent datatypes  에러가 발생한다.
아래의 PL/SQL 을 작성하여 구해야한다.

1. Single Record 에 대한 예

TO 테이블의 Description 컬럼 길이를 구하는 경우

$ vi len_long.sql
declare
  length_var NUMBER;
  cursor TOY_CURSOR is
    select * from TO;
  toy_val TOY_CURSOR%ROWTYPE;
begin
  open TOY_CURSOR;
  fetch TOY_CURSOR into toy_val;
    length_var := LENGTH(toy_val.Description);
    DBMS_OUTPUT.PUT_LINE('Length of Description: '||length_var);
  close TOY_CURSOR;
end;
/

SQL> set serveroutput on
SQL> @len_long
Length of description : 21

PL/SQL procedure successfully completed.

2. Multiple Record 에 대해서는 cursor FOR Loop 를 사용한다.

$ vi len_long.sql
declare
  length_var NUMBER;
  cursor TOY_CURSOR is
    select * from TOY;
  toy_val TOY_CURSOR%ROWTYPE;
begin
  for toy_val in TOY_CURSOR loop
    length_var := LENGTH(toy_val.Description);
    DBMS_OUTPUT.PUT_LINE('ID: '||toy_val.Toy_ID);
    DBMS_OUTPUT.PUT_LINE('Length of Description: '||length_var);
  end loop;
end;
/

SQL> set serveroutput on
SQL> @len_long  
ID: 1
Length of Description: 21
ID: 2
Length of Description: 27

PL/SQL procedure successfully completed.

댓글 없음:

댓글 쓰기