Multiple tables with the same name in SQL Server

svbyogibear

when running the query below, I get a duplicate table returned, even though only one of it exists. Why could that be?

SELECT 
s.Name AS SchemaName,
t.NAME AS TableName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB, 
SUM(a.used_pages) * 8 AS UsedSpaceKB, 
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
sys.tables t
INNER JOIN 
sys.schemas s ON s.schema_id = t.schema_id
INNER JOIN      
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
t.NAME NOT LIKE 'dt%' 
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255 
GROUP BY 
t.Name, s.Name, p.Rows
ORDER BY 
TotalSpaceKB DESC

** UPDATE The database name was displayed with a (In Recovery) tag, I just ran a script to check and it said that it is 11% completed. Could this also have caused this problem?

Diego Sergnese

As Sean pointed out, you have more than one index on the duplicated tables. To check the index name(s) you can run the slightly modified query:

SELECT 
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows AS RowCounts,
i.name,
SUM(a.total_pages) * 8 AS TotalSpaceKB, 
SUM(a.used_pages) * 8 AS UsedSpaceKB, 
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows, i.name
ORDER BY 
    t.name

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Server parse XML to table - multiple node with the same name and first node should be table columns

分類Dev

Rails ActiveRecord: Pluck from multiple tables with same column name

分類Dev

How to search a column name in all tables in a database in SQL Server 2012?

分類Dev

How do I join multiple tables in SQL Server?

分類Dev

SQL Server - compare the results of two stored procedures that output multiple tables

分類Dev

Compare multiple rows from the same table in SQL Server 2005

分類Dev

SQL Server query result needs to give different values in two columns of the same name from the same source table

分類Dev

Multiple columns with the same name in Pandas

分類Dev

Scope of temporary tables in SQL Server

分類Dev

SQL JOIN multiple tables and subqueries

分類Dev

SQL Queries from multiple tables

分類Dev

How to SET IDENTITY_INSERT ON in SQL Server 2008 for multiple tables at once

分類Dev

Output Multiple flat files to multiple SQL tables

分類Dev

Multiple celerycam procceses on the same server

分類Dev

Search for same Values in two SQL TABLES

分類Dev

Combining 2 SQL Tables with same Columns

分類Dev

Values from multiple tables, Multiple rows, Same page

分類Dev

Using Entity Framework with Multiple Databases and Providers in the Same Project (SQL Server and MySql)

分類Dev

xslt merge multiple nodes with same name

分類Dev

Is it allowed to have multiple elements with same name in XSD?

分類Dev

Delete Multiple rows with same column name

分類Dev

zip multiple files with the same name but different extensions

分類Dev

SQL Server after update triggers on 2 tables

分類Dev

SQL Server: join 2 tables with condition ON statement

分類Dev

MS Access Linked Tables to SQL Server

分類Dev

Multiple SQL tables added together without a JOIN

分類Dev

Old SQL? multiple tables after FROM

分類Dev

SQL join one to many - multiple tables

分類Dev

SQL query, two tables, multiple items

Related 関連記事

  1. 1

    SQL Server parse XML to table - multiple node with the same name and first node should be table columns

  2. 2

    Rails ActiveRecord: Pluck from multiple tables with same column name

  3. 3

    How to search a column name in all tables in a database in SQL Server 2012?

  4. 4

    How do I join multiple tables in SQL Server?

  5. 5

    SQL Server - compare the results of two stored procedures that output multiple tables

  6. 6

    Compare multiple rows from the same table in SQL Server 2005

  7. 7

    SQL Server query result needs to give different values in two columns of the same name from the same source table

  8. 8

    Multiple columns with the same name in Pandas

  9. 9

    Scope of temporary tables in SQL Server

  10. 10

    SQL JOIN multiple tables and subqueries

  11. 11

    SQL Queries from multiple tables

  12. 12

    How to SET IDENTITY_INSERT ON in SQL Server 2008 for multiple tables at once

  13. 13

    Output Multiple flat files to multiple SQL tables

  14. 14

    Multiple celerycam procceses on the same server

  15. 15

    Search for same Values in two SQL TABLES

  16. 16

    Combining 2 SQL Tables with same Columns

  17. 17

    Values from multiple tables, Multiple rows, Same page

  18. 18

    Using Entity Framework with Multiple Databases and Providers in the Same Project (SQL Server and MySql)

  19. 19

    xslt merge multiple nodes with same name

  20. 20

    Is it allowed to have multiple elements with same name in XSD?

  21. 21

    Delete Multiple rows with same column name

  22. 22

    zip multiple files with the same name but different extensions

  23. 23

    SQL Server after update triggers on 2 tables

  24. 24

    SQL Server: join 2 tables with condition ON statement

  25. 25

    MS Access Linked Tables to SQL Server

  26. 26

    Multiple SQL tables added together without a JOIN

  27. 27

    Old SQL? multiple tables after FROM

  28. 28

    SQL join one to many - multiple tables

  29. 29

    SQL query, two tables, multiple items

ホットタグ

アーカイブ