s there a syntax wherein I can get the columns of a table from a different database? I have tried using select column_name from information_schema.columns
where table_name = 'Database_Name.DBO.Table_Name'
But it's not working. Please do help me, been doing this for days.
To retrieve all columns, use the wild card * (an asterisk). The FROM clause specifies one or more tables to be queried. Use a comma and space between table names when specifying multiple tables. The WHERE clause selects only the rows in which the specified column contains the specified value.
Create a Java data access class that contains the columns you want to retrieve. Retrieve the rows from each of the databases separately, creating a List of data access objects. Combine this Lists in Java to create one master List of data access objects.
You can use following query to list all columns or search columns across tables in a database. USE AdventureWorks GO SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name, c.name AS column_name FROM sys. tables AS t INNER JOIN sys. columns c ON t.
You're close. TABLE_NAME has just the name of the table. The rest of the information is in TABLE_CATALOG and TABLE_SCHEMA. You can qualify information_schema with the catalog you want to select from:
select column_name from Database_Name.information_schema.columns
where table_name = 'Table_Name' AND table_schema = 'dbo'
This is what I found out. Each database has its own information_schema view so it won't show the information from the other databases. So if you want to get the column names for a table in another database, you have to use the information_schema view of that database.
{database name}.information_schema
Example:
SET @Sql = CONCAT('INSERT INTO ', @DBName, '.dbo.colname_table (column_name)
SELECT COLUMN_NAME FROM ', @DBName, '.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ''table_name'' AND TABLE_SCHEMA = ''dbo''
ORDER BY ORDINAL_POSITION');
EXECUTE sp_executesql @Sql;
where @DBName is the name of the database.
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