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

asmgx
  • 7,328
  • 15
  • 82
  • 143
  • refer to documentation on SQL Agent https://learn.microsoft.com/en-us/sql/ssms/agent/sql-server-agent?view=sql-server-2017 – Squirrel Sep 30 '19 at 01:39
  • @Squirrel there is nothing about python there! – asmgx Sep 30 '19 at 01:40
  • execute it as Subsystems `Operating System` command – Squirrel Sep 30 '19 at 01:42
  • 1
    Possible duplicate of [How to Execute Python Script as Administrator in SQL Server Agent Job](https://stackoverflow.com/questions/54680105/how-to-execute-python-script-as-administrator-in-sql-server-agent-job) – Piotr Palka Sep 30 '19 at 02:24

2 Answers2

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"

asmgx
  • 7,328
  • 15
  • 82
  • 143
Piotr Palka
  • 3,086
  • 1
  • 9
  • 17
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
Ed Bangga
  • 12,879
  • 4
  • 16
  • 30