在 pymssql 中调用 SQL SERVER 的存储过程时传递可选参数
Passing Optional parameters while calling SQL SERVER's stored procedure in pymssql
我正在使用 pymssql 库通过存储过程从 MS SQL 服务器检索数据。
在传递强制参数时,我没有任何问题。
如何在pymssql中传递可选参数?
我的存储过程有5个参数,2个是必需的,3个是可选的。
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2))
我必须按什么顺序传递可选参数?
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_1))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_2))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_3))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_1,optional_parameter_2,optional_parameter_3,))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2))
我想传递可选参数,并确保设置正确的参数而不是其他参数。
使用"EXEC"命令调用存储过程,它允许您确定要使用的参数:
sql = """EXEC PROCEDURE_NAME @mandatory_parameter1=?, @mandatory_parameter2=?, @optional_parameter3=?"""
params = (mandatory_parameter1_value, mandatory_parameter1_value, optional_parameter3_value)
procedure_connection.cursor.execute(sql, params)
我正在使用 pymssql 库通过存储过程从 MS SQL 服务器检索数据。 在传递强制参数时,我没有任何问题。
如何在pymssql中传递可选参数?
我的存储过程有5个参数,2个是必需的,3个是可选的。
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2))
我必须按什么顺序传递可选参数?
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_1))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_2))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_3))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2,optional_parameter_1,optional_parameter_2,optional_parameter_3,))
procedure_connection.cursor.callproc('PROCEDURE_NAME', (mandatory_parameter_1,mandatory_parameter_2))
我想传递可选参数,并确保设置正确的参数而不是其他参数。
使用"EXEC"命令调用存储过程,它允许您确定要使用的参数:
sql = """EXEC PROCEDURE_NAME @mandatory_parameter1=?, @mandatory_parameter2=?, @optional_parameter3=?"""
params = (mandatory_parameter1_value, mandatory_parameter1_value, optional_parameter3_value)
procedure_connection.cursor.execute(sql, params)