Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

columns

Return database table column names

Syntax

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

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

collapse all

Create a database connection conn using the native ODBC interface to the Microsoft® SQL Server® database. This code assumes that you are connecting to a data source named MS SQL Server with user name username and password pwd.

conn = database('MS SQL Server','username','pwd');

Retrieve the column names for each table in a catalog. Here, this code assumes that the database contains the catalog name toy_store.

catalog = 'toy_store';

columnlist = columns(conn,catalog)
columnlist = 

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

columns returns a cell array. The first column contains the table names as character vectors. The second column contains the corresponding column name lists as cell arrays.

Display the column names for the suppliers table.

columnlist{2,2}
ans = 

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

Close the database connection.

close(conn)

Create a database connection conn using the native ODBC interface to the Microsoft SQL Server database. This code assumes that you are connecting to a data source named MS SQL Server with user name username and password pwd.

conn = database('MS SQL Server','username','pwd');

Retrieve the column names for each table in a schema. Here, this code assumes that the database contains the catalog name toy_store and the schema name sch.

catalog = 'toy_store';
schema = 'sch';

columnlist = columns(conn,catalog,schema)
columnlist = 

    'inserttest'                  {1x3  cell}
    'inventoryTable'              {1x4  cell}
    'largedata'                   {1x9  cell}
    ...

columns returns a cell array. The first column contains the table names as character vectors. The second column contains the corresponding column name lists as cell arrays.

Display the column names for the inventoryTable table.

columnlist{2,2}
ans = 

    'productNumber'    'Quantity'    'Price'    'inventoryDate'

Close the database connection.

close(conn)

Create a database connection conn using the native ODBC interface to the Microsoft SQL Server database. This code assumes that you are connecting to a data source named MS SQL Server with user name username and password pwd.

conn = database('MS SQL Server','username','pwd');

Retrieve the column names in a database table. Here, this code assumes that the database contains the catalog name toy_store, the schema name sch, and the table name inventoryTable.

catalog = 'toy_store';
schema = 'sch';
tablename = 'inventoryTable';

columnlist = columns(conn,catalog,schema,tablename)
columnlist = 

    'productNumber'    'Quantity'    'Price'    'inventoryDate'

columns returns a cell array with the column names as character vectors.

Close the database connection.

close(conn)

Create a database connection conn. This 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. Here, this code assumes that the database contains the catalog name toy_store.

catalog = 'toy_store';

columnlist = columns(dbmeta,catalog)
columnlist = 

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

columns returns a cell array. The first column contains the table names as character vectors. The second column contains the corresponding column name lists as cell arrays.

Display the column names for the suppliers table.

columnlist{2,2}
ans = 

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

Close the database connection.

close(conn)

Create a database connection conn. This 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. Here, this code assumes that the database contains the catalog name toy_store and the schema name sch.

catalog = 'toy_store';
schema = 'sch';

columnlist = columns(dbmeta,catalog,schema)
columnlist = 

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

columns returns a cell array. The first column contains the table names as character vectors. The second column contains the corresponding column name lists as cell arrays.

Display the column names for the inventoryTable table.

columnlist{1,2}
ans = 

    'productNumber'    'Quantity'    'Price'    'inventoryDate'

Close the database connection.

close(conn)

Create a database connection conn. This 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. Here, this code assumes that the database contains the catalog name toy_store, the schema name sch, and the table name inventoryTable.

catalog = 'toy_store';
schema = 'sch';
tablename = 'inventoryTable';

columnlist = columns(dbmeta,catalog,schema,tablename)
columnlist = 

    'productNumber'    'Quantity'    'Price'    'inventoryDate'

columns returns a cell array with the column names as character vectors.

Close the database connection.

close(conn)

Input Arguments

collapse all

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

Database metadata, specified as a database metadata object created using dmd. To use this object, create a database connection using the database function first.

Database catalog name, specified as a character vector or string scalar.

Data Types: char | string

Database schema name, specified as a character vector or string scalar.

Data Types: char | string

Database table name, specified as a character vector or string scalar denoting the name of a table in your database.

Data Types: char | string

Output Arguments

collapse all

List of columns, returned as a cell array.

Introduced in R2010a

Was this topic helpful?