Is there a way to retrieve the view definition from a SQL Server using plain ADO?

前端 未结 6 618
攒了一身酷
攒了一身酷 2020-12-02 08:00

I\'m successfully extracting column definitions from databases hosted on a SQL server using the ADO Connection OpenSchema() call in its various incarnations so

相关标签:
6条回答
  • 2020-12-02 08:48
    SELECT definition, uses_ansi_nulls, uses_quoted_identifier, is_schema_bound  
    FROM sys.sql_modules  
    WHERE object_id = OBJECT_ID('your View Name');  
    
    0 讨论(0)
  • 2020-12-02 08:51

    Microsoft listed the following methods for getting the a View definition: http://technet.microsoft.com/en-us/library/ms175067.aspx


    USE AdventureWorks2012;
    GO
    SELECT definition, uses_ansi_nulls, uses_quoted_identifier, is_schema_bound
    FROM sys.sql_modules
    WHERE object_id = OBJECT_ID('HumanResources.vEmployee'); 
    GO
    

    USE AdventureWorks2012; 
    GO
    SELECT OBJECT_DEFINITION (OBJECT_ID('HumanResources.vEmployee')) 
    AS ObjectDefinition; 
    GO
    

    EXEC sp_helptext 'HumanResources.vEmployee';
    
    0 讨论(0)
  • 2020-12-02 08:53

    For users of SQL 2000, the actual command that will provide this information is:

    select c.text
    from sysobjects     o
    join syscomments    c on c.id = o.id
    where o.name = '<view_name_here>'
      and o.type      = 'V'
    
    0 讨论(0)
  • 2020-12-02 08:55

    Which version of SQL Server?

    For SQL Server 2005 and later, you can obtain the SQL script used to create the view like this:

    select definition
    from sys.objects     o
    join sys.sql_modules m on m.object_id = o.object_id
    where o.object_id = object_id( 'dbo.MyView')
      and o.type      = 'V'
    

    This returns a single row containing the script used to create/alter the view.

    Other columns in the table tell about about options in place at the time the view was compiled.

    Caveats

    • If the view was last modified with ALTER VIEW, then the script will be an ALTER VIEW statement rather than a CREATE VIEW statement.

    • The script reflects the name as it was created. The only time it gets updated is if you execute ALTER VIEW, or drop and recreate the view with CREATE VIEW. If the view has been renamed (e.g., via sp_rename) or ownership has been transferred to a different schema, the script you get back will reflect the original CREATE/ALTER VIEW statement: it will not reflect the objects current name.

    • Some tools truncate the output. For example, the MS-SQL command line tool sqlcmd.exe truncates the data at 255 chars. You can pass the parameter -y N to get the result with N chars.

    0 讨论(0)
  • 2020-12-02 08:57
    SELECT object_definition (OBJECT_ID(N'dbo.vEmployee'))
    
    0 讨论(0)
  • 2020-12-02 08:58

    You can get table/view details through below query.

    For table :sp_help table_name For View :sp_help view_name

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