Skip to content

Instantly share code, notes, and snippets.

@zommarin
Forked from shbaz/gist:1188016
Created September 23, 2011 05:34
Show Gist options
  • Save zommarin/1236809 to your computer and use it in GitHub Desktop.
Save zommarin/1236809 to your computer and use it in GitHub Desktop.
Find non-dbo tables, views and procedures [SQL Server]
--
-- Select the tables, views and stored procedures that are not in the [dbo] schema
--
-- Useful when double checking that prople has not run scripts with the wrong user
-- for databases that have everything in the [dbo] schema.
--
SELECT 'Table' AS [Type] ,
'[' + s.name + '].[' + t.name + ']' AS [Name]
FROM sys.tables AS t
INNER JOIN sys.schemas AS s ON t.schema_id = s.schema_id
WHERE s.name <> 'dbo'
UNION ALL
SELECT 'SProc' ,
'[' + s.name + '].[' + p.name + ']'
FROM sys.procedures AS p
INNER JOIN sys.schemas AS s ON p.schema_id = s.schema_id
WHERE s.name <> 'dbo'
UNION ALL
SELECT 'View' ,
'[' + s.name + '].[' + v.name + ']'
FROM sys.views AS v
INNER JOIN sys.schemas AS s ON v.schema_id = s.schema_id
WHERE s.name <> 'dbo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment