Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all tables & columns names of a linked-server database in SQL Server?

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?

like image 444
user3486647 Avatar asked Dec 01 '16 18:12

user3486647


People also ask

How do I get a list of tables in a database?

To get a list of the tables in a MySQL database, use the mysql client tool to connect to the MySQL server and run the SHOW TABLES command. The optional FULL modifier will show the table type as a second output column.

How do I get a list of all tables in a column in SQL?

The below query can be run in a database on SQL Server to list all tables with column details. SELECT schema_name(tab. schema_id) as schema_name, tab.name as table_name, col. column_id, col.name as column_name, t.name as data_type, col.


4 Answers

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:

SELECT s.name AS schema_name
      ,t.name AS table_Name
      ,c.name AS column_Name
    --,c.max_length
FROM [SERVER].[DB].sys.tables t
JOIN [SERVER].[DB].sys.schemas s ON t.schema_id = s.schema_id
JOIN [SERVER].[DB].sys.columns c ON t.object_id = c.object_id
--WHERE s.name = 'dbo'
ORDER BY s.name, t.name, c.name
like image 61
Nickolay Avatar answered Sep 29 '22 23:09

Nickolay


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:

EXEC sp_tables_ex @table_server = 'MYSQL_DB'
like image 39
vadim-info Avatar answered Sep 30 '22 01:09

vadim-info


Fully qualify your linked server in your FROM and JOIN, and alias them.

SELECT lst.name AS Table_Name, 
       lsc.name AS Column_Name, 
       lsc.max_length, 
       (schema_id) As Schema_name

FROM [SERVER].[DB].[sys].[tables] lst
    INNER JOIN [SERVER].[DB].[sys].[columns] lsc
        ON lst.OBJECT_ID=lsc.object_id

ORDER BY schema_name, lst.name, lsc.name
like image 32
S3S Avatar answered Sep 29 '22 23:09

S3S


There is the solution to list:

declare @temp table
(
    col1 varchar(255),
    col2 varchar(255),
    [name] varchar(255),
    [type] varchar(255),
    col3 varchar(255)
)
insert @temp exec sp_tables_ex 'Your_LinkedServer_Name'
select * from @temp

And also open a cursor:

DECLARE lstTables CURSOR FOR 
        select [name] from @temp
like image 42
Oxana N Avatar answered Sep 30 '22 01:09

Oxana N