Using SQLPrimaryKeys ODBC

I had created a small program based on WinAPIs that reads from SQL Server 2008 R2. I am using ODBC 3 to connect to database and doing different statements (select, insert, update and delete). Everything is running properly.

However, due to some dynamic statements that are creating on run-time, I need to know the primary key of a specific table.
I had used SQLPrimaryKeys; Upon execution, it is returning SQL_SUCCESS. However, I don't know how to collect the returned values. As per documentation, the returned values (primary key name) are set in "Result Set". But how could I access this result set?

Thanks in advance for any help
Last edited on
closed account (z05DSL3A)
Take a look at the example here:
https://msdn.microsoft.com/en-us/library/ms709315(v=vs.85).aspx
Thanks for your update. It was really helpful. However, I was not able to retrieve the primary key name. Below is the code if you can help or find my bug:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
SQLRETURN BsolSQL_PrimaryKeys (SQLHSTMT hStmt_p, SQLWCHAR* ctlgName_p, SQLSMALLINT cCtlg_p, 
							   SQLWCHAR* schema_p, SQLSMALLINT cSchema_p, 
							   SQLWCHAR* table_p, SQLSMALLINT cTable_p) {
	SQLRETURN return_v;

	return_v = SQLPrimaryKeys (hStmt_p, ctlgName_p, cCtlg_p, schema_p, cSchema_p, table_p, cTable_p);

	if (return_v == SQL_ERROR || return_v == SQL_INVALID_HANDLE) {
		BsolSQLShowError (SQL_HANDLE_STMT, hStmt_p);
		return_v = -1;
	}

	return return_v;
}

wstring BsolGetPrimaryKeys (SQLHANDLE hdlConn_p, wchar_t * tblName_p) {
	wchar_t * ctgName_v, * schmName_v, * tblName_v, * colName_v, * pkName_v;
	int seqNum_v, sizeOut_v;
	wstring	wsPkName_v;

	SQLRETURN		return_v;
	SQLSMALLINT		colLen_v, schmLen_v, tblLen_v;
	SQLHSTMT		hdlStmt_v;

	// Initialize Statement
	return_v = BsolSQL_AllocStmt (hdlConn_p, &hdlStmt_v);
	// Initialize all Variables
	colLen_v = 50;
	sizeOut_v = 50;
	seqNum_v = 0;
	ctgName_v = TEXT("");
	schmName_v = TEXT("");
	tblName_v = TEXT("");
	colName_v = TEXT("");
	pkName_v = TEXT("");
	// Bind column iCol_v
	return_v = SQLBindCol (hdlStmt_v, 1, SQL_C_WCHAR, ctgName_v, colLen_v, (SQLINTEGER*)&sizeOut_v);
	return_v = SQLBindCol (hdlStmt_v, 2, SQL_C_WCHAR, schmName_v, colLen_v, (SQLINTEGER*)&sizeOut_v);
	return_v = SQLBindCol (hdlStmt_v, 3, SQL_C_WCHAR, tblName_v, colLen_v, (SQLINTEGER*)&sizeOut_v);
	return_v = SQLBindCol (hdlStmt_v, 4, SQL_C_WCHAR, colName_v, colLen_v, (SQLINTEGER*)&sizeOut_v);
	return_v = SQLBindCol (hdlStmt_v, 5, SQL_C_SLONG, &seqNum_v, colLen_v, (SQLINTEGER*)&sizeOut_v);
	return_v = SQLBindCol (hdlStmt_v, 6, SQL_C_WCHAR, pkName_v, colLen_v, (SQLINTEGER*)&sizeOut_v);
	// Get Lenght of Schema Name
	schmLen_v = 50; //BsolLengthOfChaW (schmName_p);
	// Get Lenght of Table Name
	tblLen_v = 50; //BsolLengthOfChaW (tblName_p);
	// Get Primary Key
	return_v = BsolSQL_PrimaryKeys (hdlStmt_v, TEXT(""), 0, TEXT(""), schmLen_v, tblName_p, tblLen_v);
	// Fetch Row from Result Set
	return_v = SQLFetch (hdlStmt_v);
	return_v = SQLNumResultCols (hdlStmt_v, &tblLen_v);
	// If Successful
	if (return_v == SQL_SUCCESS || return_v == SQL_SUCCESS_WITH_INFO)
		// Convert wchar_t * to Wstring
		wsPkName_v = pkName_v;

	return wsPkName_v;
}


Regards,
Ahmad
Topic archived. No new replies allowed.