Sql Get Column Names From Table
We can easily learn the names of columns which table name specified. Even, we can learn data types. To learn column names we must write the code below.
TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK
YOU MAY WANT TO SEE OUR ALL EXAMPLES PAGE, THEN CLICK HERE
1 |
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='books' |
To learn with data types than we can write:
1 2 3 4 5 6 |
SELECT object_name(c.id) AS table_name, c.name AS column_name, t.name AS data_type FROM syscolumns AS c INNER JOIN systypes AS t ON c.xtype = t.xtype WHERE c.id = object_id( 'books' ) |
If we want to learn specified types then we can add conditions like below:
1 2 3 4 5 6 |
SELECT object_name(c.id) AS table_name, c.name AS column_name, t.name AS data_type FROM syscolumns AS c INNER JOIN systypes AS t ON c.xtype = t.xtype WHERE c.id = object_id( 'books' ) and t.name = 'varchar' |
Result Of The Query;
The above code, only lists the columns of the table named ‘books’ in the ‘varchar’ data type.