Search code examples
sqlsql-serverview

How to get a view table query (code) in SQL Server 2008 Management Studio


I have a view in SQL Server 2008 and would like to view it in Management Studio.

Example:

--is the underlying query for the view Example_1
select * 
from table_aView 

View name: Example_1

How to get the query of the corresponding view table (query used to create the view)?


Solution

  • In Management Studio, open the Object Explorer.

    • Go to your database
    • There's a subnode Views
    • Find your view
    • Choose Script view as > Create To > New query window

    and you're done!

    enter image description here

    If you want to retrieve the SQL statement that defines the view from T-SQL code, use this:

    SELECT  
        m.definition    
    FROM sys.views v
    INNER JOIN sys.sql_modules m ON m.object_id = v.object_id
    WHERE name = 'Example_1'