Home » SQL & PL/SQL » SQL & PL/SQL » COL_LENGTH SQL Server to Oracle
COL_LENGTH SQL Server to Oracle [message #118580] Fri, 06 May 2005 02:34 Go to next message
patrykp
Messages: 31
Registered: April 2005
Member
How migrate the procedure to Oracle?

CREATE PROCEDURE [dbo].[get_Maxdoclength] AS
--DECLARE @length varchar
SELECT COL_LENGTH ('TAB_Objects','Name') AS 'Maxlength'
GO

Best regards.
Patryk
Re: COL_LENGTH SQL Server to Oracle [message #118591 is a reply to message #118580] Fri, 06 May 2005 04:57 Go to previous message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
If I remember something from Sybase right, COL_LENGTH gets the
length of table column ?

SQL> create or replace function get_col_length (
  2   tabname varchar2,
  3   colname varchar2
  4  )
  5  return number
  6  is
  7   ln number;
  8  begin
  9   begin
 10    select data_length into ln from user_tab_cols where table_name = upper(tabname)
 11    and column_name = upper(colname);
 12   exception
 13    when no_data_found then
 14     ln := 0;
 15    when others then
 16     raise;
 17   end;
 18   return ln;
 19  end;
 20  /

Function created.

Elapsed: 00:00:00.01
SQL> var len number;
SQL> exec :len := get_col_length('emp','ename');

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00
SQL> print len

       LEN
----------
        10


Or you can use dbms_sql package:

SQL> create or replace function get_col_length2 (
  2   tabname varchar2,
  3   colname varchar2
  4  )
  5  return number
  6  is
  7   c integer;
  8   rec_tab dbms_sql.desc_tab;
  9   col_cnt integer;
 10  begin
 11   c := dbms_sql.open_cursor;
 12   dbms_sql.parse(c,'select ' || colname || ' from ' || tabname, dbms_sql.native);
 13   dbms_sql.describe_columns(c, col_cnt, rec_tab);
 14   dbms_sql.close_cursor(c);
 15   return rec_tab(1).col_max_len;
 16  end;
 17  /

Function created.

Elapsed: 00:00:00.01
SQL> exec :len := get_col_length2('emp','ename');

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00
SQL> print len

       LEN
----------
        10


Rgds.
Previous Topic: Sorting Problem (Order By) with Ref Cursor and UNION
Next Topic: please guide to how to create an account
Goto Forum:
  


Current Time: Sun Aug 31 18:18:03 CDT 2025