Strange error, cannot script out jobs from SQL.

  • When trying to script out a current job on our MSX sql server, we receive this error,"Unable cast object of type 'system.dbnull' to type 'system.string'. (microsoft.sqlserver.smo)"

    I cannot find a lot of information on this topic. what would allow this job to run, yet prevent it from being scripted out and moved to our other MSX server?

    It will work on some jobs, just not most of them. I do have SA on the box.

    ===================================

    Script failed for Server '*******'. (Microsoft.SqlServer.Smo)

    ------------------------------

    For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.2500.0+((KJ_PCU_Main).110617-0026+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Script+Server&LinkId=20476

    ------------------------------

    Program Location:

    at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(DependencyCollection depList, SqlSmoObject[] objects)

    at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(Urn[] urns, SqlSmoObject[] objects)

    at Microsoft.SqlServer.Management.Smo.Scripter.Script(Urn[] urns, SqlSmoObject[] objects)

    at Microsoft.SqlServer.Management.Smo.Scripter.EnumScript(Urn[] urns)

    at Microsoft.SqlServer.Management.Smo.Scripter.Script(Urn[] urns)

    at Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ScriptGenerator.ScriptCreate(SqlTextWriter sqlwriter, Server server, Urn[] urns, SqlScriptOptions options)

    at Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ScriptNodeActionContext.Script(SqlTextWriter writer)

    at Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.SqlScriptMenu.OnScriptItemClick(Object sender, EventArgs e)

    ===================================

    Unable to cast object of type 'System.DBNull' to type 'System.String'. (Microsoft.SqlServer.Smo)

    ------------------------------

    Program Location:

    at Microsoft.SqlServer.Management.Smo.Agent.Job.GetJobScriptingScript(StringBuilder createQuery, ScriptingOptions so)

    at Microsoft.SqlServer.Management.Smo.Agent.Job.ScriptCreate(StringCollection queries, ScriptingOptions so)

    at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithListWorker(DependencyCollection depList, SqlSmoObject[] objects)

    at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(DependencyCollection depList, SqlSmoObject[] objects)

    .

  • Maybe a version mis-match between Management Studio and the installed version of SQL ?

  • Check the owner of the job(s) in querstion. It may be owned by an SID that does not resolve to a valid login which could be giving SMO heartburn.

    SELECT SUSER_SNAME(owner_sid)

    FROM msdb.dbo.sysjobs

    WHERE name = N'problem_job_name';

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • I tried that just now, it resolves to a good name. I wonder why it states system.dbnull I cannot figure that one out.

    These are Multiserver jobs. Though, even when targeted to the current server... it fails to script out.

    .

  • Sounds like a bug in SMO.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • opc.three (3/1/2013)


    Sounds like a bug in SMO.

    I agree. ^.^ I'm trying to track it down and fix it hopefully.

    .

  • In addition to job owner, the "run as" of a step may be associated with an invalid SID.

  • I had also had this issue today on a test server (e.g., desktop). I've been defecting and re-mastering a lot. I changed my service account today as well - using my first MSA. Someplace one of my jobs (1 of about 60) got stuck as neither completely master or completely local. I could not save changes to it. I could not script it out - got the same error as you did.

    I updated the system table for the job to remove the multi-server category. (In the past, I hacked a target job for fun to change it to a local job. I knew what to look for.)

    update msdb.dbo.sysjobs

    set category_id = 3

    where name = 'MyJobNameWithTheIssue'

    It was not enough because it had a related sysjobservers record with a non-zero server_id.

    DELETE FROM js

    FROM msdb.dbo.sysjobservers js

    INNER JOIN msdb.dbo.sysjobs j

    ON js.job_id = j.job_id

    WHERE j.name = 'MyJobNameWithTheIssue'

    This was enough. It became a local job that I could edit and script out. (I am very glad I did not have to cut and paste each step.)

    PS There is a bug that will not let you remove the default instance as a target from a named instance master on the same machine. (I submitted a bug for this recently. The code is trying to insure that a target is not the master before deleting the job. Good thing to do after a host rename, but it fails to account for named instances.) I had to put the master on the default for testing. Perhaps the issue with this job is related to the bug.

    RandyHelpdesk: Perhaps Im not the only one that does not know what you are doing. 😉

  • I ran into this problem as well. For me, it was due to having history for defected servers still around. Essentially, there was job run history for servers where there was no longer a Server_id in the msdb.dbo.systargetservers table. Something in the scripting process saw those NULL Server_ids in the history and threw the error.

    I found the solution on the following URL:

    https://sqlsanctum.wordpress.com/2015/02/12/multi-server-job-scripting-failure/

    You can quickly find if you have the same scenario with the query below (from the same URL):

    SELECT

    [JobName] = j.Name

    ,[JobServerId] = js.Server_id

    ,[TargetServerId] = ts.Server_id

    FROM msdb.dbo.sysjobservers js

    INNER JOIN msdb.dbo.sysjobs j ON j.job_id = js.job_id

    LEFT JOIN msdb.dbo.systargetservers ts ON ts.server_id = js.server_id

    WHERE j.category_id = 2 AND ts.server_id IS NULL

    You can delete the history by running the command below (also from the same URL) which should allow you to script the job:

    DELETE FROM js

    FROM msdb.dbo.sysjobservers js

    INNER JOIN msdb.dbo.sysjobs j ON j.job_id = js.job_id

    LEFT JOIN msdb.dbo.systargetservers ts ON ts.server_id = js.server_id

    WHERE j.name = 'JobName' AND ts.server_id IS NULL

  • Orlando Colamatteo (3/1/2013)


    Sounds like a bug in SMO.

    Agree with this.

    I think I had that problem with SSMS 2008R2 and had to upgrade with the next CU in order to overpass it.

    Igor Micev,My blog: www.igormicev.com

Viewing 10 posts - 1 through 9 (of 9 total)

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