Solved

None of the table name falls under more than one schema

Posted on 2013-06-10
4
321 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL BCP Extra Lines Between Records 2 30
sql how to count case when 4 29
TSQL Challenge... 7 45
MSSQL 2008 R2  Check to see if a Index/Key Exist 7 9
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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