I'm interested in the most elegant way of creating a solution using all T-SQL that will allow me to target any view that is currently in the database using the WITH SCHEMABINDING option and dropping it then recreating it. We need to assume here that we don't have access to the original DDL scripts so the Views must be recreated only from what can be determined by the existing objects through T-SQL.
How I'm currently achieving this
So far I've been able to do this by looking for schema bound objects with a script like the one below.
SELECT * FROM sys.objects WHERE OBJECTPROPERTY(object_id, 'IsSchemaBound') = 1
Once I have each object I can use OBJECT_DEFINITION() function to script out the object itself. Everything is great so far except OBJECT_DEFINITION() doesn't include related indexes and perhaps other important details like permissions. In order to get around this I've found a script designed for scripting table indexes to make use of the sys.index system view which I adapted in order to find related indexes and manually produce the DDL statements to create these indexes. The problem with this approach has been that the manually created indexes doesn't appear to be as complete as the ones created through SMO and it is quite ugly trying to generate all of this in T-SQL.
Once I collect all this information into a temp table I use a cursor to execute dynamic drop statements for each View and then I have another cursor that runs later that recreates these Views.
This method works except I'm still not generating scripts equivalent to what SMO dose which lowers my confidence level in this approach. Also I can't stop from thinking that this would be much easier if I instead attempted to work directly with objects in a relational fashion instead of hacking around with generating DDL scripts. I could imagine doing a select into a temp table deleting records and then reinserting would be much cleaner to write/read and should be more efficient since it eliminates the need for generating temporary scripts and also the parsing of these scripts once they are executed. I'm I making sense or is generating DDL the recommended apporach. And if I'm to stick with the DDL generating mechanisms are they any ways I can simplify generating the index scripts so that they are closer to what SMO generates?