Labels

admin (1) aix (1) alert (1) always-on (2) Architecture (1) aws (3) Azure (1) backup (3) BI-DWH (10) Binary (3) Boolean (1) C# (1) cache (1) casting (3) cdc (1) certificate (1) checks (1) cloud (3) cluster (1) cmd (7) collation (1) columns (1) compilation (1) configurations (7) Connection-String (2) connections (6) constraint (6) copypaste (2) cpu (2) csv (3) CTE (1) data-types (1) datetime (23) db (547) DB2 (1) deadlock (2) Denali (7) device (6) dotNet (5) dynamicSQL (11) email (5) encoding (1) encryption (4) errors (124) excel (1) ExecutionPlan (10) extended events (1) files (7) FIPS (1) foreign key (1) fragmentation (1) functions (1) GCP (2) gMSA (2) google (2) HADR (1) hashing (3) in-memory (1) index (3) indexedViews (2) insert (3) install (10) IO (1) isql (6) javascript (1) jobs (11) join (2) LDAP (2) LinkedServers (8) Linux (15) log (6) login (1) maintenance (3) mariadb (1) memory (4) merge (3) monitoring (4) MSA (2) mssql (444) mssql2005 (5) mssql2008R2 (20) mssql2012 (2) mysql (36) MySQL Shell (5) network (1) NoSQL (1) null (2) numeric (9) object-oriented (1) offline (1) openssl (1) Operating System (4) oracle (7) ORDBMS (1) ordering (2) Outer Apply (1) Outlook (1) page (1) parameters (2) partition (1) password (1) Performance (103) permissions (10) pivot (3) PLE (1) port (4) PostgreSQL (14) profiler (1) RDS (3) read (1) Replication (12) restore (4) root (1) RPO (1) RTO (1) SAP ASE (48) SAP RS (20) SCC (4) scema (1) script (8) security (10) segment (1) server (1) service broker (2) services (4) settings (75) SQL (74) SSAS (1) SSIS (19) SSL (8) SSMS (4) SSRS (6) storage (1) String (35) sybase (57) telnet (2) tempdb (1) Theory (2) tips (120) tools (3) training (1) transaction (6) trigger (2) Tuple (2) TVP (1) unix (8) users (3) vb.net (4) versioning (1) windows (14) xml (10) XSD (1) zip (1)

Find Unused Objects in the DB


CREATE PROCEDURE  [dbo].[Itai_checkUnusedObjectsSP]
@IncludeTables int,
@IncludeViews int,
@IncludeFunctions int,
@IncludeSPs int,
@Schema nvarchar(100) = '',
@NotSchema nvarchar(100) = '',
@NamePrefix nvarchar(100) = ''
AS

declare @TempObjects table (schema_ nvarchar(50), table_ nvarchar(250))
declare @ObjectsNotToDelete table (schema_ nvarchar(50), table_ nvarchar(250))
declare @SQL as nvarchar(2000)
DECLARE @TempTypesStr nvarchar(100)
SET @TempTypesStr = ''

IF @IncludeTables+@IncludeViews+@IncludeFunctions+@IncludeSPs = 0
begin
select 'Select object type'
return
end


-- insert relevant objects into @TempObjects
SET @SQL = 'SELECT  SCHEMA_NAME(schema_id), name
FROM sys.objects
WHERE '

IF @IncludeTables = 1 SET @TempTypesStr = @TempTypesStr + ',''U'''
IF @IncludeViews = 1 SET @TempTypesStr = @TempTypesStr + ',''V'''
IF @IncludeFunctions = 1 SET @TempTypesStr = @TempTypesStr + ',''AF'',''FN'',''FS'',''FT'',''IF'',''TF'''
IF @IncludeSPs = 1 SET @TempTypesStr = @TempTypesStr + ',''P'',''PC'',''RF'',''X'''
select @TempTypesStr = stuff(@TempTypesStr,1,1,'')

SET @SQL = @SQL +
'type in (' + @TempTypesStr + ') '

IF @Schema <> '' SET @SQL = @SQL + '
AND SCHEMA_NAME(schema_id) = ''' + @Schema + ''' '

IF @NotSchema <> '' SET @SQL = @SQL + '
AND SCHEMA_NAME(schema_id) <> ''' + @NotSchema + ''' '

IF @NamePrefix <> '' SET @SQL = @SQL + '
AND name like ''' + @NamePrefix + '%'' '

print @SQL
insert into @TempObjects(schema_, table_) EXEC(@SQL)
--check:
--select * from @TempObjects

IF @NotSchema = ''
BEGIN
insert into @ObjectsNotToDelete(schema_, table_)
-- return objects that have conections
SELECT distinct t.schema_, t.table_ --, s.SPECIFIC_NAME as object_name_ --s.*
FROM INFORMATION_SCHEMA.ROUTINES s
inner join @TempObjects t on s.ROUTINE_DEFINITION LIKE '%' + t.table_ + '%'
--and s.SPECIFIC_SCHEMA = t.schema_
where t.table_ <> s.SPECIFIC_NAME
union
select distinct t.schema_, t.table_ --, object_name(s.id) as object_name_ --, count(*) as count_
from syscomments s inner join sys.objects o on o.object_id = s.id
inner join @TempObjects t on s.text like  '%' + t.table_ + '%' --and SCHEMA_NAME(o.schema_id) = t.schema_
where t.table_ <> object_name(s.id)
--order by table_
END
ELSE
BEGIN
insert into @ObjectsNotToDelete(schema_, table_)
-- return objects that have conections
SELECT distinct t.schema_, t.table_ --, s.SPECIFIC_NAME as object_name_ --s.*
FROM INFORMATION_SCHEMA.ROUTINES s
inner join @TempObjects t on s.ROUTINE_DEFINITION LIKE '%' + t.table_ + '%'
--and s.SPECIFIC_SCHEMA = t.schema_
and s.SPECIFIC_SCHEMA <> @NotSchema
where t.table_ <> s.SPECIFIC_NAME
union
select distinct t.schema_, t.table_ --, object_name(s.id) as object_name_ --, count(*) as count_
from syscomments s inner join sys.objects o on o.object_id = s.id
inner join @TempObjects t on s.text like  '%' + t.table_ + '%' and SCHEMA_NAME(o.schema_id) <> @NotSchema
where t.table_ <> object_name(s.id)
--order by table_
END

if @IncludeTables = 1
begin
IF @NotSchema = ''
BEGIN
insert into @ObjectsNotToDelete(schema_, table_)
select SCHEMA_NAME(o.schema_id),
--f.constraint_object_id, object_name(f.constraint_object_id) as constraint_object, o.name,
--f.constraint_column_id, --object_name(f.constraint_column_id) as constraint_column,
--f.parent_object_id, object_name(f.parent_object_id) as parent_object,
--f.parent_column_id, object_name(f.parent_column_id) as parent_column,
--f.referenced_object_id,
object_name(f.referenced_object_id) as referenced_object --,
--f.referenced_column_id --, object_name(f.referenced_column_id) as referenced_column
from sys.foreign_key_columns f
inner join sys.objects o on o.object_id = f.referenced_object_id --and SCHEMA_NAME(o.schema_id) <> 'Lms'
where object_name(referenced_object_id) like @NamePrefix + '%'
END
ELSE
BEGIN
insert into @ObjectsNotToDelete(schema_, table_)
select SCHEMA_NAME(o.schema_id),
--f.constraint_object_id, object_name(f.constraint_object_id) as constraint_object, o.name,
--f.constraint_column_id, --object_name(f.constraint_column_id) as constraint_column,
--f.parent_object_id, object_name(f.parent_object_id) as parent_object,
--f.parent_column_id, object_name(f.parent_column_id) as parent_column,
--f.referenced_object_id,
object_name(f.referenced_object_id) as referenced_object --,
--f.referenced_column_id --, object_name(f.referenced_column_id) as referenced_column
from sys.foreign_key_columns f
inner join sys.objects o on o.object_id = f.referenced_object_id and SCHEMA_NAME(o.schema_id) <> @NotSchema
where object_name(referenced_object_id) like @NamePrefix + '%'
END
end

------------------------------------------------------------------------------------
-- For this section, you have to create temporary table in the database, that will contain the objects that you know that you can't delete (For example: object that is not called from the DB, but do called from the Application).
-- The SP do not display those objects in the results list.
-- See below the table script.
IF @IncludeTables = 1
insert into @ObjectsNotToDelete(schema_, table_)
select schema_, objName_ from CheckedObjectsNotToDelete where type_ = 'Table'
IF @IncludeViews = 1
insert into @ObjectsNotToDelete(schema_, table_)
select schema_, objName_ from CheckedObjectsNotToDelete where type_ = 'View'
IF @IncludeFunctions = 1
insert into @ObjectsNotToDelete(schema_, table_)
select schema_, objName_ from CheckedObjectsNotToDelete where type_ = 'Function'
IF @IncludeSPs = 1
insert into @ObjectsNotToDelete(schema_, table_)
select schema_, objName_ from CheckedObjectsNotToDelete where type_ = 'SP'
------------------------------------------------------------------------------------


--checks:
--select * from @TempObjects order by schema_, table_
--select distinct * from @ObjectsNotToDelete order by schema_, table_

select o.* from @TempObjects o
left join (select distinct * from @ObjectsNotToDelete) ontd on o.schema_ = ontd.schema_ and o.table_ = ontd.table_
where ontd.table_ is null
order by o.schema_, o.table_

GO


------------------------------------------------------------------------------------
--Script to create table CheckedObjectsNotToDelete:
CREATE TABLE [dbo].[CheckedObjectsNotToDelete](
[type_] [nvarchar](50) NULL,
[schema_] [nvarchar](50) NULL,
[objName_] [nvarchar](250) NULL
) ON [PRIMARY]

GO

-- type_:
-- Table
-- View
-- Function
-- SP (stored procedure)

No comments:

Post a Comment