I can get the number of columns in an SQL Server database with this:
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'Address'
But is there any way (for an unknown number of columns) I can get the name and datatype and length of each column?
Using SQL Server Management Studio In Object Explorer, select the table for which you want to show properties. Right-click the table and choose Properties from the shortcut menu. For more information, see Table Properties - SSMS.
SQL Server: sp_help table_name (or sp_columns table_name for only columns) Oracle DB2: desc table_name or describe table_name. MySQL: describe table_name (or show columns from table_name for only columns)
So desc or describe command shows the structure of table which include name of the column, data-type of column and the nullability which means, that column can contain null values or not. All of these features of table are described at the time of Creation of table.
Instead of using count(*)
you can SELECT *
and you will return all of the details that you want including data_type
:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'Address'
MSDN Docs on INFORMATION_SCHEMA.COLUMNS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With