Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql-server

ado

I'm successfully extracting column definitions from databases hosted on a SQL server using the ADO Connection OpenSchema() call in its various incarnations so I can programmatically recreate those tables in another SQL database. So far, so good.

The main interaction with the above tables happens using multiple views; while OpenSchema() is able to return the column definitions for the view in the same way that it returns column definitions for a table, a crucial bit of information is missing - which table and column in the underlying tables the column in the view maps to.

I tried to access the SQL command used to create the view using ADOX Catalog Views, but it appears that the OLEDB driver for SQL Server that we're using doesn't support this functionality.

Is there any way to get at this information for the view configuration via ADO, either in a way that states "ColumnX maps to ColumnY in table Z" or in the form of the actual SQL command used to create the view?

like image 674
Timo Geusch Avatar asked Jan 22 '11 00:01

Timo Geusch


People also ask

How can we obtain the definition of a view in SQL?

In Object Explorer, expand the database that contains the view to which you want to view the properties, and then expand the Views folder. Right-click the view of which you want to view the properties and select View Dependencies. Select Objects that depend on [view name] to display the objects that refer to the view.

Can we retrieve data from view in SQL?

In a VIEW, we can also control user security for accessing the data from the database tables. We can allow users to get the data from the VIEW, and the user does not require permission for each table or column to fetch data.


1 Answers

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.

like image 145
Nicholas Carey Avatar answered Sep 20 '22 17:09

Nicholas Carey