Neste post mostrarei como converter uma coluna do tipo Varchar2 para uma coluna do tipo LOB (BLOB ou CLOB)
Para converter uma coluna de tabela de varchar2 para blob ou clob faça o seguinte:
1 – VARCHAR2 -> BLOB
1.1 – Criando uma tabela de teste
SQL> create table teste (col1 varchar2 (1000));
Table created.
1.2 – Inserindo dados a tabela
SQL> insert into teste select table_name from user_tables;
9 rows created.
SQL> commit;
Commit complete.
1.3 – Verificando os dados na tabela
SQL> SELECT * FROM teste;
COL1
———————————
CUSTKEYS
PACKAGECLIS
TESTE
CC_PARAMS
CUSTCLIS
CUSTACCOUNTS
PACKAGESLOTS
CALLPACKAGES
DIALCODES
9 rows selected.
1.4 – Adicionando uma coluna do tipo lob na tabela(BLOB)
SQL> alter table teste add blob_col blob;
Table altered.
1.5 – Verificando a estrutura da tabela
SQL> desc teste
Name Null? Type
—————————————– ——– —————————-
COL1 VARCHAR2(1000)
BLOB_COL BLOB
1.6 – Inserindo dados a coluna tipo lob
SQL> update teste set blob_col = utl_raw.cast_to_raw(COL1);
9 rows updated.
commit;
1.7 – Verificando os dados na tabela
SQL> SELECT * FROM teste;
COL1 BLOB_COL
———— ——————————
CUSTKEYS 435553544B455953
PACKAGECLIS 5041434B414745434C4953
TESTE 5445535445
CC_PARAMS 43435F504152414D53
CUSTCLIS 43555354434C4953
CUSTACCOUNTS 435553544143434F554E5453
PACKAGESLOTS 5041434B414745534C4F5453
CALLPACKAGES 43414C4C5041434B41474553
DIALCODES 4449414C434F444553
9 rows selected.
1.8 – excluindo a coluna do tipo varchar2
SQL> alter table teste drop column col1 ;
Table altered.
1.9 – Renomeando a coluna do tipo lob para COL1
SQL> alter table teste rename column blob_col to col1;
Table altered.
1.10 – Verificando a estrutura da tabela
SQL> desc teste
Name Null? Type
——————- ——– ————–
COL1 BLOB
1.11 – Verificando os dados na tabela
SQL> select utl_raw.cast_to_varchar2(col1) from teste;
UTL_RAW.CAST_TO_VARCHAR2(COL1)
——————————–
CUSTKEYS
PACKAGECLIS
TESTE
CC_PARAMS
CUSTCLIS
CUSTACCOUNTS
PACKAGESLOTS
CALLPACKAGES
DIALCODES
9 rows selected.
Podemos observar que os dados são os mesmos exibidos no ítem 1.3 deste artigo
2 – VARCHAR2 -> CLOB
2.1 – Criando uma tabela de teste
SQL> create table teste1
(col1 varchar2 (1000)); 2
Table created.
2.2 – Inserindo dados a tabela
SQL> insert into teste1 select table_name from user_tables where rownum <5;
4 rows created.
SQL> commit;
Commit complete.
2.3 – Verificando os dados na tabela
SQL> SELECT * FROM teste1;
COL1
————
CUSTKEYS
PACKAGECLIS
TESTE1
T
2.4 – Adicionando uma coluna do tipo lob na tabela(CLOB)
SQL> alter table teste1 add clob_col clob;
Table altered.
2.5 Verificando a estrutura da tabela
SQL> desc teste1
Name Null? Type
——————- ——– ——————
COL1 VARCHAR2(1000)
CLOB_COL CLOB
2.6 – Inserindo dados a coluna tipo LOB
SQL> update teste1 set clob_col = COL1;
4 rows updated.
SQL> commit ;
Commit complete.
2.7 – Verificando os dados na tabela
SQL> select * from teste1 ;
COL1 CLOB_COL
——————– ——————–
CUSTKEYS CUSTKEYS
PACKAGECLIS PACKAGECLIS
TESTE1 TESTE1
T T
2.8 – Excluindo a coluna do tipo varchar2
SQL> alter table teste1 drop column COL1;
Table altered.
2.9 – Renomeando a coluna do tipo lob para COL1
SQL> alter table teste1 rename column CLOB_COL to COL1;
Table altered.
2.10 – Verificando a estrutura da tabela
SQL> desc teste1
Name Null? Type
——————- ——– ——————
COL1 CLOB
2.11 – Verificando os dados na tabela
SQL> select * from teste1;
COL1
——————–
CUSTKEYS
PACKAGECLIS
TESTE1
T
Podemos observar que os dados são os mesmos exibidos no ítem 2.3 deste artigo
Um abraço;