Determine table referenced in a view in SQL Server

不羁的心 提交于 2019-11-29 09:24:35

问题


How can i get to know the tables used in a view in SQL Server? Is there a script or a tool that could let me know the tables used in a view and can also list down the fields?

Hope this clears the question. Let me know if not.

Please guide! Thanks!


回答1:


select
  cols.*
from
  sys.sql_expression_dependencies objs
  outer apply sys.dm_sql_referenced_entities ( OBJECT_SCHEMA_NAME(objs.referencing_id) + N'.' + object_name(objs.referencing_id), N'OBJECT' ) as cols
where
  objs.referencing_id = object_id('view_name_here')

Reference: sys.dm_sql_referenced_entities (Transact-SQL) .




回答2:


The simplest way to see the content of (most) objects would be:

sp_helptext blah

Where you substitute blah with the name of the object. This would yield the actual code which created the object. in this case, for instance it could result in:

CREATE VIEW blah
AS
  select blah.column1,blah.column2 from blah_table



回答3:


You can use DISTINCT to get only the tables.

  Select   DISTINCT cols.referenced_entity_name from  
   sys.sql_expression_dependencies objs   outer apply
   sys.dm_sql_referenced_entities (
   OBJECT_SCHEMA_NAME(objs.referencing_id) + N'.' +
   object_name(objs.referencing_id), N'OBJECT' ) as cols where  
   objs.referencing_id = object_id('viewname')


来源:https://stackoverflow.com/questions/8308252/determine-table-referenced-in-a-view-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!