2

I have Python script in a file names "C:\Python\HL.py"

In this Python script there are predictive models and updates to some tables in my SQL database.

I want to call this file as a SQL Job

How can I achieve that?

This question is different

How to Execute Python Script as Administrator in SQL Server Agent Job

the provided solutions were through proxy command and scripting in the SQL.

I just want to call a Python .py file from within SQL Server Job

4

2 Answers 2

4

When you creating a SQL Server Agent job step, you should select:
1. Step type: Operating system (CmdExec)
2. Run as: here you can use Agent service account (less secure) or better option is to create dedicated service account and register it as a proxy account. If you decide to use Agent service account, you will be granting Server Admin privileges to python scripts.
3. In command type: "C:\Windows\System32\cmd.exe" "python C:\Python\HL.py"

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

Comments

2

Create a job step with this script

DECLARE @CMDSQL VARCHAR(1000)


SET @CMDSQL = 'cmd.exe py "C:\Python\HL.py" '
Exec master..xp_cmdshell @CMDSQL

enter image description here

But make sure your xp_cmdshell is enabled.

sp_configure 'show advanced options', '1'
RECONFIGURE
-- this enables xp_cmdshell
sp_configure 'xp_cmdshell', '1' 
RECONFIGURE

3 Comments

Using SQL Agent step is correct, but I disagree with using SQL Server service account, step type "TSQL" and xp_cmdshell.
I did that but I only got this : Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved. NULL C:\Windows\system32>
have you set your python path?

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.