29

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.

Robert
  • 24,191
  • 8
  • 59
  • 72
aartist
  • 2,853
  • 3
  • 29
  • 28
  • I should mention that I know almost nothing about sybase database. I am looking for sql command. – aartist Sep 15 '09 at 22:19

12 Answers12

67

Check sysobjects and syscolumns tables.

Here is a diagram of Sybase system tables.

List of all user tables:

SELECT * FROM sysobjects WHERE type = 'U'

You can change 'U' to other objects:

  • C – computed column
  • D – default
  • F – SQLJ function
  • L – log
  • N – partition condition
  • P – Transact-SQL or SQLJ procedure
  • PR – prepare objects (created by Dynamic SQL)
  • R – rule
  • RI – referential constraint
  • S – system table
  • TR – trigger
  • U – user table
  • V – view
  • XP – extended stored procedure

List of columns in a table:

SELECT sc.* 
FROM syscolumns sc
INNER JOIN sysobjects so ON sc.id = so.id
WHERE so.name = 'my_table_name'
Hend
  • 3
  • 2
Lukasz Lysik
  • 10,012
  • 3
  • 46
  • 68
  • 1
    This is very close for single table. I am looking for the same data on all the tables of a database. – aartist Sep 16 '09 at 12:14
  • 1
    Old question I know, but how do I get the same for a temporary table? I can't find it in sysobjects or syscolumns. – 0fnt Dec 27 '12 at 06:39
  • Replace `SELECT *` by `SELECT sc.*` if you lack permissions on some columns on sysobjects (like I do). – J.N. Jan 21 '14 at 07:54
  • The poster link is broken. Sybase is definitely abandon-ware at this point in time (this is obviously not only on account of that). – Marcus Junius Brutus Jan 17 '17 at 17:01
49

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.

Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
  • 4
    Why hasn't this answer received more upvotes? This is the perfect solution. – Nicolai Oct 07 '10 at 15:26
  • I like this solution more than querying the proprietary dictionary tables, as it seems simpler and more robust. Do you happen to know [how to properly fetch all result sets from JDBC](http://stackoverflow.com/questions/7298719/calling-sybase-adaptive-server-enterprises-sp-help-from-jdbc) – Lukas Eder Sep 04 '11 at 11:13
  • 3
    link no longer works for sp_help. Tried it and my version of sybase says "Error: Procedure 'sp_help' not found (DBD: prepare failed)" – Joel May 15 '17 at 14:19
7

Sybase IQ:

describe table_name;
Serhii Ziniuk
  • 87
  • 1
  • 2
  • Adding some explanation of code is always desirable in a good answer. See @Pascal's answer for a great answer, where he includes links to the documentation, and copies the relevant information here. – Heretic Monkey Dec 17 '14 at 17:30
  • Hi, I think that the "describe" command not exists in Sybase IQ – Jérôme B Feb 14 '17 at 15:09
5
     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'
Kishore
  • 51
  • 1
  • 2
2

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
shanmugs
  • 21
  • 1
2

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'
MDn
  • 21
  • 1
1

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

Chris Morgan
  • 2,050
  • 16
  • 19
0

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.

Sualeh Fatehi
  • 4,362
  • 2
  • 21
  • 27
0

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

Patrick Wolf
  • 2,370
  • 2
  • 27
  • 27
0

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

hui chen
  • 546
  • 8
  • 11
-1

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
Steve Weet
  • 27,020
  • 11
  • 66
  • 86
  • Unfortuantely, that's not the case... Both Sybase ASE and Sybase SQL Anywhere ship with their own proprietary dictionary views – Lukas Eder Sep 04 '11 at 09:27
  • @Lukas. I beg to differ. There is nothing wrong with what I have said. What follows from your statement is that Sybase is not SQL-92 compliant, not that my statement is incorrect. – Steve Weet Sep 05 '11 at 10:35
  • 1
    ;-) As you wish. But the question really was about Sybase, not SQL 92... Actually it was about Sybase ASE, even if that was not explicitly stated by the OP... – Lukas Eder Sep 05 '11 at 15:03
  • 1
    I think you both are right - Lukas' "that's not the case" could refer to Steve's "If Sybase is SQL-92 compliant" and yes, the OP won't be able to get the info he wants using Steve's query....Shame though that Sybase (and other vendors) hasn't implemented this yet - – reiniero Apr 19 '12 at 09:57
-1

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
Orhan Celik
  • 1,314
  • 13
  • 11
  • The version I used is SAP IQ 16 (New name for Sybase) select @@version; @@version SAP IQ/16.0.102.6.1386/20147/P/sp10.15... – Orhan Celik Oct 03 '17 at 16:40
  • you're confusing a couple things ... 1) Sybase was the name of a company which had 4 different RDBMS products (ASE, SQLAnywhere, IQ, Advantage) ... 2) approximately 8 years ago SAP bought Sybase, and over these last 8 years SAP has slowly replaced the 'Sybase' name with 'SAP'; the OP has tagged the question with 'sybase-ase' meaning 'Sybase ASE`; nowadays 'Sybase ASE' goes by a couple names ... 'Sybase ASE' by us die-hard ASE users and 'SAP ASE' by everyone else; Sybase ASE (aka SAP ASE) is not the same as Sybase IQ (aka SAP IQ), ie, ASE and IQ are 2 different RDBMS products – markp-fuso Oct 03 '17 at 16:44
  • Thanks for the background info. As a result, the Sybase product I use is SAP IQ (Sybase IQ), not the ASE version. – Orhan Celik Oct 03 '17 at 16:47