-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckTablesAndViews.sql
36 lines (35 loc) · 1.06 KB
/
CheckTablesAndViews.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
DROP FUNCTION IF EXISTS adm.UDF_CHK_NamingTableAndView
GO
CREATE FUNCTION adm.UDF_CHK_NamingTableAndView ()
RETURNS TABLE
AS
RETURN
WITH
CorrectNamedObjects
AS (SELECT
so.id,
so.name
FROM sys.sysobjects so
CROSS JOIN DBATOOLS.adm.A_CHK_NamingConventionElements DomainsList
CROSS JOIN DBATOOLS.adm.A_CHK_NamingConventionElements PrefixList
WHERE
DomainsList.ObjType='All' AND DomainsList.EltType='Domain' AND
PrefixList.ObjType='TableAndView' AND PrefixList.EltType='Prefix' AND
so.xtype IN ( 'U', 'V' ) AND
so.name COLLATE Latin1_General_CS_AS LIKE PrefixList.EltValue + '\_' + DomainsList.EltValue + '\_%' ESCAPE '\'
)
SELECT CASE so.xtype
WHEN 'U' THEN
'TABLE'
WHEN 'V' THEN
'VIEW'
END ObjType,
so.xtype,
so.id,
SCHEMA_NAME(so.uid) SchName,
so.name ObjName,
CASE WHEN (cno.id IS NULL) THEN 'No' ELSE 'Yes' END NamingConventionRespect
FROM sys.sysobjects so
LEFT OUTER JOIN CorrectNamedObjects cno ON (cno.id = so.id)
WHERE so.xtype IN ( 'U', 'V' )
;