当使用sp_execute_external_script从 SQL Server 2017 执行 Python 时,传递到脚本中的 SQL 参数将成为 Python 中的全局变量,而输入数据集则成为局部变量。
为什么范围不同?为什么不都成为局部变量或都成为全局变量?
EXEC sp_execute_external_script
@language = N'python',
@script = N'
print("Is InputDataSet global? " + str("InputDataSet" in globals()))
print("Is InputDataSet local? " + str("InputDataSet" in locals()))
print("Is i (passed in parameter) global? " + str("i" in globals()))
print("Is i (passed in parameter) local? " + str("i" in locals()))
',
@input_data_1 = N'select 1',
@params = N'@i INT',
@i = 10;
产生:
Is InputDataSet global? False
Is InputDataSet local? True
Is i (passed in parameter) global? True
Is i (passed in parameter) local? False