我编写了一个 python 脚本,它通过“import pyodbc”从 sql 获取数据。这个脚本提取的数据被解析成一个文本消息网关 API,以相应地向客户发送文本消息。这在 python 中运行良好。
但是,现在我想编写一个 sql 存储过程,每次在我的业务中生成新发票时都会运行该存储过程,将电话号码 + 消息的数据发送到同一个 sql 存储过程中的这个 python python 脚本。
我现在遇到的问题是用 ssms 2017 编写这个 python 脚本并在没有语法错误的情况下执行它。考虑到我使用的是 sql 2017,我分别启用了 python 和 r。
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}")
'
这是我收到的错误
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.