[sql] From a Sybase Database, how I can get table description ( field names and types)?

I have access to command line isql and I like to get Meta-Data of all the tables of a given database, possibly in a formatted file. How I can achieve that?

Thanks.

This question is related to sql metadata sybase sap-ase isql

The answer is


sp_help is what you're looking for.

From Sybase online documentation on the sp_help system procedure:

Description

Reports information about a database object (any object listed in sysobjects) and about system or user-defined datatypes, as well as computed columns and function-based indexes. Column displays optimistic_index_lock.

Syntax

sp_help [objname]

[...]

Here is the (partial) output for the publishers table (pasted from Using sp_help on database objects):

Name               Owner        Object_type     Create_date 
----------------   -----------  -------------   ------------------------------
publishers         dbo          user table      Nov 9 2004 9:57AM

(1 row affected)
Column_name Type     Length   Prec  Scale   Nulls   Default_name   Rule_name
----------- -------  ------   ----- ------- ------- -------------- ---------- 
pub_id      char          4    NULL  NULL        0  NULL           pub_idrule
pub_name    varchar      40    NULL  NULL        1  NULL           NULL
city        varchar      20    NULL  NULL        1  NULL           NULL
state       char          2    NULL  NULL        1  NULL           NULL
Access_Rule_name    Computed_Column_object     Identity
------------------- -------------------------  ------------
NULL                NULL                                  0
NULL                NULL                                  0
NULL                NULL                                  0
NULL                NULL                                  0

Still quoting Using sp_help on database objects:

If you execute sp_help without supplying an object name, the resulting report shows each object in sysobjects, along with its name, owner, and object type. Also shown is each user-defined datatype in systypes and its name, storage type, length, whether null values are allowed, and any defaults or rules bound to it. The report also notes if any primary or foreign key columns have been defined for a table or view.


Sybase IQ:

describe table_name;

If you want to use a command line program, but are not restricted to using SQL, you can use SchemaCrawler. SchemaCrawler is open source, and can produce files in plain text, CSV, or (X)HTML formats.


sp_tables will also work in isql. It gives you the list of tables in the current database.


For Sybase ASE, sp_columns table_name will return all the table metadata you are looking for.


Here a different approach to get meta data. This very helpful SQL command returns you the table / view definition as text:

SELECT text FROM syscomments WHERE id = OBJECT_ID('MySchema.MyTable') ORDER BY number, colid2, colid

Enjoy Patrick


If Sybase is SQL-92 compliant then this information is stored within the INFORMATION_SCHEMA tables.

So the following will give you a list of tables and views in any SQL-92 compliant database

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES

When finding user table, in case if want the table owner name also, you can use the following:

select su.name + '.' + so.name
from   sysobjects so,
       sysusers   su
where  so.type = 'U' and
       so.uid  = su.uid
order  by su.name,
          so.name

You can search for column in all tables in database using:

SELECT so.name 
FROM sysobjects so
INNER JOIN syscolumns sc ON so.id = sc.id 
WHERE sc.name = 'YOUR_COLUMN_NAME'

In the Sybase version I use, the following gives list of columns for selected table

select *
FROM sys.syscolumns sc
where tname = 'YOUR_TABLE_NAME'
--and creator='YOUR_USER_NAME' --if you want to further restrict tables
--according to the user name that created it

     SELECT
DB_NAME() TABLE_CATALOG,
NULL TABLE_SCHEMA,
so.name TABLE_NAME,
sc.name COLUMN_NAME,
sc.colid ORDINAL_POSITION,
NULL COLUMN_DEFAULT,
CASE WHEN st.allownulls=1 THEN 'YES'
 ELSE 'NO'
END IS_NULLABLE,
st.name DATA_TYPE,
CASE WHEN st.name like '%char%' THEN st.length
END CHARACTER_MAXIMUM_LENGTH,
CASE WHEN st.name like '%char%' THEN st.length
END*2 CHARACTER_OCTET_LENGTH,
CASE WHEN st.name in ('numeric','int') THEN st.length
END NUMERIC_MAXIMUM_LENGTH,
CASE WHEN st.name in ('numeric','int') THEN st.prec
END NUMERIC_PRECISION,
NULL NUMERIC_PRECISION_RADIX,
CASE WHEN st.name in ('numeric','int') THEN st.scale
END NUMERIC_SCALE,
CASE WHEN st.name in ('datetime') THEN st.prec
END DATETIME_PRECISION,
NULL CHARACTER_SET_CATALOG,
NULL CHARACTER_SET_SCHEMA,
NULL COLLATION_CATALOG,
NULL COLLATION_SCHEMA,
NULL DOMAIN_CATALOG,
NULL DOMAIN_SCHEMA,
NULL DOMAIN_NAME
FROM 
sysobjects so
INNER JOIN 
syscolumns sc
ON sc.id = so.id
inner join systypes st on st.usertype = sc.usertype 
WHERE so.name = 'TableName'

Examples related to sql

Passing multiple values for same variable in stored procedure SQL permissions for roles Generic XSLT Search and Replace template Access And/Or exclusions Pyspark: Filter dataframe based on multiple conditions Subtracting 1 day from a timestamp date PYODBC--Data source name not found and no default driver specified select rows in sql with latest date for each ID repeated multiple times ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database

Examples related to metadata

VS 2017 Metadata file '.dll could not be found Clean out Eclipse workspace metadata Retrieving and Saving media metadata using FFmpeg How to query the permissions on an Oracle directory? How to find available directory objects on Oracle 11g system? How do I find the date a video (.AVI .MP4) was actually recorded? Sql Query to list all views in an SQL Server 2005 database How do I list all tables in all databases in SQL Server in a single result set? List of foreign keys and the tables they reference in Oracle DB From a Sybase Database, how I can get table description ( field names and types)?

Examples related to sybase

Convert INT to VARCHAR SQL From a Sybase Database, how I can get table description ( field names and types)? Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF Difference between drop table and truncate table? How to convert a string to a date in sybase

Examples related to sap-ase

How do I find out what version of Sybase is running From a Sybase Database, how I can get table description ( field names and types)?

Examples related to isql

From a Sybase Database, how I can get table description ( field names and types)?