Can''t script indexes for views using SQL-DMO

  • I'm using SQL-DMO with C# and have no problem scripting out portions of our databases however when I try to script out indexes on views, I get nothing in return even though my views do have indexes on them. The Microsoft SQL Server documentation states that you can script out indexes for both tables and views, however I've had no luck. My code looks like:

    foreach(SQLDMO.View2 view in database.Views){

    if (!view.SystemObject){

    string objectScriptText = view.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_NonClusteredIndexes,

    "",

    SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_UnicodeFile);

    if (objectScriptText != null){

    objectScriptText = objectScriptText.Trim();

    if (objectScriptText.Length > 0){

    string filePath = CreateFilePath(subDirectoryName, view.Name, scriptType);

    WriteFile(filePath, objectScriptText, view.Name, appendFile);

    appendFile = true;

    }

    }

    }

    }//foreach

  • try iterating the index objects (using index2) - foreach(SQLDMO.Index2 in View2.indexes){//your code}

     

     

    regards,

    Mark Baekdal

    MSN m_baekdal@hotmail.com

    +44 (0)141 416 1490

    +44 (0)208 241 1762

    http://www.dbghost.com

    http://www.innovartis.co.uk

    Build, Comparison and Synchronization from Source Control = Database change management for SQL Server

     

     

  • I can get the indexes to script if I also script the view itself using SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default. I was attempting to script out the indexes to a separate file from the script that creates the view. I'll give your solution a try. Thanks

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply