Sql Server - Get view creation statement for existing view

前端 未结 6 821
灰色年华
灰色年华 2021-02-06 03:48

Is there a way to get the statement that created a view for an existing view in SQL Server 2008? I thought there was a stored procedure or some metadata that had this data, but

相关标签:
6条回答
  • 2021-02-06 04:35

    It's in sys.sql_modules. Other schema tables like INFORMATION_SCHEMA ones only contain the first 4000 characters of the definition (they truncate).

    0 讨论(0)
  • 2021-02-06 04:40

    In case it helps someone in the future, here's a little script I put together to output the creation script for all views in a database.

    DECLARE @definition varchar(max)
    DECLARE @view CURSOR
    SET @view = CURSOR FOR
        SELECT m.definition FROM sys.views v INNER JOIN sys.sql_modules m ON m.object_id = v.object_id
    OPEN @view
    FETCH NEXT FROM @view INTO @definition
    WHILE @@FETCH_STATUS = 0 BEGIN
        PRINT @definition
        PRINT 'GO'
        FETCH NEXT FROM @view INTO @definition
    END CLOSE @view DEALLOCATE @view
    
    0 讨论(0)
  • 2021-02-06 04:42

    Right click on the view and script it.

    0 讨论(0)
  • 2021-02-06 04:45

    Right click on the View name, then left click "SCRIPT VIEW as", then left click "ALTER TO", then left click "NEW QUERY EDITOR WINDOW" -- bingo, your there! To print, click on the script screen and then send to your printer using your toolbar printer icon or click on FILE>>PRINT. Of course, be careful to exit without making any changes. This works the same for stored procedures.

    0 讨论(0)
  • 2021-02-06 04:48

    You can see the script as code, and copy paste it into an editor like this:

    SELECT 
        v.TABLE_NAME, 
        v.VIEW_DEFINITION 
    FROM 
        INFORMATION_SCHEMA.VIEWS v 
    WHERE 
        v.TABLE_NAME LIKE '%%'
    

    and insert the view name you want.

    0 讨论(0)
  • 2021-02-06 04:50

    Have you had a look at sp_helptext?

    sp_helptext 'dbo.name_of_view'
    

    SQL SERVER – Stored Procedure to display code (text) of Stored Procedure, Trigger, View or Object

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