Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
En este tema se describe cómo enviar datos a un procedimiento almacenado como parámetro con valores de tabla cuando todos los valores están en memoria. Para obtener otro ejemplo que muestre los parámetros con valores de tabla, consulte Uso de Table-Valued Parámetros (ODBC).
Prerrequisito
En este procedimiento se supone que se ha ejecutado el siguiente Transact-SQL en el servidor:
create type TVParam as table(ProdCode integer, Qty integer)
create procedure TVPOrderEntry(@CustCode varchar(5), @Items TVPParam,
@OrdNo integer output, @OrdDate datetime output)
as
set @OrdDate = GETDATE();
insert into TVPOrd (OrdDate, CustCode)
values (@OrdDate, @CustCode) output OrdNo);
select @OrdNo = SCOPE_IDENTITY();
insert into TVPItem (OrdNo, ProdCode, Qty)
select @OrdNo, @Items.ProdCode, @Items.Qty
from @Items
Para enviar los datos
Declare variables para los parámetros SQL. En este caso, el valor de la tabla se mantiene completamente en memoria, por lo que los valores de las columnas del valor de tabla se declaran como matrices.
SQLRETURN r; // Variables for SQL parameters. #define ITEM_ARRAY_SIZE 20 SQLCHAR CustCode[6]; SQLCHAR *TVP = (SQLCHAR *) "TVParam"; SQLINTEGER ProdCode[ITEM_ARRAY_SIZE], Qty[ITEM_ARRAY_SIZE]; SQLINTEGER OrdNo; char OrdDate[23]; // Variables for indicator/length variables associated with parameters. SQLLEN cbCustCode, cbTVP, cbProdCode[ITEM_ARRAY_SIZE], cbQty[ITEM_ARRAY_SIZE], cbOrdNo, cbOrdDate;Enlace los parámetros. Los parámetros de enlace son un proceso de dos fases cuando se usan parámetros con valores de tabla. En la primera fase, los parámetros del paso para el procedimiento almacenado se enlazan de la manera normal, como se indica a continuación.
// Bind parameters for call to TVPOrderEntryDirect. // 1 - Custcode input r = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,SQL_VARCHAR, SQL_C_CHAR, 5, 0, CustCode, sizeof(CustCode), &cbCustCode); // 2 - Items TVP r = SQLBindParameter(hstmt, 2,// ParameterNumber SQL_PARAM_INPUT,// InputOutputType SQL_C_DEFAULT,// ValueType SQL_SS_TABLE,// Parametertype ITEM_ARRAY_SIZE,// ColumnSize: For a table-valued parameter this is the row array size. 0,// DecimalDigits: For a table-valued parameter this is always 0. TVP,// ParameterValuePtr: For a table-valued parameter this is the type name of the //table-valued parameter, and also a token returned by SQLParamData. SQL_NTS,// BufferLength: For a table-valued parameter this is the length of the type name or SQL_NTS. &cbTVP);// StrLen_or_IndPtr: For a table-valued parameter this is the number of rows actually used. // 3 - OrdNo output r = SQLBindParameter(hstmt, 3, SQL_PARAM_OUTPUT,SQL_INTEGER, SQL_C_LONG, 0, 0, &OrdNo, sizeof(SQLINTEGER), &cbOrdNo); // 4 - OrdDate output r = SQLBindParameter(hstmt, 4, SQL_PARAM_OUTPUT,SQL_TYPE_TIMESTAMP, SQL_C_CHAR, 23, 3, &OrdDate, sizeof(OrdDate), &cbOrdDate);La segunda fase del enlace de parámetros consiste en enlazar las columnas para el parámetro con valores de tabla. El foco del parámetro se establece primero en el ordinal del parámetro con valores de tabla. A continuación, las columnas del valor de tabla se enlazan mediante SQLBindParameter de la misma manera que si fueran parámetros del procedimiento almacenado, pero con ordinales de columna para ParameterNumber. Si hubiera más parámetros con valores de tabla, estableceríamos el foco en cada uno a su vez y enlazaría sus columnas. Por último, el foco del parámetro se restablece a 0.
// Bind columns for the table-valued parameter (param 2). // First set focus on param 2. r = SQLSetStmtAttr(hstmt, SQL_SOPT_SS_PARAM_FOCUS, (SQLPOINTER) 2, SQL_IS_INTEGER); // Col 1 - ProdCode r = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,SQL_INTEGER, SQL_C_LONG, 0, 0, ProdCode, sizeof(SQLINTEGER), cbProdCode); // Col 2 - Qty r = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT,SQL_INTEGER, SQL_C_LONG, 0, 0, Qty, sizeof(SQLINTEGER), cbQty); // Reset param focus. r = SQLSetStmtAttr(hstmt, SQL_SOPT_SS_PARAM_FOCUS, (SQLPOINTER) 0, SQL_IS_INTEGER);Rellene los búferes de parámetros.
cbTVPse establece en el número de filas que se enviarán al servidor.// Populate parameters. cbTVP = 0; // Number of rows available for input. strcpy_s((char *) CustCode, sizeof(CustCode), "CUST1"); cbCustCode = SQL_NTS; ProdCode[cbTVP] = 1215;cbProdCode[cbTVP] = sizeof(SQLINTEGER); Qty[cbTVP] = 5;cbQty[cbTVP] = sizeof(SQLINTEGER); cbTVP++; // Number of rows available for input ProdCode[cbTVP] = 1017;cbProdCode[cbTVP] = sizeof(SQLINTEGER); Qty[cbTVP] = 2;cbQty[cbTVP] = sizeof(SQLINTEGER); cbTVP++; // Number of rows available for input.Llame al procedimiento:
// Call the procedure. r = SQLExecDirect(hstmt, (SQLCHAR *) "{call TVPOrderEntry(?, ?, ?, ?)}",SQL_NTS);