Files
sql-scripts/TPDT-268 - ACP in task sequence/dba_storedProcedures/Medor.sql
2024-03-07 16:52:14 +01:00

255 lines
9.3 KiB
Transact-SQL

USE [HCITools]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Medor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Medor]
GO
USE [HCITools]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Medor]
@in_debug int = null,
@in_Search sysname,
@max_recursion int = 0
AS
/*=============================================================================
Explication du traitement realise par la SP
-------------------------------------------
Cette SP sert à chercher dans l'ensemble des bases le texte passé en paramétre dans les SP, trigger etc... et les jobs
Contexte d'utilisation
----------------------
Cette SP est appelée à la main
Parametres
----------
@in_debug : non utilisé pour le moment
@in_Search : texte à chercher
@max_recursion : nombre maximum de récursion (défaut 0)
Creation : 25.07.17 / FLA
Modifications :
=============================================================================*/
set nocount on;
declare @result_sp int,
@errno int,
@errmsg varchar(255)
/*------------------- Declaration des variables --------------------*/
declare @cmd varchar(max),
@name sysname
/*------------ Affectation des parametres aux variables ------------*/
/*-------------------------- Traitement ---------------------------*/
create table #ObjectLink
(
OL_guid uniqueidentifier default newid(),
OL_Database sysname,
OL_schema sysname,
OL_sysobject_name sysname,
OL_contained_piece sysname,
OL_TexteAround varchar(110),
OL_recursion_level int,
OL_sysobject_id int,
OL_sysobject_xtype char (2)
)
;
CREATE NONCLUSTERED INDEX Ix_ObjectLink_OL_sysobject_name ON #ObjectLink(OL_sysobject_name)
;
CREATE NONCLUSTERED INDEX Ix_ObjectLink_OL_contained_piece ON #ObjectLink(OL_contained_piece)
;
declare c_databases cursor local forward_only static for
SELECT name
FROM sys.databases
ORDER BY name
open c_databases
;
FETCH NEXT FROM c_databases
into @name
while @@fetch_status = 0
begin
select @cmd = 'insert into #ObjectLink
(OL_Database,OL_schema,OL_sysobject_name, OL_contained_piece, OL_TexteAround, OL_recursion_level, OL_sysobject_id, OL_sysobject_xtype)
(
select distinct '''+@name+''',OBJECT_SCHEMA_NAME (so.id,DB_ID('''+@name+''')), so.name, '''+@in_Search+''' ''contained_piece'', SUBSTRING(sc.text,CHARINDEX('''+@in_Search+''',sc.text,0)-50,100) ''Texte around'', 0 ''recursion_level'', so.id, so.xtype
from '+@name+'..syscomments sc with (nolock)
join '+@name+'..sysobjects so with (nolock)
on so.id = sc.id
and so.name not like ''dt[_]%''
where sc.text like ''%'+@in_Search+'%''
and so.name <> '''+@in_Search+'''
)
;
insert into #ObjectLink
(OL_Database,OL_schema,OL_sysobject_name, OL_contained_piece, OL_TexteAround, OL_recursion_level, OL_sysobject_id, OL_sysobject_xtype)
(
select distinct '''+@name+''',OBJECT_SCHEMA_NAME (so.id,DB_ID('''+@name+''')), so.name, '''+@in_Search+''' ''contained_piece'', NULL ''Texte around'', 0 ''recursion_level'', so.id, so.xtype
from '+@name+'..sysobjects so with (nolock)
where so.name not like ''dt[_]%''
and so.name = '''+@in_Search+'''
)
;
declare @ObjectLinkCount_before int,
@ObjectLinkCount_after int,
@recursion int
;
select @ObjectLinkCount_before = 0
;
select @ObjectLinkCount_after = count (*) from #ObjectLink
;
select @recursion = 0
;
while @recursion < '+CAST(@max_recursion as varchar(2))+'
and @ObjectLinkCount_after > @ObjectLinkCount_before
begin
select @recursion = @recursion + 1
;
select @ObjectLinkCount_before = @ObjectLinkCount_after
;
insert into #ObjectLink
(OL_Database,OL_schema,OL_sysobject_name, OL_contained_piece, OL_TexteAround, OL_recursion_level, OL_sysobject_id, OL_sysobject_xtype)
(
select distinct '''+@name+''',OBJECT_SCHEMA_NAME (so.id,DB_ID('''+@name+''')), so.name, ol.OL_sysobject_name ''contained_piece'',SUBSTRING(sc.text,CHARINDEX('''+@in_Search+''',sc.text,0)-50,100) ''Texte around'', @recursion ''recursion_level'', so.id, so.xtype
from '+@name+'..syscomments sc with (nolock)
join '+@name+'..sysobjects so with (nolock)
on so.id = sc.id
and so.name not like ''dt[_]%''
join #ObjectLink ol
on sc.text like ''%'' + ol.OL_sysobject_name + ''%''
and so.name not in (select OL_sysobject_name from #ObjectLink union all select OL_contained_piece from #ObjectLink )
and ol.OL_recursion_level = @recursion-1
)
;
select @ObjectLinkCount_after = count (*) from #ObjectLink
;
end /* end while */
;'
--select @cmd
exec (@cmd)
FETCH NEXT FROM c_databases
into @name
;
end
;
close c_databases
;
deallocate c_databases
select OL_recursion_level,
OL_sysobject_xtype,
case OL_sysobject_xtype
when 'F' then 'Aggregate function (CLR)'
when 'C ' then 'CHECK constraint'
when 'D ' then 'Default or DEFAULT constraint'
when 'F ' then 'FOREIGN KEY constraint'
when 'L ' then 'Log'
when 'FN ' then 'Scalar function'
when 'FS ' then 'Assembly (CLR) scalar-function'
when 'FT ' then 'Assembly (CLR) table-valued function'
when 'IF ' then 'In-lined table-function'
when 'IT ' then 'Internal table'
when 'P ' then 'Stored procedure'
when 'PC ' then 'Assembly (CLR) stored-procedure'
when 'PK ' then 'PRIMARY KEY constraint (type is K)'
when 'RF ' then 'Replication filter stored procedure'
when 'S ' then 'System table'
when 'SN ' then 'Synonym'
when 'SQ ' then 'Service queue'
when 'TA ' then 'Assembly (CLR) DML trigger'
when 'TF ' then 'Table function'
when 'TR ' then 'SQL DML Trigger'
when 'TT ' then 'Table type'
when 'U ' then 'User table'
when 'UQ ' then 'UNIQUE constraint (type is K)'
when 'V ' then 'View'
when 'X ' then 'Extended stored procedure'
else 'unknown xtype'
end 'xtype_text',
OL_schema, OL_Database, OL_sysobject_name, OL_contained_piece, OL_TexteAround
from #ObjectLink ol
order by OL_recursion_level, OL_sysobject_xtype,OL_Database,OL_schema, OL_sysobject_name, OL_contained_piece
;
/*------------------------------------------------------------------------------------------------
Retourne les jobs qui appellent le bout de texte cherché
------------------------------------------------------------------------------------------------*/
select distinct *
from
(
select sj.name 'job_name',
sjs.step_id,
sjs.step_name,
@in_Search 'contained_piece',
SUBSTRING(sjs.command,CHARINDEX(''+@in_Search+'',sjs.command,0)-50,100) 'Texte around'
from msdb.dbo.sysjobs sj with (nolock)
join msdb.dbo.sysjobsteps sjs with (nolock)
on sjs.job_id = sj.job_id
where sjs.command like '%' + @in_Search + '%'
union
select sj.name 'job_name',
sjs.step_id,
sjs.step_name,
ol.OL_contained_piece 'contained_piece',
SUBSTRING(sjs.command,CHARINDEX(''+ol.OL_contained_piece+'',sjs.command,0)-50,100) 'Texte around'
from msdb.dbo.sysjobs sj with (nolock)
join msdb.dbo.sysjobsteps sjs with (nolock)
on sjs.job_id = sj.job_id
join #ObjectLink ol
on sjs.command like '%' + ol.OL_contained_piece + '%'
WHERE ol.OL_contained_piece <> @in_Search
union
select sj.name 'job_name',
sjs.step_id,
sjs.step_name,
ol.OL_sysobject_name 'contained_piece',
SUBSTRING(sjs.command,CHARINDEX(''+ol.OL_sysobject_name+'',sjs.command,0)-50,100) 'Texte around'
from msdb.dbo.sysjobs sj with (nolock)
join msdb.dbo.sysjobsteps sjs with (nolock)
on sjs.job_id = sj.job_id
join #ObjectLink ol
on sjs.command like '%' + ol.OL_sysobject_name + '%'
WHERE ol.OL_sysobject_name <> @in_Search
) vJobs
order by vJobs.job_name, vJobs.step_id, vJobs.contained_piece
;
DROP TABLE #ObjectLink
/*------------------ Retour au programme appelant -----------------*/
return(@@error);
/*---------------------- Traitement des erreurs ----------------------*/
error_99:
raiserror (@errmsg, 16, 1);
return(@errno);
GO