ORACLE long type converted into VARCHAR2

Source: Internet
Author: User
Tags chr
The long type is strongly deprecated in Oracle, but in order to solve the immediate need, it is forced to use a long type, simple ah, direct insert on the line. However, it is not thought that the long type does not support like or directly to_char.
Regret, want to change the long back to the VARCHAR2 type, but can not directly modify, otherwise it will be an error, reminders must be empty data, in order not to empty the data, comprehensive information on the network summarized the following solutions:
The message type in table jivemsg long needs to be replaced by VARCHAR2:

Original JIVEMSG table structure:
CREATE TABLE "Jivemsg"
("URI" VARCHAR2 (BYTE) DEFAULT ' not NULL ENABLE.
"SENDER" VARCHAR2 (BYTE) DEFAULT ',
"RECEIVER" VARCHAR2 (BYTE) DEFAULT ',
"CreationDate" VARCHAR2 (BYTE) DEFAULT ',
"MessageType" VARCHAR2 (BYTE) DEFAULT ',
"Messagesize" number (*,0) DEFAULT 0,
"Message" LONG,
"Fkdomain" VARCHAR2 (BYTE) DEFAULT NULL,
"VERSION" VARCHAR2 (BYTE) DEFAULT NULL,
"FLAGS" VARCHAR2 (BYTE) DEFAULT NULL,
"Receivername" VARCHAR2 (BYTE) DEFAULT NULL,
"SenderName" VARCHAR2 (BYTE) DEFAULT NULL,
CONSTRAINT "JIVEMSG_PK" PRIMARY KEY ("URI")
);

-------------------------------------------------------------------------

Conversion steps:

1: Create JIVEMSG2 table:
CREATE TABLE "JIVEMSG2"
("URI" VARCHAR2 (BYTE) DEFAULT ' not NULL ENABLE.
"SENDER" VARCHAR2 (BYTE) DEFAULT ',
"RECEIVER" VARCHAR2 (BYTE) DEFAULT ',
"CreationDate" VARCHAR2 (BYTE) DEFAULT ',
"MessageType" VARCHAR2 (BYTE) DEFAULT ',
"Messagesize" number (*,0) DEFAULT 0,
"Message" LONG,
"Fkdomain" VARCHAR2 (BYTE) DEFAULT NULL,
"VERSION" VARCHAR2 (BYTE) DEFAULT NULL,
"FLAGS" VARCHAR2 (BYTE) DEFAULT NULL,
"Receivername" VARCHAR2 (BYTE) DEFAULT NULL,
"SenderName" VARCHAR2 (BYTE) DEFAULT NULL,
CONSTRAINT "JIVEMSG2_PK" PRIMARY KEY ("URI")
);
2: Modify the type of table JIVEMSG2
ALTER TABLE JIVEMSG2 MODIFY ("message" VARCHAR2 (4000 CHAR));

3: Import the data from the table jivemsg into the table JIVEMSG2
This step is critical because the long type cannot be converted directly to VARCHAR2.
So this side uses a function written by someone else to solve.

INSERT into JIVEMSG2 Select Uri,sender,receiver,creationdate,messagetype,messagesize, Long_to_char (rowid, ' ucstar6 ', ' Jivemsg ', ' message '-Fkdomain,version,flags,receivername,sendername from jivemsg msg;


Function:
* * Where In_rowid is a row id,in_owner the account name for the database, In_table_name is the database table name, in_column a long type of table field name for the database * *
CREATE OR REPLACE FUNCTION Long_to_char (In_rowid rowid,in_owner
Varchar,in_table_name varchar,in_column varchar2)
return varchar AS
TEXT_C1 varchar2 (32767);
Sql_cur VARCHAR2 (2000);
--
Begin
Sql_cur: = ' SELECT ' | | in_column| | ' from
'|| in_owner| | '. ' | | in_table_name| | ' WHERE rowID =
'|| Chr (39) | | in_rowid| | Chr (39);
Dbms_output.put_line (sql_cur);
Execute immediate sql_cur into TEXT_C1;

TEXT_C1: = substr (TEXT_C1, 1, 4000);
return TEXT_C1;
End;
/

4: Delete the table jivemsg and rename jivemsg2 to Jivemsg
DROP TABLE jivemsg;
RENAME JIVEMSG2 to Jivemsg;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.