Thursday, January 23, 2014

Digesting Sql Server View Definitions

I needed to parse a view definition and figure out the field mappings and underlying tables.  Just some tricks along the way.  I was using Sql Server 2008.

This will give you the entire view definition divided out into rows.  Each row constitutes 255 chars

  EXEC sp_helptext 'myViewName'
or
EXEC sp_help 'myViewName'

This gives you the entire view definition

select definition, *
from sys.objects     o
join sys.sql_modules m on m.object_id = o.object_id
where o.object_id = object_id( 'myViewName')
  and o.type      = 'V'


Getting all the tables and fields that a view is dependant upon

sp_depends @objname = N'dbo.myViewName'


Getting the columns in the view in an easier consumable format

SELECT
*
FROM  
  INFORMATION_SCHEMA.COLUMNS
WHERE  
  TABLE_NAME like ('%myViewName%')
ORDER BY
  ORDINAL_POSITION ASC;

No comments:

Post a Comment