If it's a regular database, i can simply use this query to get a list of all table names and their column names of the database.
use [my_database_name]
GO
SELECT sys.tables.name AS Table_Name,
sys.columns.name AS Column_Name,
sys.columns.max_length,
(schema_id) As Schema_name
FROM sys.tables
INNER JOIN sys.columns
ON sys.tables.OBJECT_ID=sys.columns.object_id
ORDER BY schema_name, sys.tables.name, sys.columns.name
but right now I need to connect to a linked-server database therefore the 'use' can't be used. Is there another way?
There is the solution to list:
And also open a cursor:
The system stored procedure sp_tables is used to list out the tables available in the current database of the current server. You can use sp_tables_ex for the linked server. The following returns list of tables available in the specified Server:
To expand slightly on @scsimon's answer...
The
(schema_id) As Schema_name
is not very useful, as it's really an ID. To get the list of all tables (and their columns) with actual schema names one can use:Fully qualify your linked server in your
FROM
andJOIN
, and alias them.