Sql Server - Get view creation statement for existing view

前端 未结 6 823
灰色年华
灰色年华 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: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
    

提交回复
热议问题