How do I get column name of table from specific database?

Hemant Sonar

I wanted to get column name of a table, for that I've used below query, which works fine.

select COLUMN_NAME 
from INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME = 'G001'

But now that I want to get column name of table from specific database, for the same I'm using below query ,but it doesn't giving any output.

In e.g. HS is my database name out of multiple databases and G001 is my table name.

And in my case I need to pass database name and table name as parameter.

select COLUMN_NAME 
from INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME = 'HS..G001'

So my question is how do I get column name of table from specific database?

DavidG

You use the INFORMATION_SCHEMA in the database you want to query, eg:

SELECT COLUMN_NAME 
FROM HS.INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME='G001'

If you need to pass this as a parameter, then you need to write some dynamic SQL:

DECLARE @database VARCHAR(255) = 'HS'
DECLARE @table VARCHAR(255) = 'G001'

DECLARE @sql VARCHAR(1000) = 'SELECT COLUMN_NAME 
                              FROM ' + @database + '.INFORMATION_SCHEMA.COLUMNS 
                              WHERE TABLE_NAME = ''' + @table + ''''

EXEC(@sql)

Note: Dynamic SQL like this can be vulnerable to SQL injection so make sure you sanitise your inputs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get specific data from a specific column in MySQL?

From Dev

How can I get the attributes from a mysql table by specific column?

From Dev

Get specific value from table with table name, row and column index

From Dev

How to export specific column from specific table from database in phpmyadmin

From Dev

MySQL query to get count of column name from a table of a database?

From Dev

Get a column name from database

From Dev

How to get login with different database table column name in Laravel 5.2?

From Dev

How do I populate my Date Dimension Table in SQL Server so as to get a column with this specific value?

From Dev

Laravel, get data of a specific column from table and store it in a different column name in another table

From Dev

How can I get column name and type from an existing table in SQLAlchemy?

From Dev

How Do You Get the Name For A Specific Value In A Powershell Hash Table?

From Dev

Need to find specific column name from database

From Dev

How do I get selected database name from Combo box in a text box when I choose the Path name

From Dev

How do I get selected database name from Combo box in a text box when I choose the Path name

From Dev

In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

From Dev

How do I directly reference a table column in a database relationship?

From Dev

How do I get the last inserted id from a MySQL database table?

From Dev

How do I get the first and last name of a person in a select box from an associated table in cakePHP 2.4?

From Dev

Gather column name of a table from database

From Dev

Jsoup how to get text from a specific column of an html table

From Dev

Get the table name(s) from MySQL database where column name is like X value is equal to Y

From Dev

How do i get a specific process name memory usage?

From Dev

How do i get a specific process name memory usage?

From Dev

How do I use wget to get a file with a specific name?

From Dev

How to get Date & Time from Database table column in laravel?

From Dev

How to get all table and column names from database in Yii Framework

From Dev

How to get Table data in a db column from other database

From Dev

How does data.table get the column name from j?

From Dev

How to get last column name from mysql table?

Related Related

  1. 1

    How do I get specific data from a specific column in MySQL?

  2. 2

    How can I get the attributes from a mysql table by specific column?

  3. 3

    Get specific value from table with table name, row and column index

  4. 4

    How to export specific column from specific table from database in phpmyadmin

  5. 5

    MySQL query to get count of column name from a table of a database?

  6. 6

    Get a column name from database

  7. 7

    How to get login with different database table column name in Laravel 5.2?

  8. 8

    How do I populate my Date Dimension Table in SQL Server so as to get a column with this specific value?

  9. 9

    Laravel, get data of a specific column from table and store it in a different column name in another table

  10. 10

    How can I get column name and type from an existing table in SQLAlchemy?

  11. 11

    How Do You Get the Name For A Specific Value In A Powershell Hash Table?

  12. 12

    Need to find specific column name from database

  13. 13

    How do I get selected database name from Combo box in a text box when I choose the Path name

  14. 14

    How do I get selected database name from Combo box in a text box when I choose the Path name

  15. 15

    In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

  16. 16

    How do I directly reference a table column in a database relationship?

  17. 17

    How do I get the last inserted id from a MySQL database table?

  18. 18

    How do I get the first and last name of a person in a select box from an associated table in cakePHP 2.4?

  19. 19

    Gather column name of a table from database

  20. 20

    Jsoup how to get text from a specific column of an html table

  21. 21

    Get the table name(s) from MySQL database where column name is like X value is equal to Y

  22. 22

    How do i get a specific process name memory usage?

  23. 23

    How do i get a specific process name memory usage?

  24. 24

    How do I use wget to get a file with a specific name?

  25. 25

    How to get Date & Time from Database table column in laravel?

  26. 26

    How to get all table and column names from database in Yii Framework

  27. 27

    How to get Table data in a db column from other database

  28. 28

    How does data.table get the column name from j?

  29. 29

    How to get last column name from mysql table?

HotTag

Archive