1

I am trying to force SMO to schema qualify object names for stored procedures, UDFs and views, but for some reason it doesn't work. I have got a stored procedure without a schema and need to have the default schema pre-pended to it:

I have this code:

var procs = from sp in _smoDB.StoredProcedures.OfType<StoredProcedure>()
              where !sp.IsSystemObject && !sp.IsEncrypted
              select sp;

foreach ( StoredProcedure sproc in procs ) {

    var script = sproc.Script( ScriptOption.SchemaQualify );
    var scriptText = script[3];

}

When I access the script text it isn't putting the schema name (dbo) in front of the stored prcocedure. Can anyone explain this?

1 Answer 1

4

I haven't worked with SMO in 2008/R2 (only 2005), so I may be missing something, but here's what's I'd try:

var procs = from sp in _smoDB.StoredProcedures.OfType<StoredProcedure>()
            where !sp.IsSystemObject && !sp.IsEncrypted               
            select sp; 

var options = new ScriptOptions();         // reusable ScriptOptions object
options.SchemaQualify = true;
options.EnforceScriptingOptions = true;    // surprise!
foreach ( StoredProcedure sproc in procs ) 
{
    var script = sproc.Script( options );     
    var scriptText = script[3];  
}

(I haven't tested this)

I'm guessing the stored procedure was originally created without specifying the schema, and that is what is being scripted. To get a schema-qualified script, you'll need to enforce the scripting options and will lose any comments and formatting (here's the article from MSDN).

I believe this will apply to Views and Functions as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Setting the .EnforceScriptingOptons = True scripting option has done the trick. Thanks for this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.