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

Answer accepted by question author
  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. 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

Your answer

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