?
Solved

None of the table name falls under more than one schema

Posted on 2013-06-10
4
Medium Priority
?
329 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 1064 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 1064 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 536 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 70

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

839 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