Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

sql - Converting Long to Varchar2

I am trying to insert into varchar2 column from a long column. here is the below example, TEXT.TEXT_COL = VARCHAR2(4000) and NOTE.TEXT_NOTE = LONG.

INSERT INTO TEXT(ROW_ID, TEXT_COL)
SELECT 1, TEXT_NOTE FROM NOTE; 

When i run the above sql i get error

SQL Error: ORA-00997: illegal use of LONG datatype

I used TO_LOB() too, but still the same error.

Is there any function which simply coverts long and put it in varchar2. Let me know your thoughts.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Converting from long to varchar2 right away using a single statement is not possible, as long has certain restrictions.

You can either Create a temporary table or use PL/SQL code to solve your problem:

  • Temporary Table:

    CREATE TABLE TABLE2 AS SELECT TO_LOB(COLUMN1) COLUMN FROM TABLE1;

  • PL/SQL Code:

    DECLARE
      VAR1 LONG;
      VAR2 VARCHAR2(4000);
    BEGIN
      SELECT TEXT INTO VAR1 FROM USER_VIEWS WHERE ROWNUM = 1;  
      VAR2 := SUBSTR(VAR1, 1, 4000);
      DBMS_OUTPUT.PUT_LINE(VAR2);
    END;
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...