1096 Stimmen

Wie bekomme ich eine Liste aller Tabellen in einer Datenbank mit TSQL?

Wie kann ich am besten die Namen aller Tabellen in einer bestimmten Datenbank auf SQL Server ermitteln?

2 Stimmen

4 Stimmen

Hat SHOW TABLES (wie in MySQL verwendet) funktionieren?

10voto

spoulson Punkte 20898

select * from sysobjects where xtype='U'

10voto

devio Punkte 36064
SELECT name 
FROM sysobjects 
WHERE xtype='U' 
ORDER BY name;

(SQL Server 2000 Standard; wird in SQL Server 2005 weiterhin unterstützt).

10voto

Ray Punkte 178277
exec sp_msforeachtable 'print ''?'''

8voto

Hassan Munir Punkte 149
Any of the T-SQL code below will work in SQL Server 2019:

-- here, you need to prefix the database name in INFORMATION_SCHEMA.TABLES
SELECT TABLE_NAME FROM [MSSQL-TEST].INFORMATION_SCHEMA.TABLES;

-- The next 2 ways will require you to point
-- to the specific database you want to list the tables

USE [MSSQL-TEST];
-- (1) Using sys.tables
SELECT * FROM sys.tables;

-- (2) Using sysobjects
SELECT * FROM sysobjects
WHERE type='U';

Here’s a working example using [Skyvia] using sys.tables.

[Skyvia] should be the link to https://skyvia.com/connectors/sql-server

  [1]: https://i.stack.imgur.com/o3qo9.png

enter image description here

Your SQL GUI tool should also have a way to list down all the tables in a database like the one above.

So, whatever suits your need and taste, there’s a code or GUI tool for that.

7voto

Leon Bouquiet Punkte 3789

Die Kehrseite der INFORMATION_SCHEMA.TABLES ist, dass sie auch Systemtabellen enthält, wie z. B. dtproperties und die MSpeer_... Tabellen, ohne dass diese von Ihren eigenen Tabellen unterschieden werden können.

Ich würde die Verwendung von sys.objects (die neue Version des veralteten sysobjects View), die den Ausschluss der Systemtabellen unterstützt:

select *
from sys.objects
where type = 'U'      -- User tables
and is_ms_shipped = 0 -- Exclude system tables

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X