Solved

None of the table name falls under more than one schema

Posted on 2013-06-10
4
320 Views
Last Modified: 2013-06-13
In my database there are more than 10 schemas. How to ensure that none of the table  is not fall under more than one schema.

I mean that if a table name say for example tEmployee if falling under the schema [dbo] and [MyDB.Employee] means that should be shown in result.

How to write TSQL to achieve that? Please do assist.
0
Comment
Question by:Easwaran Paramasivam
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:Ross Turner
Ross Turner earned 266 total points
ID: 39234914
Give this a whirl....

SELECT
TABLE_SCHEMA,
TABLE_NAME,
row_number() over (partition by TABLE_NAME order by Table_Name)
FROM INFORMATION_SCHEMA.TABLES
Order by Table_Name


;With CTE As (
SELECT top 100
TABLE_SCHEMA,
TABLE_NAME,
row_number() over (partition by TABLE_NAME order by Table_Name) As NUMBER
FROM INFORMATION_SCHEMA.TABLES
Order by Table_Name
)
Select * from CTE
where NUMBER > 1
0
 
LVL 7

Assisted Solution

by:Ross Turner
Ross Turner earned 266 total points
ID: 39234929
This might work better

;With CTE As (
SELECT top 100
TABLE_SCHEMA,
TABLE_NAME,
row_number() over (partition by TABLE_NAME order by Table_Name) As NUMBER
FROM INFORMATION_SCHEMA.TABLES
Order by Table_Name
)
Select distinct
IST.* 
from CTE C
Inner Join  INFORMATION_SCHEMA.TABLES IST on C.TABLE_NAME = IST.TABLE_NAME
where NUMBER > 1

Open in new window

0
 
LVL 8

Accepted Solution

by:
didnthaveaname earned 134 total points
ID: 39235030
I think this one may boil down to personal preference, but here's a different method with joins/grouping:

select
	T1.name as tableName,
	schema_name( T1.schema_id ) as schemaName
from
	sys.tables as T1
		inner join sys.tables as T2 on T1.name = T2.name and T1.schema_id <> T2.schema_id
group by
	T1.name,
	T1.schema_id
order by
	tableName,
	schemaName;	

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39238757
SELECT
    t.name AS table_name,
    SCHEMA_NAME(t.schema_id) AS schema_name
    --,t.*
FROM sys.tables t
WHERE
    --ignore system tables
    t.is_ms_shipped = 0 AND SCHEMA_NAME(t.schema_id) NOT IN (N'INFORMATION_SCHEMA', N'sys') AND
    EXISTS(
        SELECT 1
        FROM sys.tables t2
        WHERE
            --ignore system tables
            t2.is_ms_shipped = 0 AND SCHEMA_NAME(t2.schema_id) NOT IN (N'INFORMATION_SCHEMA', N'sys') AND
            --check table name same but schema name (id) is different
            (t2.name = t1.name AND t2.schema_id <> t1.schema_id)
    )
ORDER BY
    1, 2
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question