Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Restituire un elenco di tabelle da un server collegato in SQL Server (esempi T-SQL)

In SQL Server puoi usare sp_tables_ex stored procedure di sistema per restituire le informazioni sulle tabelle da un server collegato specificato.

Il modo più semplice per eseguire questa procedura memorizzata consiste nel passare il nome del server collegato. In questo modo verranno restituite tutte le tabelle dal database predefinito sul server collegato specificato, comprese le tabelle e le viste di sistema. Potrebbe essere una lunga lista.

Hai anche la possibilità di specificare un database diverso e/o uno schema di tabella specifico. Puoi anche filtrare i risultati in base al tipo di tabella (ad es. tabella, vista, tabella di sistema, ecc.).

Sintassi

La sintassi è questa:

sp_tables_ex [ @table_server = ] 'table_server'   
     [ , [ @table_name = ] 'table_name' ]   
     [ , [ @table_schema = ] 'table_schema' ]  
     [ , [ @table_catalog = ] 'table_catalog' ]   
     [ , [ @table_type = ] 'table_type' ]   
     [ , [@fUsePattern = ] 'fUsePattern' ]

Il @table_server argomento è l'unico argomento richiesto. Questo è il nome del server collegato da cui vuoi le informazioni sulla tabella.

Gli altri argomenti sono facoltativi e li tratterò nei seguenti esempi. Per ulteriori informazioni su questi argomenti, vedere la documentazione Microsoft.

Esempio 1 – Restituisci tutte le tabelle

L'esempio seguente restituisce tutte le tabelle, viste, tabelle di sistema, alias, ecc. dal database predefinito sul server collegato chiamato Homer.

EXEC sp_tables_ex 'Homer';

Questo restituisce centinaia di righe sul mio sistema, quindi non elencherò i risultati in questo esempio. Tieni presente che la maggior parte di queste righe sono tabelle di sistema e viste di sistema.

Questo potrebbe anche essere fatto in questo modo:

EXEC sp_tables_ex @table_server = 'Homer';

Esempio 2:specificare un database diverso

L'esempio seguente specifica che WideWorldImportersDW dovrebbe essere utilizzato il database.

EXEC sp_tables_ex 
  @table_server = 'Homer',   
  @table_catalog = 'WideWorldImportersDW';

Di nuovo, questo restituisce centinaia di righe, quindi non elencherò i risultati.

Esempio 3:restituire una tabella specifica

In questo esempio, restituisco informazioni su una tabella specifica.

EXEC sp_tables_ex 
  @table_server = 'Homer', 
  @table_catalog = 'Music',
  @table_name = 'Artists';

Risultati:

+-------------+---------------+--------------+--------------+-----------+
| TABLE_CAT   | TABLE_SCHEM   | TABLE_NAME   | TABLE_TYPE   | REMARKS   |
|-------------+---------------+--------------+--------------+-----------|
| Music       | dbo           | Artists      | TABLE        | NULL      |
+-------------+---------------+--------------+--------------+-----------+

Esempio 4 – Restituisci solo visualizzazioni

In questo esempio, specifico che devono essere restituite solo le viste.

EXEC sp_tables_ex 
  @table_server = 'Homer', 
  @table_catalog = 'Music',
  @table_type = 'VIEW';

Risultati:

+-------------+---------------+--------------+--------------+-----------+
| TABLE_CAT   | TABLE_SCHEM   | TABLE_NAME   | TABLE_TYPE   | REMARKS   |
|-------------+---------------+--------------+--------------+-----------|
| Music       | dbo           | BluesAlbums  | VIEW         | NULL      |
| Music       | dbo           | JazzAlbums   | VIEW         | NULL      |
| Music       | dbo           | RockAlbums   | VIEW         | NULL      |
+-------------+---------------+--------------+--------------+-----------+

Tieni presente che questi non includono le viste di sistema. Se avessi voluto restituire le viste di sistema, avrei usato @table_type = 'SYSTEM VIEW' (e il set di risultati sarebbe molto più grande).

Il @table_type argomento accetta i seguenti tipi:ALIAS , GLOBAL TEMPORARY , LOCAL TEMPORARY , SYNONYM , SYSTEM TABLE , SYSTEM VIEW , TABLE e VIEW .

Esempio 5:specificare uno schema di tabella

L'esempio seguente restringe i risultati a uno schema di tabella specifico (Dimension ) all'interno del WideWorldImportersDW banca dati.

EXEC sp_tables_ex 
  @table_server = 'Homer',   
  @table_catalog = 'WideWorldImportersDW',   
  @table_schema = 'Dimension';

Risultati:

+----------------------+---------------+------------------+--------------+-----------+
| TABLE_CAT            | TABLE_SCHEM   | TABLE_NAME       | TABLE_TYPE   | REMARKS   |
|----------------------+---------------+------------------+--------------+-----------|
| WideWorldImportersDW | Dimension     | City             | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Customer         | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Date             | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Employee         | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Payment Method   | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Stock Item       | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Supplier         | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Transaction Type | TABLE        | NULL      |
+----------------------+---------------+------------------+--------------+-----------+

Esempio 6 – Caratteri jolly

Il @fUsePattern argomento ti consente di specificare se il % , _ , [ e ] i caratteri vengono interpretati come caratteri jolly.

Il valore predefinito è 1 , il che significa che sono interpretati come caratteri jolly. Puoi specificare 0 per specificare che dovrebbero non essere interpretato come caratteri jolly.

Ecco un esempio di utilizzo di un carattere jolly:

EXEC sp_tables_ex 
  @table_server = 'Homer', 
  @table_catalog = 'WideWorldImportersDW',
  @table_name = 'C%',
  @table_type = 'TABLE',
  @fUsePattern = 1;

Risultati:

+----------------------+---------------+------------------+--------------+-----------+
| TABLE_CAT            | TABLE_SCHEM   | TABLE_NAME       | TABLE_TYPE   | REMARKS   |
|----------------------+---------------+------------------+--------------+-----------|
| WideWorldImportersDW | Dimension     | City             | TABLE        | NULL      |
| WideWorldImportersDW | Dimension     | Customer         | TABLE        | NULL      |
| WideWorldImportersDW | Integration   | City_Staging     | TABLE        | NULL      |
| WideWorldImportersDW | Integration   | Customer_Staging | TABLE        | NULL      |
+----------------------+---------------+------------------+--------------+-----------+

Ed ecco cosa succede se non specifichi alcun carattere jolly:

EXEC sp_tables_ex 
  @table_server = 'Homer', 
  @table_catalog = 'WideWorldImportersDW',
  @table_name = 'C%',
  @table_type = 'TABLE',
  @fUsePattern = 0;

Risultati:

(0 rows affected)
Time: 0.324s