Consulta SQL para buscar esquema de todas las tablas


Estoy trabajando en una base de datos SQL Server 2008 que tiene muchas tablas (alrededor de 200). Muchas de estas tablas contienen un campo con el nombre "CreatedDate". Estoy tratando de identificar todo el esquema de tabla con este campo en particular.

¿Hay una consulta SQL para hacer esto?

Author: CJBS, 2009-08-12

8 answers

Me gustaría consultar el information_schema - esto tiene vistas que son mucho más legibles que las tablas subyacentes.

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%create%'
 48
Author: Raj More,
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
2009-08-12 19:07:18

También puede intentar hacer esto utilizando una de las muchas herramientas de terceros que están disponibles para esto.

Las consultas son excelentes para búsquedas simples, pero si necesita hacer más manipulación con los datos, busque referencias y este es el lugar donde puede hacer un trabajo mucho mejor con estas.

Además, estos son muy útiles cuando algunos objetos están encriptados y necesita buscar

Estoy usando ApexSQL Search que es gratis, pero también hay muchos más (también gratis) en el mercado como Red Gate o SSMS Tool Pack.

 6
Author: Dwoolk,
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-06-04 11:13:00
  select object_name(c.object_id) as table_name
    , schema_name(t.schema_id) as schema_name
    from sys.columns c
    join sys.tables t on c.object_id = t.object_id
     where c.name=N'CreatedDate';

Se vuelve un poco más complicado si desea otras propiedades de tabla, pero se referirá a las vistas del catálogo de objetos como sys.cuadros, sys.columnas etc.

 5
Author: Remus Rusanu,
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
2009-08-12 16:56:53

Mi favorito...

SELECT objParent.name AS parent, obj.name, col.*
FROM sysobjects obj 
    LEFT JOIN syscolumns col
        ON obj.id = col.id
    LEFT JOIN sysobjects objParent
        ON objParent.id = obj.parent_obj
WHERE col.name LIKE '%Comment%'
   OR obj.name LIKE '%Comment%'

Arriba estoy buscando "Comentario".

Elimine los signos de porcentaje si desea una coincidencia directa.

Esto busca tablas, campos y cosas como nombres de clave primaria, restricciones, vistas, etc.

Y cuando desee buscar en StoredProcs después de monkeying con las tablas (y necesita hacer que los procs coincidan), use lo siguiente...

SELECT name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Comment%'

Espero que eso ayude, encuentro que estas dos consultas son extremadamente útiles.

 2
Author: Allbite,
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
2010-10-18 21:51:11

Para mí solo tengo acceso de lectura para ejecutar querys, así que necesito usar esta función a menudo aquí está lo que uso:

SELECT  *
FROM    INFORMATION_SCHEMA.TABLES
where   TABLES.TABLE_NAME like '%your table name here%'

Puede reemplazar .MESAS con .COLUMNAS entonces se vería así:

 SELECT *
 FROM   INFORMATION_SCHEMA.COLUMNS
 WHERE  columns.COLUMN_NAME like '%your column name here%'
 1
Author: UrbanBackpacker,
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-01-21 22:08:06

Utilice esta consulta:

SELECT 
    t.name AS table_name,
    SCHEMA_NAME(schema_id) AS schema_name,
    c.name AS column_name , *
FROM sys.tables AS t
    INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID 
Where 
    ( c.name LIKE '%' + '<ColumnName>' + '%' )
    AND 
    ( t.type = 'U' ) -- Use This To Prevent Selecting System Tables
 1
Author: Ardalan Shahgholi,
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
2016-02-16 10:42:27

Lo mismo pero de manera ANSI

SELECCIONAR * DE INFORMATION_SCHEMA.TABLA DONDE TABLE_NAME EN ( SELECCIONAR TABLE_NAME DE INFORMATION_SCHEMA.COLUMNA DONDE COLUMN_NAME = 'CreateDate' )

 0
Author: Tamil.SQL,
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
2009-08-12 17:10:20

No es necesario que escriba SQL Query para esto en SQL Server 2008.

En el Explorador de objetos SSMS elija Bases de datos o tablas de la base de datos requerida (si necesita buscar en una base de datos), abra el menú Ver Details> Detalles del Explorador de objetos (alternativamente presione F7), escriba %CreatedDate% en el cuadro de texto Buscar, ENTER, enjoy

 0
Author: Gennady Vanin Геннадий Ванин,
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
2010-10-19 01:18:55