Condividi tramite


Invio di dati come parametro con valori di tabella con tutti i valori in memoria (ODBC)

In questo argomento viene illustrato come inviare dati a una stored procedure come parametro con valori di tabella quando tutti i valori sono in memoria.

Prerequisiti

Questa procedura presuppone che sia stata eseguita l'istruzione Transact-SQL seguente sul server:

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

Per inviare i dati

  1. Dichiarare le variabili per i parametri SQL. In questo caso, il valore di tabella è contenuto completamente in memoria, pertanto i valori relativi alle colonne del valore di tabella vengono dichiarati come matrici.

    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;
    
  2. Associare i parametri. L'associazione dei parametri è un processo composto da due fasi quando si utilizzano i parametri con valori di tabella. Nella prima fase i parametri per la stored procedure vengono associati attraverso la modalità normale, nel modo seguente.

    // 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);
    
  3. La seconda fase di associazione dei parametri consiste nell'associare le colonne per il parametro con valori di tabella. Lo stato attivo del parametro viene impostato prima sull'ordinale del parametro con valori di tabella, quindi le colonne del valore di tabella vengono associate attraverso SQLBindParameter con le stesse modalità utilizzate per i parametri della stored procedure, ma con gli ordinali delle colonne per ParameterNumber. Se ci fossero più parametri con valori di tabella, lo stato attivo verrebbe impostato a sua volta su ciascuno di essi, seguito dall'associazione delle colonne. Lo stato attivo del parametro viene infine reimpostato su 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);
    
  4. Popolare i buffer dei parametri. cbTVP viene impostato sul numero di righe da inviare al server.

    // 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.
    
  5. Chiamare la routine:

    // Call the procedure.
    r = SQLExecDirect(hstmt, (SQLCHAR *) "{call TVPOrderEntry(?, ?, ?, ?)}",SQL_NTS);