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

后端 未结 5 1046
不知归路
不知归路 2020-12-23 17:37

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 * 
         


        
相关标签:
5条回答
  • 2020-12-23 18:18

    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'
    
    0 讨论(0)
  • 2020-12-23 18:28

    right-click the view in the object-explorer, select "script view as...", then "create to" and then "new query editor window"

    0 讨论(0)
  • 2020-12-23 18:31

    if i understood you can do the following

    Right Click on View Name in SQL Server Management Studio -> Script View As ->CREATE To ->New Query Window

    0 讨论(0)
  • 2020-12-23 18:37

    Use sp_helptext before the view_name. Example:

    sp_helptext Example_1
    

    Hence you will get the query:

    CREATE VIEW dbo.Example_1
    AS
    SELECT       a, b, c
    FROM         dbo.table_name JOIN blah blah blah
    WHERE        blah blah blah
    

    sp_helptext will give stored procedures.

    0 讨论(0)
  • 2020-12-23 18:44

    Additionally, if you have restricted access to the database (IE: Can't use "Script Function as > CREATE To"), there is another option to get this query.

    Find your View > right click > "Design".

    This will give you the query you are looking for.

    0 讨论(0)
提交回复
热议问题