Cómo identificar todos los procedimientos almacenados que hacen referencia a una tabla en particular


He creado una tabla sobre el entorno de desarrollo para fines de prueba y hay pocos sp que están reformando esta tabla. Ahora tengo que dejar caer esta tabla, así como identificar todos los sp que se refieren a esta tabla. Estoy enfrentando dificultades para encontrar la lista de todos los sp.Por favor sugiera alguna consulta asumiendo que el nombre de la tabla es 'x' y la base de datos es sql server 2005.

Author: Cœur, 2011-09-01

9 answers

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%TableNameOrWhatever%'

POR cierto here aquí hay un recurso útil para este tipo de preguntas: http://msdn.microsoft.com/en-us/library/ms345522%28v=SQL.90%29.aspx

 168
Author: Chains,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-23 20:38:50

Lo siguiente funciona en SQL2008 y superior. Proporciona una lista de procedimientos y funciones almacenados.

select distinct [Table Name] = o.Name, [Found In] = sp.Name, sp.type_desc
  from sys.objects o inner join sys.sql_expression_dependencies  sd on o.object_id = sd.referenced_id
                inner join sys.objects sp on sd.referencing_id = sp.object_id
                    and sp.type in ('P', 'FN')
  where o.name = 'YourTableName'
  order by sp.Name
 22
Author: Guy Hollington,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-07 15:10:06

Una forma sin consulta sería usar Sql Server Management Studio.

Busque la tabla, haga clic derecho y elija "Ver dependencias".

EDITAR

Pero, como dijeron los comentaristas, no es muy confiable.

 15
Author: Hans Kesting,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-05 19:38:32

A veces las consultas anteriores no darán el resultado correcto, hay un procedimiento almacenado construido disponible para obtener las dependencias de la tabla como:

EXEC sp_depends @objname = N'TableName';
 13
Author: Pramod Pawar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-12 03:02:20
SELECT
    o.name
FROM
    sys.sql_modules sm
INNER JOIN sys.objects o ON
    o.object_id = sm.object_id
WHERE
    sm.definition LIKE '%<table name>%'

Tenga en cuenta que esto también mostrará SPs donde el nombre de la tabla está en los comentarios o donde el nombre de la tabla es una subcadena de otro nombre de tabla que se está utilizando. Por ejemplo, si tiene tablas llamadas "test" y "test_2" e intenta buscar SPs con "test", obtendrá resultados para ambas.

 5
Author: Tom H,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-01 15:03:37

La siguiente consulta obtendrá todos los nombres de procedimientos almacenados y la definición correspondiente de los SP

select 
   so.name, 
   text 
from 
   sysobjects so, 
   syscomments sc 
where 
   so.id = sc.id 
   and UPPER(text) like '%<TABLE NAME>%'
 5
Author: Deepak Kothari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-16 13:54:04

La siguiente consulta solo funciona cuando se buscan dependencias en una tabla y no en una columna:

EXEC sp_depends @objname = N'TableName';

Sin embargo, la siguiente consulta es la mejor opción si desea buscar todo tipo de dependencias, no se pierde nada. En realidad da más información de la requerida.

 select distinct
        so.name
        --, text 
  from 
       sysobjects so, 
       syscomments sc 
  where 
     so.id = sc.id 
     and lower(text) like '%organizationtypeid%'
  order by so.name
 5
Author: Siraj Ansari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-26 11:23:01
SELECT DISTINCT OBJECT_NAME(OBJECT_ID),
object_definition(OBJECT_ID)
FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%' + 'table_name' + '%'

GO

Esto funcionará si tiene que mencionar el nombre de la tabla.

 2
Author: ric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-12 18:01:49

En management studio puede hacer clic con el botón derecho en la tabla y hacer clic en 'Ver dependencias' introduzca la descripción de la imagen aquí

Que se puede ver una lista de Objetos que tienen dependencias con su mesa :introduzca la descripción de la imagen aquí

 0
Author: nzrytmn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-05 09:11:19