How to run python code directly with correct syntax within sequel server management studio 2017

0

I wrote a python script that takes data from sql via "import pyodbc" The data this scrip pulls is parsed into a text message gateway API to send text messages to customers accordingly. This works fine in python.

However, now I want to write a sql stored procedure that will run every time an new invoice is generated in my business, the send the data for phone number + message through to this python python script within the same sql stored procedure.

The issue I am having right now is writing this python scrip with ssms 2017 and executing it without syntax error. Considering I am using sql 2017 I have enabled python and r respectively.

execute sp_execute_external_script
@language = N'Python',
@script =  N'

import africastalking

username = "sandbox"
apikey = "bf62be6"
africastalking.initialize(username, apikey)
sms = africastalking.SMS
recipients = ["+254797301255"]
message = ["Test from SQL"]
sender = "MegaLtd"

try:
    response = sms.send(message, recipients, sender)
    print(response)
except Exception as e:
    print(f"Houston, we have a problem {e}")

'

This is the error I receive

Msg 39004, Level 16, State 20, Line 2
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 2
An external script error occurred: 

Error in execution.  Check the output for more information.
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "C:\PROGRA~1\MICROS~3\MSSQL1~1.MPR\MSSQL\EXTENS~1\MPRYCESQLSEVER01\1D611E8A-CDE1-4F30-9FAC-0BB13871A3DE\sqlindb.py", line 59
    print(f"Houston, we have a problem {e}")
                                          ^
SyntaxError: invalid syntax

SqlSatelliteCall error: Error in execution.  Check the output for more information.
STDOUT message(s) from external script: 
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MPRYCESQLSEVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 406, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MPRYCESQLSEVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 291, in rx_native_call
    ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.
python
mysql
ssms-2017

1 Answer

0

Your environment most likely uses Python 3.5.2

https://docs.microsoft.com/en-us/sql/advanced-analytics/r/use-sqlbindr-exe-to-upgrade-an-instance-of-sql-server?view=sql-server-2017

However, f-strings have been introduced in Python 3.6

So you either have to somehow upgrade your Python, or just rewrite the exception handling, e.g. like this:

# ORIG: print(f"Houston, we have a problem {e}")
print("Houston, we have a problem {}".format(e))

If you're not sure which Python version your environment uses, you could check with

import sys
print(sys.version)
answered on Stack Overflow May 13, 2019 by Mike Scotty

User contributions licensed under CC BY-SA 3.0