Scheduling Python Notebook using SQL Server Agent

Muhammad Fawad 60 Reputation points
2023-12-21T11:14:42.4133333+00:00

I am trying to schedule a Python notebook by creating a job Using an SQL Server agent but when I try to execute this it gives me the following error.
12/21/2023 10:05:00,Duetto_Job,Error,1,SUN-SQL02\BI_SIHOT,Duetto_Job,Exec-Notebook,,Executed as user: NT Service\SQLAgent$BI_SIHOT. The job script encountered the following errors. These errors did not stop the script: A job step received an error at line 6 in a PowerShell script. The corresponding line is '$sqlResult = Invoke-Sqlcmd -Query $JSONTable -Database $TargetDatabase -MaxCharLength 2147483647 '. Correct the script and reschedule the job. The error information returned by PowerShell is: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider<c/> error: 40 - Could not open a connection to SQL Server) The system cannot find the file specified ' A job step received an error at line 6 in a PowerShell script. The corresponding line is '$sqlResult = Invoke-Sqlcmd -Query $JSONTable -Database $TargetDatabase -MaxCharLength 2147483647 '. Correct the script and reschedule the job. The error information returned by PowerShell is: ' '. Process Exit Code -1. The step failed.,00:00:17,0,0,,,,0

But when I run the same notebook manually, it works fine. Help me please

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Rob Sewell 86 Reputation points MVP
    2024-01-02T12:46:41.7266667+00:00

    Hi Muhammad Fawad

    You have created a Python Notebook which runs successfully as an individual notebook but you are trying to run the Notebook in a SQL Agent Job as a Notebook Job like this https://blog.robsewell.com/blog/running-jupyter-notebooks-as-agent-jobs/

    The error that you are receiving is from the Agent Job Step 'Exec Notebook' which you can find by editing the Agent Notebook Job in ADS and then the Job Step. My feeling is that it is not correctly locating the instance.

    User's image

    click in the job step and press edit

    User's image

    The problem is that in this code there is no value set for the Invoke-SqlCmd cmdlet so it is using the hostname which is obviously no good when you have a named instance. I get login failures on my default instance for the named instance agent service account when I try to run a Notebook Agent Job on a named instance.

    User's image

    A quick solution is to add this to the PowerShell code step at the top

    $SqlInstance = '{0}\{1}' -f "$(ESCAPE_SQUOTE(MACH))", "$(ESCAPE_SQUOTE(INST))"
    
    $PSDefaultParameterValues = @{
         "Invoke-SqlCmd:ServerInstance" = $SqlInstance 
    } 
    

    What is this doing?

    It is creating a variable named SqlInstance that is made up of the SQL Agent Job Tokens for the machine and the instance name

    Then it is setting that variable as the default value for the ServerInstance paramater of the Invoke-SqlCmd cmdlet for this session only

    This means that all of the times that the Invoke-SqlCmd cmdlet is called it will use the correct value.

    You should probably make this a default for you to add to any Notebook Agent job

    This resolved this error for me

    User's image

    Let me know how you get on

    1 person found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-12-21T22:37:48.35+00:00

    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

    This error means that the SQL Server instance you are trying to connect to was not reached. It could be because the instance is not running, you have fat-fingered the name, there is a firewall etc.

    Judging from the error message, you don't seem to have specified a server at all. The name of the service account suggests that this is a named instance, and if you are not specifying the server name, this means default instance on the local server.

    But when I run the same notebook manually, it works fine. Help me please

    Maybe you are specifying a correct server name then?

    2 people found this answer helpful.

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Olaf Helper 47,436 Reputation points
    2023-12-22T06:32:42.91+00:00

    12/21/2023 10:05:00,Duetto_Job,Error,1,SUN-SQL02\BI_SIHO

    So you have a named instance "BI_SIHO" running?

    You do not pass the server/instance name to the "Invoke-Sqlcmd" command using the "-ServerInstance" parameter. How could the cmd guess to which SQL Server you want to connect to? Impossible and so it fails.

    See Invoke-Sqlcmd


  4. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-12-29T22:32:31.3266667+00:00

    Now that you have seen the PowerShell code, the fix should be quite apparent. Modify this line:

    $sqlResult = Invoke-Sqlcmd -Query $JSONTable -Database $TargetDatabase -MaxCharLength 2147483647
    

    To read:

    $sqlResult = Invoke-Sqlcmd -Query $JSONTable -Database $TargetDatabase -MaxCharLength 2147483647 -Server "SUN-SQL02\\BI_SIHOT"
    

    I'm a little uncertain whether the backslash should be doubled. If the above fails, try without doubling.

    However, I can't say that I feel that this is satisfactory. There should be a way to specify the server name when you create the jobs. Or the PowerShell script should be generated to include the server name - after all, it seems to know on which instance the job was created.

    I've reached out to an MVP colleague who have worked more with notebooks than I do to see if there is something we are missing.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.