Discovering Schemas
The following sections show how to obtain schema information.
List Tables and Views
Although the sql package does not provide a native interface to query tables and views, you can use the sys_tables system table provided by the driver.
tables, _ := db.Query("SELECT CatalogName, SchemaName, TableName FROM sys_tables WHERE TableType='TABLE'") defer tables.Close() for tables.Next() { var ( catalog string schema string table string ) rows.Scan(&catalog, &schema, &table) fmt.Printf("Catalog: %s, Schema: %s, Table: %s", catalog, schema, table) } views, _ := db.Query("SELECT CatalogName, SchemaName, TableName FROM sys_tables WHERE TableType='VIEW'") defer views.Close() for views.Next() { var ( catalog string schema string view string ) rows.Scan(&catalog, &schema, &view) fmt.Printf("Catalog: %s, Schema: %s, Table: %s", catalog, schema, table) }
List Table Columns
You can use the sys_tablecolumns to get information about columns.
columns, _ := db.Query("SELECT ColumnName, DataType, Length, NumericPrecision, IsNullable FROM sys_tablecolumns WHERE TableName = '[SampleCatalog_1].[SampleSchema_1].SampleTable_1'") defer columns.Close() for columns.Next() { var ( column string datatype int length int precision int nullable bool ) rows.Scan(&column, &datatype, &length, &precision, &nullable) fmt.Printf("Name: %s, Type: %d, Length: %d, Precision: %d, Nullable: %t\n", column, datatype, length, precision, nullable) }