使用記憶體中的所有值,將資料當做資料表值參數傳送 (ODBC)

本主題描述所有的值都在記憶體中時,如何將資料當做資料表值參數傳送至預存程序。 如需示範資料表值參數的其他範例,請參閱<使用資料表值參數 (ODBC)>。

必要條件

此程序假設已在伺服器上執行下列 Transact-SQL:

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

傳送資料

  1. 宣告 SQL 參數的變數。 在此情況下,資料表值會完整保留在記憶體中,因此會將資料表值資料行的值宣告為陣列。

    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. 繫結參數。 使用資料表值參數時,繫結參數是一個兩個階段的程序。 在第一個階段中,預存程序的 step 參數會以正常的方式繫結,如下所示。

    // 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. 參數繫結的第二個階段是建立資料表值參數的資料行。 參數焦點會先設定為資料表值參數的序數。 接著,資料表值的資料行會透過 SQLBindParameter,使用與預存程序參數相同的方式,但是使用 ParameterNumber 的資料行序數來繫結。 如果有其他資料表值參數,則會輪流將焦點設定為每個資料表值參數,並繫結其資料行。 最後,參數焦點就會重設為 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. 擴充參數緩衝區。 cbTVP 會設定為要傳送到伺服器的資料列數目。

    // 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. 呼叫程序:

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

請參閱

概念

ODBC 資料表值參數程式設計範例