Home » SQL & PL/SQL » SQL & PL/SQL » LONG to VARCHAR2 conversion using dblink (Oracle Enterprise Edition : 10.2.0.5.0 - 64bit,HPUX: Version 10.2.0.5.0)
LONG to VARCHAR2 conversion using dblink [message #630239] Tue, 23 December 2014 07:44 Go to next message
jois
Messages: 12
Registered: October 2014
Junior Member
there are database : DB1 and DB2. DB1 has a table with long column. in DB2 need to create table(varchar2) from DB1 table (long) using DBLINK.
length of lob is <32 K.

Is there any other option to convert at DB2?

- cannot convert long to lob,later lob to varchar2 since we cant use to_lob over db link
- used plsql to convert long to varchar,but it is overrunning & ORA-12151: TNS:received bad packet type from network layer,for even 10 records with no results (may be due to dblink)

--table on DB1
CREATE TABLE orgnistaion_db1
(
   "DateCompleted"   DATE,
   "CompletedBy"     NUMBER (10),
   "COFSTAId"        NUMBER (10),
   "CONNId"          NUMBER (10),
   "CMUId"           NUMBER (10),
   "comment"         LONG
);


-- code to convert using plsql at DB2
DECLARE
   long_var   LONG;
   var_var    VARCHAR2 (2000);
BEGIN
   FOR i IN (SELECT "comment" FROM orgnistaion_db1@source_db_link)
   LOOP
      var_var := SUBSTR (i."comment", 1, 2000);

      INSERT INTO orgnistaion_db2 ("comment")
           VALUES (var_var);

      COMMIT;
   END LOOP;
END;


-- Dont have access to DB1,so unable to provide data.Table is having less than <20K records.

Thanks
Re: LONG to VARCHAR2 conversion using dblink [message #630240 is a reply to message #630239] Tue, 23 December 2014 07:49 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
https://docs.oracle.com/database/121/SQPUG/apb.htm#i641251
Re: LONG to VARCHAR2 conversion using dblink [message #630243 is a reply to message #630239] Tue, 23 December 2014 08:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Otherwise the conversion function should be in the same DB than the table with LONG.

Re: LONG to VARCHAR2 conversion using dblink [message #630250 is a reply to message #630240] Tue, 23 December 2014 12:28 Go to previous messageGo to next message
jois
Messages: 12
Registered: October 2014
Junior Member
Thanks BlackSwan,are you sugegsting to copy complete data set from DB1 to DB2 using COPY command & then to try converting long to varchar2 in DB2?

@Michael,conversion function is in DB2,where varchar2 is required.Thanks

[Updated on: Tue, 23 December 2014 12:30]

Report message to a moderator

Re: LONG to VARCHAR2 conversion using dblink [message #630251 is a reply to message #630250] Tue, 23 December 2014 12:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

NO, conversion function MUST be in the db with LONG.

Re: LONG to VARCHAR2 conversion using dblink [message #630252 is a reply to message #630250] Tue, 23 December 2014 12:41 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It is NOT clear to me why to are attempting to "convert long to varchar2".

COPY

Copies data from a query to a table in the same or another database. COPY supports the following datatypes:

CHAR
DATE
LONG
NUMBER
VARCHAR2

COPY allows you to move the data without any "conversion" required.
If you REALLY, Really, really want to convert LONG to VARCHAR2 then do it on the source DB before moving the data to the target DB
Re: LONG to VARCHAR2 conversion using dblink [message #630254 is a reply to message #630251] Tue, 23 December 2014 14:37 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Tue, 23 December 2014 13:39

NO, conversion function MUST be in the db with LONG.


Why? OP stated LONGs are < 32K. Therefore all OP needs is:

BEGIN
  FOR v_rec IN (SELECT * FROM orgnistaion_db1@source_db_link) LOOP
    INSERT
      INTO orgnistaion_db2
      VALUES (
              v_rec."DateCompleted",
              v_rec."CompletedBy",
              v_rec."COFSTAId",
              v_rec."CONNId",
              v_rec."CMUId",
              v_rec."comment"
             );
  END LOOP;
END;
/


For example:

SQL> connect scott@sol10
Enter password: *****
Connected.
SQL> create table tbl
  2    as
  3      select  owner,
  4              trigger_name,
  5              to_lob(trigger_body) trigger_body
  6        from  dba_triggers
  7  /

Table created.

SQL> with t as (
  2             select  owner,
  3                     trigger_name,
  4                     max(length(trigger_body)) len
  5               from  tbl
  6               group by owner,
  7                        trigger_name
  8               having max(length(trigger_body)) <= 32767
  9               order by 3 desc
 10            )
 11  select  *
 12    from  t
 13    where rownum <= 3
 14  /

OWNER                          TRIGGER_NAME                          LEN
------------------------------ ------------------------------ ----------
SYSMAN                         BLACKOUT_CHANGE                     16289
SYSMAN                         METRIC_ERRORS_CUR_AND_DUPES         12351
SYSMAN                         CHECK_DUPLICATE_TARGETS              6003

SQL> connect scott@sol11
Enter password: *****
Connected.
SQL> create table tbl(
  2                   owner varchar2(30),
  3                   trigger_name varchar2(30),
  4                   trigger_body clob
  5                  )
  6  /

Table created.

SQL> declare
  2      cursor v_cur
  3        is
  4          select  owner,
  5                  trigger_name,
  6                  trigger_body
  7            from  dba_triggers@sol10
  8            where owner = 'SYSMAN'
  9              and trigger_name in (
 10                                   'BLACKOUT_CHANGE',
 11                                   'METRIC_ERRORS_CUR_AND_DUPES',
 12                                   'CHECK_DUPLICATE_TARGETS'
 13                                  );
 14  begin
 15      for v_rec in v_cur loop
 16        insert
 17          into tbl
 18          values(
 19                 v_rec.owner,
 20                 v_rec.trigger_name,
 21                 v_rec.trigger_body
 22                );
 23      end loop;
 24  end;
 25  /

PL/SQL procedure successfully completed.

SQL> column owner format a6
SQL> column trigger_body format a40
SQL> select  *
  2    from  tbl
  3  /

OWNER  TRIGGER_NAME                   TRIGGER_BODY
------ ------------------------------ ----------------------------------------
SYSMAN BLACKOUT_CHANGE                DECLARE

                                          latest_availability_rowid ROWID;
                                          latest_availability_status NUM

SYSMAN CHECK_DUPLICATE_TARGETS        DECLARE
                                        l_cnt_url    NUMBER := 0;
                                        l_avail_cnt  NUMBER := 0;
                                        l_old_agent_na

SYSMAN METRIC_ERRORS_CUR_AND_DUPES    DECLARE

OWNER  TRIGGER_NAME                   TRIGGER_BODY
------ ------------------------------ ----------------------------------------

                                          current_availability_rowid ROWID;
                                          current_availability_status N


SQL> 


SY.
Re: LONG to VARCHAR2 conversion using dblink [message #630255 is a reply to message #630254] Tue, 23 December 2014 14:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Your source table contains a LOB not a LONG.
Your target table contains a LOB not a VARCHAR2.

So this does not apply to OP's case.

Re: LONG to VARCHAR2 conversion using dblink [message #630258 is a reply to message #630255] Tue, 23 December 2014 19:16 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Tue, 23 December 2014 15:43

So this does not apply to OP's case.


You misread my example. My source table is DBA_TRIGGERS with column TRIGGER_BODY of LONG datatype. Column TRIGGER_BODY contains text both less than 32K and greater that 32K. I needed values shorter that 32K. Since I can't apply function LENGTH to LONG column, I created another table with TRIGGER_BODY converted to CLOB and just to find 3 triggers with body shorter than 32K. Then I used these triggers in select from remote DBA_TRIGGERS. So my test is exactly what OP is trying to do - populate CLOB/VARCHAR2 column from remote LONG column (for VARCHAR2 LONG must not exceed 4000 bytes). Remote table/view has LONG column. Local table has CLOB column. PL/SQL reads remote LONG into PL/SQL LONG which is limited to 32K.

SY.
Re: LONG to VARCHAR2 conversion using dblink [message #630273 is a reply to message #630258] Wed, 24 December 2014 00:41 Go to previous messageGo to next message
jois
Messages: 12
Registered: October 2014
Junior Member
Thanks Solomon Yakobson. It should work ideally as you demonstrated with example. but

 select * from table_with_long_column@source_db_link where "column"=9159  
is throwing up following error message though it has only 5 records

ORA-00600: internal error code, arguments: [HO define: Long fetch], [], [], [], [], [], [], []

so what I found is,even
 select * from table_with_long_column@source_db_link 
is not working with ORA-12151: TNS:received bad packet type from network layer,so it is of no use writing code at DB2 to change the types.

I think as BlackSwan said,only way is to convert at DB1 itself
Re: LONG to VARCHAR2 conversion using dblink [message #630338 is a reply to message #630273] Wed, 24 December 2014 15:56 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Are both databases Oracle? This error in most cases is raised by HS connectivity or Oracle Gateway.

SY.
Re: LONG to VARCHAR2 conversion using dblink [message #630362 is a reply to message #630273] Thu, 25 December 2014 06:43 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Jay1 wrote on Wed, 24 December 2014 01:41
I think as BlackSwan said,only way is to convert at DB1 itself


OK. I don't have 2 HPUX 10G databases, but I have one 11G and one 10G. So I an pulling from 11G to 10G:

SQL> select  *
  2    from  v$version
  3  /

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bi
PL/SQL Release 10.2.0.2.0 - Production
CORE    10.2.0.2.0      Production
TNS for HPUX: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - Production

SQL> select  *
  2    from  v$version@link1
  3  /

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for HPUX: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

SQL> create table test_long(body long)
  2  /

Table created.

SQL> declare
  2      cursor v_cur
  3        is
  4          select  body
  5            from  hyper_user.hbr_allocprocs@link1;
  6  begin
  7      for v_rec in v_cur loop
  8        insert
  9          into test_long
 10          values(
 11                 v_rec.body
 12                );
 13      end loop;
 14  end;
 15  /

PL/SQL procedure successfully completed.

SQL> -- and to check how long LONGs are
SQL> create table tbl
  2    as
  3      select  to_lob(body) body
  4        from  test_long
  5  /

Table created.

SQL> select  length(body)
  2    from  tbl
  3  /

LENGTH(BODY)
------------
        1929
       12772
        3855
        3811
        4630
        6088
        4373
       13652
       13917
       15159
        5758

LENGTH(BODY)
------------
       13748
        5305
       14080
       14081
        1988
       13374
        5876
        4793
       19384
        4266
        6180

22 rows selected.

SQL> 


SY.

[Updated on: Thu, 25 December 2014 06:43]

Report message to a moderator

Re: LONG to VARCHAR2 conversion using dblink [message #630496 is a reply to message #630338] Mon, 29 December 2014 06:04 Go to previous message
jois
Messages: 12
Registered: October 2014
Junior Member
Hi Solomon Yakobson,sorry I was out of station,so could not reply immediately.The source data is coming from SQL Server (ntext column) --> Oracle 10g on windows (intermediate server since sqlserver canot connect to Oracle 10g on Unix)--> Oracle 10g on Unix.

Im getting these errors on Oracle 10g on Unix.Currently checking with DBA if we can change at Oracle 10g on windows(intermediate server)

[Updated on: Mon, 29 December 2014 06:10]

Report message to a moderator

Previous Topic: SQL query to calculate distance and update
Next Topic: How to get list of top 15 sql order by by buffer gets
Goto Forum:
  


Current Time: Tue Apr 23 22:37:47 CDT 2024