dotConnect for Snowflake Documentation
On this page
    Using GetSchema Method
    On this page

    dotConnect for Snowflake provides the GetSchema method for getting Snowflake metadata. The method lets you read schema information without writing queries and parsing the output: it returns information on schemas, tables, views, columns, and keys in an easy-to-process format. The method is introduced in System.Data.Common.DbConnection.

    The metadata is read from the INFORMATION_SCHEMA views and with the SHOW commands of the database specified in the Database connection string parameter. Objects of the INFORMATION_SCHEMA schema itself are not returned.

    How To Use

    The GetSchema method is available in three overloads. All overloads return a System.Data.DataTable object that contains information about schema elements.

    public override DataTable GetSchema();
    
    Public Overloads Overrides Function GetSchema() As DataTable
    

    If you call the GetSchema method without parameters, or with the single parameter "MetaDataCollections" (which is actually the same), the method returns the list of metadata collections. The first column of every row is a collection name that you can pass to the method as the collectionName argument. The second column is the number of restrictions for this collection.

    public override DataTable GetSchema(string collectionName);
    
    Public Overloads Overrides Function GetSchema(ByVal collectionName As String) As DataTable
    

    GetSchema with one argument returns general information about the collection queried. For example, GetSchema("Tables") returns the list of the tables in the current schema of the session if the Schema connection string parameter is specified, or in all schemas of the database if Schema is not specified.

    public override DataTable GetSchema(string collectionName, string[] restrictionValues);
    
    Public Overloads Overrides Function GetSchema(ByVal collectionName As String, ByVal restrictionValues() As String) As DataTable
    

    In this overload, the first parameter is the name of a collection, and the second parameter is an array of restrictions that specify which subset of the collection is returned. The first restriction of every collection, except the ones that have no restrictions, is the schema name, followed by the table name and the column or key name. A null restriction value means that no restriction is applied. If the second parameter is null/Nothing, it is ignored.

    Note the following when you specify restrictions:

    Instead of specifying the metadata collection name as a string constant, you can use the members of System.Data.Common.DbMetaDataCollectionNames and Devart.Data.Snowflake.SnowflakeMetaDataCollectionNames as the first GetSchema argument value. The members of these classes are string fields; each field stores the corresponding metadata collection name. This allows misspellings to be caught at compile time and enables IntelliSense to suggest all available metadata collection names.

    GetSchema Method Reference

    The following table provides detailed information on metadata collections that can be retrieved using the GetSchema method, and restrictions that can be applied for them.

    Collection Name

    Number of restrictions

    Return value

    Restriction descriptions

    MetaDataCollections

    0

    Returns the list of metadata collections. 
    DataSourceInformation

    0

    Returns information about the data source, for example, the product name and version. 
    DataTypes

    0

    Returns information about the data types that the provider supports. 
    Restrictions

    0

    Returns the restrictions of every metadata collection. 
    ReservedWords

    0

    Returns the list of reserved words. 
    Schemas

    1

    Returns the list of schemas in the database.
    1. Schema name
    Tables

    2

    Returns the list of tables. By default, views (the VIEW table type) are not included; use the Views collection to get them, or set the table type restriction to ALL. Tables of other types, such as materialized views and external tables, are returned by default.
    1. Schema name
    2. Table name
    3. Table type: a value of the TABLE_TYPE column of INFORMATION_SCHEMA.TABLES, for example, BASE TABLE or TEMPORARY TABLE, or ALL to return tables and views. This third restriction is optional: it is not counted in the MetaDataCollections collection and is not listed in the Restrictions collection.
    Views

    2

    Returns the list of views.
    1. Schema name
    2. View name
    Columns

    3

    Returns the list of columns of tables and views.
    1. Schema name
    2. Table or view name
    3. Column name
    PrimaryKeys

    3

    Returns the list of primary keys.
    1. Schema name
    2. Table name
    3. Primary key name
    PrimaryKeyColumns

    3

    Returns the list of primary key columns.
    1. Schema name
    2. Table name
    3. Primary key name
    UniqueKeys

    3

    Returns the list of unique keys.
    1. Schema name
    2. Table name
    3. Unique key name
    UniqueKeyColumns

    3

    Returns the list of unique key columns.
    1. Schema name
    2. Table name
    3. Unique key name
    ForeignKeys

    6

    Returns the list of foreign keys.
    1. Schema name
    2. Table name
    3. Foreign key name
    4. Referenced schema name
    5. Referenced table name
    6. Referenced constraint name
    ForeignKeyColumns

    6

    Returns the list of foreign key columns.
    1. Schema name
    2. Table name
    3. Foreign key name
    4. Referenced schema name
    5. Referenced table name
    6. Referenced constraint name

    For standard tables, Snowflake stores the definitions of primary, unique, and foreign keys but doesn't enforce them. The key collections return these definitions.

    GetSchema Resultset Columns

    The tables below list the columns of the collections with their names in the case in which dotConnect for Snowflake returns them: most of the names are in uppercase. The DataTable and DataRow classes find columns by name case-insensitively, so, for example, row["Name"] and row["NAME"] return the same value.

    Schemas

    Column Name

    Description

    NAMEThe name of the schema.
    OWNERThe role that owns the schema.
    ISTRANSIENTIndicates whether the schema is transient.
    ISMANAGEDACCESSIndicates whether the schema is a managed access schema.
    RETENTIONTIMEThe number of days that historical data is retained for Time Travel.
    CREATEDThe date and time when the schema was created.
    LASTALTEREDThe date and time when the schema was last altered.
    COMMENTThe comment for the schema.

    Tables

    Column Name

    Description

    SCHEMAThe name of the schema that contains the table.
    NAMEThe name of the table.
    OWNERThe role that owns the table.
    TYPEThe type of the table, for example, BASE TABLE or TEMPORARY TABLE.
    ROWCOUNTThe number of rows in the table.
    BYTESThe number of bytes accessed by a scan of the table.
    ISTRANSIENTIndicates whether the table is transient.
    CLUSTERINGKEYThe clustering key of the table.
    AUTOCLUSTERINGONIndicates whether automatic clustering is enabled for the table.
    RETENTIONTIMEThe number of days that historical data is retained for Time Travel.
    CREATEDThe date and time when the table was created.
    LASTALTEREDThe date and time when the table was last altered.
    COMMENTThe comment for the table.

    Views

    Column Name

    Description

    SCHEMAThe name of the schema that contains the view.
    NAMEThe name of the view.
    OWNERThe role that owns the view.
    DEFINITIONThe text of the query that defines the view.
    ISSECUREIndicates whether the view is secure.
    CREATEDThe date and time when the view was created.
    LASTALTEREDThe date and time when the view was last altered.
    COMMENTThe comment for the view.

    Columns

    Column Name

    Description

    SCHEMAThe name of the schema that contains the table or view.
    TableThe name of the table or view.
    NAMEThe name of the column.
    POSITIONThe position of the column in the table, starting from 1.
    NULLABLEIndicates whether the column can contain NULL values.
    DATATYPEThe Snowflake data type of the column, for example, TEXT, BOOLEAN, or TIMESTAMP_NTZ. NUMBER columns are returned as FIXED, and FLOAT columns are returned as REAL.
    LENGTHThe maximum length of a text or binary column. For a BINARY column without a declared length, it is 8388608. For VARIANT, OBJECT, ARRAY, GEOGRAPHY, GEOMETRY, and VECTOR columns, it is 16777216.
    PrecisionThe precision of a numeric column. For a FLOAT column, it is 38.
    SCALEThe scale of a numeric column. For a FLOAT column, it is 19. For TIME, TIMESTAMP_NTZ, TIMESTAMP_LTZ, and TIMESTAMP_TZ columns, it is the precision of fractional seconds, from 0 to 9.
    COLLATIONThe collation of a text column.
    DefaultThe default value of the column.
    ISIDENTITYIndicates whether the column is an identity column.
    IDENTITYSTARTThe start value of an identity column.
    IDENTITYINCREMENTThe increment of an identity column.
    COMMENTThe comment for the column.
    ProviderTypeThe numeric representation of the System.Data.DbType value of the column.
    NativeTypeThe numeric representation of the SnowflakeType value of the column. For more information, see Data Type Mapping.

    PrimaryKeys, UniqueKeys

    Column Name

    Description

    SCHEMAThe name of the schema that contains the table.
    TableThe name of the table.
    NAMEThe name of the constraint.
    ISDEFERRABLEIndicates whether the constraint is deferrable.
    ISINITIALLYDEFERREDIndicates whether the constraint is initially deferred.
    ISENFORCEDIndicates whether the constraint is enforced.
    CREATEDThe date and time when the constraint was created.
    LASTALTEREDThe date and time when the constraint was last altered.
    COMMENTThe comment for the constraint.

    PrimaryKeyColumns, UniqueKeyColumns

    Column Name

    Description

    ConstraintNameThe name of the constraint.
    SchemaThe name of the schema that contains the table.
    TableThe name of the table.
    NameThe name of the column.
    PositionThe position of the column in the key, starting from 1.
    CreatedThe date and time when the constraint was created.
    CommentThe comment for the constraint.

    ForeignKeys

    Column Name

    Description

    CONSTRAINTNAMEThe name of the foreign key.
    SCHEMAThe name of the schema that contains the foreign key.
    TableThe name of the table that contains the foreign key.
    REFERENCEDCONSTRAINTNAMEThe name of the primary or unique key that the foreign key references.
    REFERENCEDSCHEMAThe name of the schema that contains the referenced key.
    REFERENCEDTABLEThe name of the table that contains the referenced key.
    MATCHOPTIONThe match option of the foreign key.
    UPDATERULEThe update rule of the foreign key.
    DELETERULEThe delete rule of the foreign key.
    CREATEDThe date and time when the foreign key was created.
    LASTALTEREDThe date and time when the foreign key was last altered.
    COMMENTThe comment for the foreign key.

    ForeignKeyColumns

    Column Name

    Description

    ConstraintNameThe name of the foreign key.
    SchemaThe name of the schema that contains the table.
    TableThe name of the table.
    NameThe name of the foreign key column.
    PositionThe position of the column in the foreign key, starting from 1.
    ReferencedConstraintNameThe name of the referenced primary or unique key.
    ReferencedSchemaThe name of the schema that contains the referenced table.
    ReferencedTableThe name of the referenced table.
    ReferencedColumnThe name of the referenced column.
    UpdateRuleThe update rule of the foreign key.
    DeleteRuleThe delete rule of the foreign key.
    DeferrabilityThe deferrability of the foreign key.
    CreatedThe date and time when the foreign key was created.
    CommentThe comment for the foreign key.

    Samples

    The following code sample shows how to get the list of columns of the EMPLOYEES table in the PUBLIC schema and print the name and data type of each column.

    using (SnowflakeConnection connection = new SnowflakeConnection(connectionString)) {
      connection.Open();
      DataTable columns = connection.GetSchema(
        SnowflakeMetaDataCollectionNames.Columns,
        new string[] { "PUBLIC", "EMPLOYEES" });
      foreach (DataRow row in columns.Rows)
        Console.WriteLine("{0}: {1}", row["Name"], row["DataType"]);
    }
    
    Using connection As New SnowflakeConnection(connectionString)
      connection.Open()
      Dim columns As DataTable = connection.GetSchema( _
        SnowflakeMetaDataCollectionNames.Columns, _
        New String() {"PUBLIC", "EMPLOYEES"})
      For Each row As DataRow In columns.Rows
        Console.WriteLine("{0}: {1}", row("Name"), row("DataType"))
      Next
    End Using
    

    The following code sample gets all tables whose names start with ORDER in all schemas of the database. The schema restriction is set to "%", so that the Schema connection string parameter is not applied.

    DataTable tables = connection.GetSchema("Tables", new string[] { "%", "ORDER%" });
    
    Dim tables As DataTable = connection.GetSchema("Tables", New String() {"%", "ORDER%"})
    

    See Also

    Metadata | Data Type Mapping