过程参数Procedure Parameters

过程调用中的参数可以是输入、输入/输出或输出参数。Parameters in procedure calls can be input, input/output, or output parameters. 这不同于所有其他 SQL 语句中的参数,这些语句始终是输入参数。This is different from parameters in all other SQL statements, which are always input parameters.

输入参数用于将值发送到过程。Input parameters are used to send values to the procedure. 例如,假设 Parts 表中包含 PartID、Description 和 Price 列。For example, suppose the Parts table has PartID, Description, and Price columns. InsertPart 过程可能会为表中的每个列提供一个输入参数。The InsertPart procedure might have an input parameter for each column in the table. 例如:For example:

{call InsertPart(?, ?, ?)}  

SQLExecDirectSQLExecute返回 SQL_SUCCESS、SQL_SUCCESS_WITH_INFO、SQL_ERROR、SQL_INVALID_HANDLE 或 SQL_NO_DATA 之前,驱动程序不能修改输入缓冲区的内容。A driver should not modify the contents of an input buffer until SQLExecDirect or SQLExecute returns SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_ERROR, SQL_INVALID_HANDLE, or SQL_NO_DATA. SQLExecDirectSQLExecute返回 SQL_NEED_DATA 或 SQL_STILL_EXECUTING 时,不应修改输入缓冲区的内容。The contents of the input buffer should not be modified while SQLExecDirect or SQLExecute returns SQL_NEED_DATA or SQL_STILL_EXECUTING.

输入/输出参数用于将值发送到过程,并从过程检索值。Input/output parameters are used both to send values to procedures and retrieve values from procedures. 同时使用与 input 和 output 参数相同的参数会造成混淆,应避免这样做。Using the same parameter as both an input and an output parameter tends to be confusing and should be avoided. 例如,假设某一过程接受订单 ID 并返回该客户的 ID。For example, suppose a procedure accepts an order ID and returns the ID of the customer. 可以使用单个输入/输出参数定义此参数:This can be defined with a single input/output parameter:

{call GetCustID(?)}  

使用两个参数可能更好:订单 ID 的输入参数以及客户 ID 的输出或输入/输出参数:It might be better to use two parameters: an input parameter for the order ID and an output or input/output parameter for the customer ID:

{call GetCustID(?, ?)}  

Output 参数用于检索过程的返回值,并从过程参数中检索值;返回值的过程有时称为函数Output parameters are used to retrieve the procedure return value and to retrieve values from procedure arguments; procedures that return values are sometimes known as functions. 例如,假设刚才提到的GetCustID过程返回一个值,该值指示它是否能够查找顺序。For example, suppose the GetCustID procedure just mentioned returns a value that indicates whether it was able to find the order. 在下面的调用中,第一个参数是一个输出参数,用于检索过程返回值,第二个参数是用于指定顺序 ID 的输入参数,第三个参数是用于检索客户 ID 的输出参数:In the following call, the first parameter is an output parameter used to retrieve the procedure return value, the second parameter is an input parameter used to specify the order ID, and the third parameter is an output parameter used to retrieve the customer ID:

{? = call GetCustID(?, ?)}  

驱动程序在过程中处理输入和输入/输出参数的值,而不是其他 SQL 语句中的输入参数。Drivers handle values for input and input/output parameters in procedures no differently than input parameters in other SQL statements. 执行语句时,它们会检索绑定到这些参数的变量的值,并将它们发送到数据源。When the statement is executed, they retrieve the values of the variables bound to these parameters and send them to the data source.

执行语句之后,驱动程序将输入/输出和输出参数的返回值存储在绑定到这些参数的变量中。After the statement has been executed, drivers store the returned values of input/output and output parameters in the variables bound to those parameters. 在提取了过程返回的所有结果并将SQLMoreResults返回 SQL_NO_DATA 之前,不一定要设置这些返回值。These returned values are not guaranteed to be set until after all results returned by the procedure have been fetched and SQLMoreResults has returned SQL_NO_DATA. 如果执行语句导致错误,则输入/输出参数缓冲区或输出参数缓冲区的内容不确定。If executing the statement results in an error, the contents of the input/output parameter buffer or output parameter buffer are undefined.

应用程序调用SQLProcedure来确定过程是否具有返回值。An application calls SQLProcedure to determine whether a procedure has a return value. 它调用SQLProcedureColumns来确定每个过程参数的类型(返回值、输入、输入/输出或输出)。It calls SQLProcedureColumns to determine the type (return value, input, input/output, or output) of each procedure parameter.