MSSQL.read_columns

MSSQL.read_columns(table_name, column_names, dtype=None, schema_name=None, chunk_size=None, **kwargs)

Read data of specific columns of a table.

Parameters
  • table_name (str) – name of a table in the currently-connected database

  • column_names (list or tuple) – column name(s) of the specified table

  • dtype (str or None) – data type, defaults to None; options include 'hierarchyid', 'varbinary' and 'geometry'

  • schema_name (str or None) – name of a schema, defaults to DEFAULT_SCHEMA when schema_name=None

  • chunk_size (int or None) – number of rows to include in each chunk (if specified), defaults to None

  • kwargs – [optional] parameters of pandas.read_sql

Returns

data of specific columns of the queried table

Return type

pandas.DataFrame

Examples:

>>> from pyhelpers.dbms import MSSQL
>>> from pyhelpers._cache import example_dataframe

>>> mssql = MSSQL()
Connecting <server_name>@localhost:1433/master ... Successfully.

>>> mssql.get_table_names()
['MSreplication_options',
 'spt_fallback_db',
 'spt_fallback_dev',
 'spt_fallback_usg',
 'spt_monitor']

>>> mssql.read_columns('MSreplication_options', column_names=['optname', 'value'])
          optname  value
0   transactional   True
1           merge   True
2  security_model   True

See also