¿Cómo puedo obtener nombres de columna de una tabla en SQL Server?
Me gustaría consultar el nombre de todas las columnas de una tabla. Encontré cómo hacer esto en:
, Pero necesito saber: ¿cómo se puede hacer esto en Microsoft SQL Server (2008 en mi caso)?
17 answers
Puede obtener esta información y mucho, mucho más consultando las vistas del Esquema de información .
Esta consulta de ejemplo:
SELECT *
FROM Northwind.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Customers'
Se puede hacer sobre todos estos objetos DB:
- CHECK_CONSTRAINTS
- COLUMN_DOMAIN_USAGE
- COLUMN_PRIVILEGES
- COLUMNAS
- CONSTRAINT_COLUMN_USAGE
- CONSTRAINT_TABLE_USAGE
- DOMAIN_CONSTRAINTS
- DOMINIOS
- KEY_COLUMN_USAGE
- PARÁMETROS
- REFERENTIAL_CONSTRAINTS
- RUTINAS
- ROUTINE_COLUMNS
- ESQUEMAS
- TABLE_CONSTRAINTS
- TABLE_PRIVILEGES
- CUADROS
- VIEW_COLUMN_USAGE
- VIEW_TABLE_USAGE
- VISTAS
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-12-04 11:53:27
Puede usar el procedimiento almacenado sp_columns que devolvería información perteneciente a todas las columnas de una tabla dada. Puede encontrar más información aquí http://msdn.microsoft.com/en-us/library/ms176077.aspx
También puede hacerlo mediante una consulta SQL. Algo como esto debería ayudar -
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('dbo.yourTableName')
O una variación sería:
SELECT o.Name, c.Name
FROM sys.columns c
JOIN sys.objects o ON o.object_id = c.object_id
WHERE o.type = 'U'
ORDER BY o.Name, c.Name
Esto obtiene todas las columnas de todas las tablas, ordenadas por el nombre de la tabla y luego por el nombre de la columna.
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-01-05 12:48:06
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'
Esto es mejor que obtener de sys.columns
porque muestra DATA_TYPE
directamente.
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-06-17 15:51:54
Puede usar sp_help
en SQL Server 2008.
sp_help <table_name>;
Atajo de teclado para el comando anterior: seleccione el nombre de la tabla (es decir, resalte) y presione ALT+F1 .
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-08-06 15:12:35
Al usar esta consulta se obtiene la respuesta:
select Column_name
from Information_schema.columns
where Table_name like 'table name'
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-03-26 14:21:04
Puede escribir esta consulta para obtener el nombre de la columna y todos los detalles sin usar INFORMATION_SCHEMA en MySQL:
SHOW COLUMNS FROM database_Name.table_name;
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-09-19 12:37:25
{Esta es otra variación utilizada para documentar una base de datos grande para la conversión (Editada para remove eliminar columnas estáticas)
SELECT o.Name as Table_Name
, c.Name as Field_Name
, t.Name as Data_Type
, t.length as Length_Size
, t.prec as Precision_
FROM syscolumns c
INNER JOIN sysobjects o ON o.id = c.id
LEFT JOIN systypes t on t.xtype = c.xtype
WHERE o.type = 'U'
ORDER BY o.Name, c.Name
{En la unión izquierda, c. type se reemplaza por c. xtype para obtener tipos varchar
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
2012-04-26 08:07:50
SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID('TABLE_NAME')
/ / TABLE_NAME es tu tabla
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-05-25 08:19:16
SELECT column_name, data_type, character_maximum_length, table_name,ordinal_position, is_nullable
FROM information_schema.COLUMNS WHERE table_name LIKE 'YOUR_TABLE_NAME'
ORDER BY ordinal_position
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
2012-02-23 18:14:14
Puedes probar esto.Esto da todos los nombres de columna con sus respectivos tipos de datos.
desc <TABLE NAME> ;
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-06-27 12:31:04
A esta pregunta SO le falta el siguiente enfoque:
-- List down all columns of table 'Logging'
select * from sys.all_columns where object_id = OBJECT_ID('Logging')
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-27 19:13:53
Simplemente ejecute este comando
EXEC sp_columns 'Su Nombre de tabla'
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-03-23 05:41:03
Se comprobará si el dado el table
es Mesa Base.
SELECT
T.TABLE_NAME AS 'TABLE NAME',
C.COLUMN_NAME AS 'COLUMN NAME'
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_NAME=C.TABLE_NAME
WHERE T.TABLE_TYPE='BASE TABLE'
AND T.TABLE_NAME LIKE 'Your Table Name'
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-04-08 14:28:01
Puede utilizar esta consulta
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'
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-06-30 07:43:33
SELECT c.Name
FROM sys.columns c
JOIN sys.objects o ON o.object_id = c.object_id
WHERE o.object_id = OBJECT_ID('TABLE_NAME')
ORDER BY c.Name
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-03-09 21:34:33
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'name_of_your_table'
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-12-04 16:04:09
Otra opción que podría decirse que es más intuitiva es:
SELECT [name]
FROM sys.columns
WHERE object_id = OBJECT_ID('[yourSchemaType].[yourTableName]')
Esto le da todos los nombres de sus columnas en una sola columna.
Si le importan otros metadatos, puede cambiar edit the SELECT STATEMENT A SELECT *
.
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-24 17:12:09