columns

Return database table column names

Syntax

  • columnlist = columns(conn,catalog) example
  • columnlist = columns(conn,catalog,schema) example
  • columnlist = columns(conn,catalog,schema,tablename) example
  • columnlist = columns(dbmeta,catalog) example
  • columnlist = columns(dbmeta,catalog,schema) example
  • columnlist = columns(dbmeta,catalog,schema,tablename) example

Description

example

columnlist = columns(conn,catalog) returns a list of all column names in the catalog catalog for the database with the database connection conn.

example

columnlist = columns(conn,catalog,schema) returns a list of all column names in the schema schema.

example

columnlist = columns(conn,catalog,schema,tablename) returns a list of all column names for the table tablename.

example

columnlist = columns(dbmeta,catalog) returns a list of all column names in the catalog catalog for the database whose database metadata object is dbmeta.

example

columnlist = columns(dbmeta,catalog,schema) returns a list of all column names in the schema schema.

example

columnlist = columns(dbmeta,catalog,schema,tablename) returns a list of all column names for the table tablename.

Examples

expand all

Retrieve the Column List for a Catalog Using the Database Connection

Create a database connection conn. For example, the following code uses database name dbname, user name username, password pwd, database server name sname, and port number 123456 to connect to a Microsoft® SQL Server® database.

conn = database('dbname','username','pwd',...
                'Vendor','Microsoft SQL Server',...
                'Server','sname',...
                'portnumber',123456);

Retrieve the column names for each table in a catalog using the catalog name toy_store.

columnlist = columns(conn,'toy_store')
columnlist = 

    'salesVolume'                                  {1x13  cell}
    'suppliers'                                    {1x5   cell}
    'yearlySales'                                  {1x3   cell}
    ...

columns returns a cell array where the first column contains the table names as strings and the second column contains the corresponding column name lists as cell arrays.

Display the column names for the suppliers table.

 columnlist{1,2}
columnlist = 

    'SupplierNumber'    'SupplierName'    'City'    'Country'    'FaxNumber'

Close the database connection.

close(conn)

Retrieve the Column List for a Catalog and Schema Using the Database Connection

Create a database connection conn. For example, the following code uses database name dbname, user name username, password pwd, database server name sname, and port number 123456 to connect to a Microsoft SQL Server database.

conn = database('dbname','username','pwd',...
                'Vendor','Microsoft SQL Server',...
                'Server','sname',...
                'portnumber',123456);

Retrieve the column names for each table in a schema using the catalog name toy_store and the schema name schema.

columnlist = columns(conn,'toy_store','schema')
columnlist = 

    'inventoryTable'              {1x3  cell}
    'invoice'                     {1x5  cell}
    'productTable'                {1x5  cell}
    ...

columns returns a cell array where the first column contains the table names as strings and the second column contains the corresponding column name lists as cell arrays.

Display the column names for the inventoryTable table.

 columnlist{1,2}
columnlist = 

    'productNumber'    'Quantity'    'Price'

Close the database connection.

close(conn)

Retrieve the Column List for a Catalog, Schema, and Table Name Using the Database Connection

Create a database connection conn. For example, the following code uses database name dbname, user name username, password pwd, database server name sname, and port number 123456 to connect to a Microsoft SQL Server database.

conn = database('dbname','username','pwd',...
                'Vendor','Microsoft SQL Server',...
                'Server','sname',...
                'portnumber',123456);

Retrieve the column names in a database table using the catalog name toy_store, the schema name schema, and the table name inventoryTable.

columnlist = columns(conn,'toy_store','schema','inventoryTable')
columnlist = 

    'productNumber'    'Quantity'    'Price'

columns returns a cell array with the column names as strings.

Close the database connection.

close(conn)

Retrieve the Column List for a Catalog Using the Database Metadata Object

Create a database connection conn. For example, the following code uses database name dbname, user name username, password pwd, database server name sname, and port number 123456 to connect to a Microsoft SQL Server database.

conn = database('dbname','username','pwd',...
                'Vendor','Microsoft SQL Server',...
                'Server','sname',...
                'portnumber',123456);

Create the database metadata object dbmeta.

dbmeta = dmd(conn);

Retrieve the column names for each table in a catalog using the catalog name toy_store.

columnlist = columns(dbmeta,'toy_store')
columnlist = 

    'salesVolume'                                  {1x13  cell}
    'suppliers'                                    {1x5   cell}
    'yearlySales'                                  {1x3   cell}
    ...

columns returns a cell array where the first column contains the table names as strings and the second column contains the corresponding column name lists as cell arrays.

Display the column names for the suppliers table.

 columnlist{1,2}
columnlist = 

    'SupplierNumber'    'SupplierName'    'City'    'Country'    'FaxNumber'

Close the database connection.

close(conn)

Retrieve the Column List for a Catalog and Schema Using the Database Metadata Object

Create a database connection conn. For example, the following code uses database name dbname, user name username, password pwd, database server name sname, and port number 123456 to connect to a Microsoft SQL Server database.

conn = database('dbname','username','pwd',...
                'Vendor','Microsoft SQL Server',...
                'Server','sname',...
                'portnumber',123456);

Create the database metadata object dbmeta.

dbmeta = dmd(conn);

Retrieve the column names for each table in a schema using the catalog name toy_store and the schema name schema.

columnlist = columns(dbmeta,'toy_store','schema')
columnlist = 

    'inventoryTable'              {1x3  cell}
    'invoice'                     {1x5  cell}
    'productTable'                {1x5  cell}
    ...

columns returns a cell array where the first column contains the table names as strings and the second column contains the corresponding column name lists as cell arrays.

Display the column names for the inventoryTable table.

 columnlist{1,2}
columnlist = 

    'productNumber'    'Quantity'    'Price'

Close the database connection.

close(conn)

Retrieve the Column List for a Catalog, Schema, and Table Name Using the Database Metadata Object

Create a database connection conn. For example, the following code uses database name dbname, user name username, password pwd, database server name sname, and port number 123456 to connect to a Microsoft SQL Server database.

conn = database('dbname','username','pwd',...
                'Vendor','Microsoft SQL Server',...
                'Server','sname',...
                'portnumber',123456);

Create the database metadata object dbmeta.

dbmeta = dmd(conn);

Retrieve the column names in a database table using the catalog name toy_store, the schema name schema, and the table name inventoryTable.

columnlist = columns(dbmeta,'toy_store','schema','inventoryTable')
columnlist = 

    'productNumber'    'Quantity'    'Price'

columns returns a cell array with the column names as strings.

Close the database connection.

close(conn)

Input Arguments

expand all

conn — Database connectionconnection object

Database connection, specified as a database connection object created using database.

dbmeta — Database metadataobject

Database metadata, specified as a database metadata object created using dmd.

catalog — Database catalog namestring

Database catalog name, specified as a string.

Data Types: char

schema — Database schema namestring

Database schema name, specified as a string.

Data Types: char

tablename — Database table namestring

Database table name, specified as a string denoting the name of a table in your database.

Data Types: char

Output Arguments

expand all

columnlist — List of columnscell array

List of columns, returned as a cell array.

Was this topic helpful?