将LONG转换为VARCHAR2或某些text数据类型

时间:2022-09-15 18:26:31

As we all know LONG is deprecated in Oracle a long back but Oracle itself is still using this datatype in their views.

我们都知道LONG在Oracle中被弃用了很长时间,但Oracle本身仍然在他们的视图中使用这种数据类型。

So if I have to change LONG into some kind of text datatype how can I achieve that.

因此,如果我必须将LONG更改为某种文本数据类型,我该如何实现。

I am trying to query this and getting error.

我试图查询这个并得到错误。

ORA-00932: inconsistent datatypes: expected - got LONG

Query -

查询 -

SELECT NVL(ie.column_expression, ic.column_name)
from user_ind_columns ic left join user_ind_expressions ie  on ic.index_name = ie.index_name and ic.table_name = ie.table_name 
where ic.table_name = 'Some Table'

2 个解决方案

#1


3  

There are several methods, one such is create table using TO_LOB. It is designed to convert a LONG or LONG RAW column to a CLOB or BLOB, respectively. Other methods are using PL/SQL, DBMS_XMLGEN. You can also use TO_LOB in insert statements.

有几种方法,一种是使用TO_LOB创建表。它旨在将LONG或LONG RAW列分别转换为CLOB或BLOB。其他方法使用PL / SQL,DBMS_XMLGEN。您还可以在insert语句中使用TO_LOB。

Let's see how to convert LONG into CLOB-

让我们看看如何将LONG转换为CLOB-

SQL> CREATE TABLE t (x INT, y LONG);

Table created.

SQL>
SQL> INSERT INTO t VALUES (1, RPAD('*',9,'!'));

1 row created.

SQL> INSERT INTO t VALUES (2, RPAD('*',9,'@'));

1 row created.

SQL> INSERT INTO t VALUES (3, RPAD('*',9,'#'));

1 row created.

SQL> COMMIT;

Commit complete.

SQL>

So, we have table t with column y s LONG data type.

因此,我们有一个表格,其中列为y的LONG数据类型。

SQL> CREATE TABLE t1
  2  AS
  3  SELECT * FROM t
  4  /
SELECT * FROM t
       *
ERROR at line 3:
ORA-00997: illegal use of LONG datatype

SQL>

We can see the LONG restriction.

我们可以看到LONG限制。

Let's use TO_LOB to convert it into CLOB.

让我们使用TO_LOB将其转换为CLOB。

SQL> CREATE TABLE t1
  2  AS
  3  SELECT x,
  4         to_lob(y) as y
  5  FROM t
  6  /

Table created.

SQL> desc t1;
 Name                                                  Null?    Type
 ----------------------------------------------------- -------- ------------------------------------
 X                                                              NUMBER(38)
 Y                                                              CLOB

SQL>

Now you have the same table with the LONG column converted to CLOB.

现在,您将LONG列转换为CLOB具有相同的表。

#2


1  

this is stupid (as in probably not efficient) but it works for samll lengths of y (ie < 2000 characters)..

这是愚蠢的(可能效率不高)但它适用于y的长度(即<2000个字符)..

CREATE TABLE t (x INT, y LONG);

INSERT INTO t VALUES (1, RPAD('*',9,'!'));

CREATE TABLE t1
AS
SELECT x,
       regexp_substr(SYS.DBMS_XMLGEN.GETXML('select y from t where rowid = '''||rowid||''''),'<Y>(.*)</Y>',1,1,'in',1) y
FROM t
/

it works by using dbms_xmlgen to generate a clob based on the LONG column.. then substr-ing the value back out.

它的工作原理是使用dbms_xmlgen根据LONG列生成clob ..然后将值重新输出。

this only works for small contents of the LONG column. but that is all i had and this worked for me.

这仅适用于LONG列的小内容。但这就是我所拥有的,这对我有用。

#1


3  

There are several methods, one such is create table using TO_LOB. It is designed to convert a LONG or LONG RAW column to a CLOB or BLOB, respectively. Other methods are using PL/SQL, DBMS_XMLGEN. You can also use TO_LOB in insert statements.

有几种方法,一种是使用TO_LOB创建表。它旨在将LONG或LONG RAW列分别转换为CLOB或BLOB。其他方法使用PL / SQL,DBMS_XMLGEN。您还可以在insert语句中使用TO_LOB。

Let's see how to convert LONG into CLOB-

让我们看看如何将LONG转换为CLOB-

SQL> CREATE TABLE t (x INT, y LONG);

Table created.

SQL>
SQL> INSERT INTO t VALUES (1, RPAD('*',9,'!'));

1 row created.

SQL> INSERT INTO t VALUES (2, RPAD('*',9,'@'));

1 row created.

SQL> INSERT INTO t VALUES (3, RPAD('*',9,'#'));

1 row created.

SQL> COMMIT;

Commit complete.

SQL>

So, we have table t with column y s LONG data type.

因此,我们有一个表格,其中列为y的LONG数据类型。

SQL> CREATE TABLE t1
  2  AS
  3  SELECT * FROM t
  4  /
SELECT * FROM t
       *
ERROR at line 3:
ORA-00997: illegal use of LONG datatype

SQL>

We can see the LONG restriction.

我们可以看到LONG限制。

Let's use TO_LOB to convert it into CLOB.

让我们使用TO_LOB将其转换为CLOB。

SQL> CREATE TABLE t1
  2  AS
  3  SELECT x,
  4         to_lob(y) as y
  5  FROM t
  6  /

Table created.

SQL> desc t1;
 Name                                                  Null?    Type
 ----------------------------------------------------- -------- ------------------------------------
 X                                                              NUMBER(38)
 Y                                                              CLOB

SQL>

Now you have the same table with the LONG column converted to CLOB.

现在,您将LONG列转换为CLOB具有相同的表。

#2


1  

this is stupid (as in probably not efficient) but it works for samll lengths of y (ie < 2000 characters)..

这是愚蠢的(可能效率不高)但它适用于y的长度(即<2000个字符)..

CREATE TABLE t (x INT, y LONG);

INSERT INTO t VALUES (1, RPAD('*',9,'!'));

CREATE TABLE t1
AS
SELECT x,
       regexp_substr(SYS.DBMS_XMLGEN.GETXML('select y from t where rowid = '''||rowid||''''),'<Y>(.*)</Y>',1,1,'in',1) y
FROM t
/

it works by using dbms_xmlgen to generate a clob based on the LONG column.. then substr-ing the value back out.

它的工作原理是使用dbms_xmlgen根据LONG列生成clob ..然后将值重新输出。

this only works for small contents of the LONG column. but that is all i had and this worked for me.

这仅适用于LONG列的小内容。但这就是我所拥有的,这对我有用。