47

I'm attempting to select the column names of a view in a similar way as selecting from information_schema.columns.

I can't seem to find a way to do this. Has anyone else done this before or know if it is even possible?

1

7 Answers 7

80

information_schema.columns.Table_name (at least under Sql Server 2000) includes views, so just use

SELECT * FROM information_schema.columns WHERE table_name = 'VIEW_NAME'
3
  • 7
    returns nothing in SSMS 2012
    – Imad
    Commented Jul 30, 2015 at 10:32
  • 9
    Works for me :-) But I'm smart enough to replace 'VIEW_NAME' with the actual name of my view ;-) Commented Oct 30, 2018 at 3:42
  • join INFORMATION_SCHEMA.TABLES (...) where TABLE_TYPE = 'VIEW'
    – Peet Brits
    Commented May 20, 2022 at 7:47
21

Try this:

SELECT *
FROM sys.views

This gives you the views as such - if you need the columns, use this:

SELECT * 
FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.YourViewNameHere')

Not sure how you can do it using the INFORMATION_SCHEMA - I never use that, since it feels rather "clunky" and unintuitive, compared to the sys schema catalog views.

See the MSDN docs on the Catalog Views for all the details of all the views available, and what information they might contain.

1
  • This gives me the name of all the views, but not the names of the columns in the view. Commented Jun 29, 2012 at 20:19
5

INFORMATION_SCHEMA views holds metadata about objects within database. INFORMATION_SCHEMA.COLUMNS uses underlying sys tables to retrive the metadata ( check sp_helptext 'master.Information_schema.columns' ). You can use this simpler query to select column names used in any view.

SELECT col.name 
FROM <db_name>.sys.columns col, <db_name>.sys.views vew
WHERE col.object_id = vew.object_id
AND vew.name = '<view_name>'
2
  • welcome to stack, whenever you answer a problem always try to explain how the solution will help the asker. please take some time and edit your answer Commented Sep 24, 2015 at 5:34
  • 2
    Instead of multiple tables on FROM clause, you can use JOIN. So the query will be SELECT col.name FROM sys.columns col JOIN sys.views vew ON col.object_id = vew.object_id WHERE vew.name = '<view_name>'
    – Arulkumar
    Commented Sep 24, 2015 at 6:06
5

I found this way working for views (SQL 2017). I was not able to get data from information_schema.columns and sys.columns:

    SELECT * FROM sys.all_columns WHERE object_id = OBJECT_ID('<view_name>')
2
SELECT distinct VIEW_NAME
  ,TABLE_SCHEMA
  ,TABLE_NAME
  ,COLUMN_NAME
FROM   INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
--WHERE  TABLE_SCHEMA = 'Person'
ORDER BY
   VIEW_NAME
  ,TABLE_SCHEMA
  ,TABLE_NAME
  ,COLUMN_NAME
1
  • 1
    This is a very interesting query but it actually returns the table columns used to get the results in the query, not the view columns returned. Therefore it is incorrect for the answer but very useful none the less.
    – AnthonyVO
    Commented Jan 15, 2018 at 18:56
1
select schema_name(v.schema_id) as schema_name,
   object_name(c.object_id) as view_name,
   c.column_id,
   c.name as column_name,
   type_name(user_type_id) as data_type,
   c.max_length,
   c.precision
from sys.columns c
join sys.views v 
 on v.object_id = c.object_id
order by schema_name,
     view_name,
     column_id;
1
  • Please, edit your question to add an explanation. See how to answer.
    – Syscall
    Commented Apr 20, 2021 at 19:28
1

I needed to create something similar, so I expounded on what Ghost suggested. I needed to create a top 1000 view for every view in a database order by the first ordinal column descending.

SELECT CONCAT (
              'CREATE VIEW dbo.temp_',
              v.name,
              ' AS select top (1000) * from ',
              OBJECT_SCHEMA_NAME (v.object_id),
              '.',
              v.name,
              ' ORDER BY [',
              c.COLUMN_NAME,
              '] DESC;'
          ) AS viewName
FROM sys.views AS v
    LEFT OUTER JOIN INFORMATION_SCHEMA.COLUMNS AS c
             ON v.name = c.TABLE_NAME
WHERE c.ORDINAL_POSITION = 1;

Not the answer you're looking for? Browse other questions tagged or ask your own question.