Aracılığıyla paylaş


Konumlandırılmış Güncelleştirme ve Silme Deyimleri

Uygulamalar, konumlanmış bir güncelleştirme veya silme ifadeyle sonuç kümesindeki geçerli satırı güncelleştirebilir veya silebilir. Konumlanmış güncelleştirme ve silme deyimleri bazı veri kaynakları tarafından desteklenir, ancak tümü tarafından desteklenmez. Bir veri kaynağının konumlanmış güncelleştirme ve silme deyimlerini destekleyip desteklemediğini belirlemek için, uygulama SQL_DYNAMIC_CURSOR_ATTRIBUTES1, SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1, SQL_KEYSET_CURSOR_ATTRIBUTES1 veya SQL_STATIC_CURSOR_ATTRIBUTES1 InfoType (imlecin türüne bağlı olarak) ile SQLGetInfo çağırır. ODBC imleç kitaplığının konumlandırılmış güncelleştirme ve silme deyimlerinin simülasyonunu yaptığını unutmayın.

Konumlandırılmış güncelleştirme veya silme deyimini kullanmak için uygulamanın SELECT FOR UPDATE deyimiyle bir sonuç kümesi oluşturması gerekir. Bu deyimin söz dizimi şöyledir:

SELECT [ALL | DISTINCT] select-list

FROMtablo referans listesi

[WHEREarama koşulu]

GÜNCELLEME İÇİN [sütun-adı [,sütun-adı]...]

Uygulama daha sonra imleci güncelleştirilecek veya silinecek satıra konumlandıracak. Gerekli satırı içeren bir satır kümesini almak için SQLFetchScroll'ı çağırarak ve satır kümesi imlecini bu satıra konumlandırmak için SQLSetPos'ı çağırarak bunu yapabilir. Uygulama daha sonra konumlanmış update veya delete deyimini sonuç kümesi tarafından kullanılan deyimden farklı bir deyimde yürütür. Bu deyimlerin söz dizimi şöyledir:

UPDATEtablo adı

SETsütun tanımlayıcısı= {expression | NULL}

[,kolon-belirleyici= {ifade | NULL}]...

GÜNCEL OLDUĞU YERimleç-adı

DELETE FROMtable-nameWHERE CURRENT OFcursor-name

Bu deyimlerin imleç adı gerektirdiğine dikkat edin. Uygulama, sonuç kümesini oluşturan deyimi yürütmeden önce SQLSetCursorName ile bir imleç adı belirtebilir veya imleç oluşturulduğunda veri kaynağının otomatik olarak bir imleç adı oluşturmasına izin verebilir. İkinci durumda, uygulama bu imleç adını SQLGetCursorName çağırarak konumlanmış güncelleştirme ve silme deyimlerinde kullanmak üzere alır.

Örneğin, aşağıdaki kod kullanıcının Müşteriler tablosunda gezinmesine, müşteri kayıtlarını silmesine veya adreslerini ve telefon numaralarını güncelleştirmesine olanak tanır. Müşterilerin sonuç kümesini oluşturmadan önce bir imleç adı belirtmek için SQLSetCursorName'i çağırır ve üç deyim tanıtıcısı kullanır: sonuç kümesi için hstmtCust , konumlandırılmış bir güncelleştirme deyimi için hstmtUpdate ve konumlandırılmış delete deyimi için hstmtDelete . Kod, konumlu güncelleme ifadesindeki parametrelere ayrı değişkenler bağlayabilir, ancak satır kümesi tamponlarını günceller ve bu tamponların öğelerini bağlar. Bu, satır kümesi arabelleklerinin güncelleştirilmiş verilerle eşitlenmesini sağlar.

#define POSITIONED_UPDATE 100  
#define POSITIONED_DELETE 101  
  
SQLUINTEGER    CustIDArray[10];  
SQLCHAR        NameArray[10][51], AddressArray[10][51],   
               PhoneArray[10][11];  
SQLINTEGER     CustIDIndArray[10], NameLenOrIndArray[10],   
               AddressLenOrIndArray[10],  
               PhoneLenOrIndArray[10];  
SQLUSMALLINT   RowStatusArray[10], Action, RowNum;  
SQLHSTMT       hstmtCust, hstmtUpdate, hstmtDelete;  
  
// Set the SQL_ATTR_BIND_TYPE statement attribute to use column-wise   
// binding. Declare the rowset size with the SQL_ATTR_ROW_ARRAY_SIZE   
// statement attribute. Set the SQL_ATTR_ROW_STATUS_PTR statement   
// attribute to point to the row status array.  
SQLSetStmtAttr(hstmtCust, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0);  
SQLSetStmtAttr(hstmtCust, SQL_ATTR_ROW_ARRAY_SIZE, 10, 0);  
SQLSetStmtAttr(hstmtCust, SQL_ATTR_ROW_STATUS_PTR, RowStatusArray, 0);  
  
// Bind arrays to the CustID, Name, Address, and Phone columns.  
SQLBindCol(hstmtCust, 1, SQL_C_ULONG, CustIDArray, 0, CustIDIndArray);  
SQLBindCol(hstmtCust, 2, SQL_C_CHAR, NameArray, sizeof(NameArray[0]),  
            NameLenOrIndArray);  
SQLBindCol(hstmtCust, 3, SQL_C_CHAR, AddressArray, sizeof(AddressArray[0]),  
         AddressLenOrIndArray);  
SQLBindCol(hstmtCust, 4, SQL_C_CHAR, PhoneArray, sizeof(PhoneArray[0]),  
            PhoneLenOrIndArray);  
  
// Set the cursor name to Cust.  
SQLSetCursorName(hstmtCust, "Cust", SQL_NTS);  
  
// Prepare positioned update and delete statements.  
SQLPrepare(hstmtUpdate,  
   "UPDATE Customers SET Address = ?, Phone = ? WHERE CURRENT OF Cust",  
   SQL_NTS);  
SQLPrepare(hstmtDelete, "DELETE FROM Customers WHERE CURRENT OF Cust", SQL_NTS);  
  
// Execute a statement to retrieve rows from the Customers table.  
SQLExecDirect(hstmtCust,  
   "SELECT CustID, Name, Address, Phone FROM Customers FOR UPDATE OF Address, Phone",  
   SQL_NTS);  
  
// Fetch and display the first 10 rows.  
SQLFetchScroll(hstmtCust, SQL_FETCH_NEXT, 0);  
DisplayData(CustIDArray, CustIDIndArray, NameArray, NameLenOrIndArray, AddressArray,  
            AddressLenOrIndArray, PhoneArray, PhoneLenOrIndArray, RowStatusArray);  
  
// Call GetAction to get an action and a row number from the user.  
while (GetAction(&Action, &RowNum)) {  
   switch (Action) {  
  
      case SQL_FETCH_NEXT:  
      case SQL_FETCH_PRIOR:  
      case SQL_FETCH_FIRST:  
      case SQL_FETCH_LAST:  
      case SQL_FETCH_ABSOLUTE:  
      case SQL_FETCH_RELATIVE:  
         // Fetch and display the requested data.  
         SQLFetchScroll(hstmtCust, Action, RowNum);  
         DisplayData(CustIDArray, CustIDIndArray, NameArray, NameLenOrIndArray,  
                     AddressArray, AddressLenOrIndArray, PhoneArray,  
                     PhoneLenOrIndArray, RowStatusArray);  
         break;  
  
      case POSITIONED_UPDATE:  
         // Get the new data and place it in the rowset buffers.  
         GetNewData(AddressArray[RowNum - 1], &AddressLenOrIndArray[RowNum - 1],  
                     PhoneArray[RowNum - 1], &PhoneLenOrIndArray[RowNum - 1]);  
  
         // Bind the elements of the arrays at position RowNum-1 to the   
         // parameters of the positioned update statement.  
         SQLBindParameter(hstmtUpdate, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,  
                           50, 0, AddressArray[RowNum - 1], sizeof(AddressArray[0]),  
                           &AddressLenOrIndArray[RowNum - 1]);  
         SQLBindParameter(hstmtUpdate, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,  
                           10, 0, PhoneArray[RowNum - 1], sizeof(PhoneArray[0]),  
                           &PhoneLenOrIndArray[RowNum - 1]);  
  
         // Position the rowset cursor. The rowset is 1-based.  
         SQLSetPos(hstmtCust, RowNum, SQL_POSITION, SQL_LOCK_NO_CHANGE);  
  
         // Execute the positioned update statement to update the row.  
         SQLExecute(hstmtUpdate);  
         break;  
  
      case POSITIONED_DELETE:  
         // Position the rowset cursor. The rowset is 1-based.  
         SQLSetPos(hstmtCust, RowNum, SQL_POSITION, SQL_LOCK_NO_CHANGE);  
  
         // Execute the positioned delete statement to delete the row.  
         SQLExecute(hstmtDelete);  
         break;  
   }  
}  
  
// Close the cursor.  
SQLCloseCursor(hstmtCust);