设置参数值Setting Parameter Values

若要设置参数的值,应用程序只需将绑定到参数的变量的值设置为。To set the value of a parameter, the application simply sets the value of the variable bound to the parameter. 如果设置了此值,则它并不重要,只要在执行语句之前设置该值。It is not important when this value is set, as long as it is set before the statement is executed. 应用程序可以在绑定变量之前或之后设置值,并且它可以根据需要更改多次。The application can set the value before or after binding the variable, and it can change the value as many times as it wants. 执行语句时,驱动程序只检索变量的当前值。When the statement is executed, the driver simply retrieves the current value of the variable. 当多次执行预定义语句时,此方法特别有用;每次执行语句时,应用程序都会为某些或所有变量设置新值。This is particularly useful when a prepared statement is executed more than once; the application sets new values for some or all of the variables each time the statement is executed. 有关此操作的示例,请参阅本部分前面的准备执行For an example of this, see Prepared Execution, earlier in this section.

如果在对SQLBindParameter的调用中绑定了长度/指示器缓冲区,则在执行该语句之前,必须将其设置为以下值之一:If a length/indicator buffer was bound in the call to SQLBindParameter, it must be set to one of the following values before the statement is executed:

  • 绑定变量中数据的字节长度。The byte length of the data in the bound variable. 仅当变量为字符或二进制(ValueType为 SQL_C_CHAR 或 SQL_C_BINARY)时,驱动程序才会检查此长度。The driver checks this length only if the variable is character or binary (ValueType is SQL_C_CHAR or SQL_C_BINARY).

  • SQL_NTS。SQL_NTS. 数据为以 null 值结束的字符串。The data is a null-terminated string.

  • SQL_NULL_DATA。SQL_NULL_DATA. 数据值为 NULL,驱动程序将忽略绑定变量的值。The data value is NULL, and the driver ignores the value of the bound variable.

  • SQL_DATA_AT_EXEC 或 SQL_LEN_DATA_AT_EXEC 宏的结果。SQL_DATA_AT_EXEC or the result of the SQL_LEN_DATA_AT_EXEC macro. 参数的值将与SQLPutData一起发送。The value of the parameter is to be sent with SQLPutData. 有关详细信息,请参阅本部分后面的发送长数据For more information, see Sending Long Data, later in this section.

下表显示了绑定变量的值和应用程序为各种参数值设置的长度/指示器缓冲区。The following table shows the values of the bound variable and the length/indicator buffer that the application sets for a variety of parameter values.

参数Parameter

value
参数Parameter

TRANSACT-SQL(SQL)

数据类型data type
变量(C)Variable (C)

数据类型data type
中的值Value in

绑定bound

可变variable
中的值Value in

长度/指示器length/indicator

缓冲区 [d]buffer[d]
"ABC""ABC" SQL_CHARSQL_CHAR SQL_C_CHARSQL_C_CHAR ABC\0 [a]ABC\0[a] SQL_NTS 或3SQL_NTS or 3
1010 SQL_INTEGERSQL_INTEGER SQL_C_SLONGSQL_C_SLONG 1010 --
1010 SQL_INTEGERSQL_INTEGER SQL_C_CHARSQL_C_CHAR 10 \ 0 [a]10\0[a] SQL_NTS 或2SQL_NTS or 2
1 P。M1 P.M. SQL_TYPE_TIMESQL_TYPE_TIME SQL_C_TYPE_TIMESQL_C_TYPE_TIME 13,0,0 [b]13,0,0[b] --
1 P。M1 P.M. SQL_TYPE_TIMESQL_TYPE_TIME SQL_C_CHARSQL_C_CHAR {t ' 13:00: 00 '} \ 0 [a],[c]{t '13:00:00'}\0[a], [c] SQL_NTS 或14SQL_NTS or 14
NullNULL SQL_SMALLINTSQL_SMALLINT SQL_C_SSHORTSQL_C_SSHORT -- SQL_NULL_DATASQL_NULL_DATA

[a] "\ 0" 表示 null 终止字符。[a] "\0" represents a null-termination character. 仅当长度/指示器缓冲区中的值 SQL_NTS 时,才需要 null 终止字符。The null-termination character is required only if the value in the length/indicator buffer is SQL_NTS.

[b] 此列表中的数字是存储在 TIME_STRUCT 结构的字段中的数字。[b] The numbers in this list are the numbers stored in the fields of the TIME_STRUCT structure.

[c] 字符串使用 ODBC date escape 子句。[c] The string uses the ODBC date escape clause. 有关详细信息,请参阅日期、时间和时间戳文本For more information, see Date, Time, and Timestamp Literals.

[d] 驱动程序必须始终检查此值,以确定它是否是一个特殊值,如 SQL_NULL_DATA。[d] Drivers must always check this value to see whether it is a special value, such as SQL_NULL_DATA.

在执行时,驱动程序使用参数值执行的操作取决于驱动程序。What a driver does with a parameter value at execution time is driver-dependent. 必要时,驱动程序将值从绑定变量的 C 数据类型和字节长度转换为参数的 SQL 数据类型、精度和小数位数。If necessary, the driver converts the value from the C data type and byte length of the bound variable to the SQL data type, precision, and scale of the parameter. 在大多数情况下,驱动程序会将值发送到数据源。In most cases, the driver then sends the value to the data source. 在某些情况下,它会将值设置为文本格式,并将其插入到 SQL 语句中,然后再将语句发送到数据源。In some cases, it formats the value as text and inserts it into the SQL statement before sending the statement to the data source.