Skip to content

SQLPrimaryKeys

SQLPrimaryKeys#

SQLPrimaryKeys returns the column names that make up the primary key for a table. The driver returns the information as a result set. This function does not support returning primary keys from multiple tables in a single call.

SQLPrimarykeysW() as a Unicode string supports same execution as SQLPrimarykey().

Syntax#

SQLRETURN SQLPrimaryKeys (
        SQLHSTMT    stmt,
        SQLCHAR *   cName,
        SQLSMALLINT     cNameLength,
        SQLCHAR *   sName,
        SQLSMALLINT     sNameLength,
        SQLCHAR *   tName,
        SQLSMALLINT     tNameLength );

Arguments#

Data Type Argument In/Output Description
SQLHSTMT stmt Input Statement handle
SQLCHAR * cName Input Catalog name
SQLSMALLINT cNameLength Input The length, in bytes, of *cName
SQLCHAR * sName Input Schema name
SQLSMALLINT sNameLength Input The length, in bytes, of *sName
SQLCHAR * tName Input The table name. Cannot be a NULL pointer. tName cannot contain a string search pattern.
SQLSMALLINT tNameLength Input The length, in bytes, of *tName

Return Values#

SQL_SUCCESS
SQL_SUCCESS_WITH_INFO
SQL_INVALID_HANDLE
SQL_ERROR

Description#

SQLPrimaryKeys() returns the results in the format of standard result set which is sorted in order byTABLE_CAT, TABLE_SCHEM, TABLE_NAME, and KEY_SEQ.

The search type cannot be used to define the schema-defining arguments or the table name. In many cases, calling of SQLPrimaryKeys() is mapped on the search that is complex and cost a lot.

Therefore, the stored result set must be used instead of repeating the calling.

The following table lists the columns of the result set.

No. Name Data Type Description
1 TABLE_CAT VARCHAR Null will be always returned
2 TABLE_SCHEM VARCHAR Primary key table schema name
3 TABLE_NAME VARCHAR (NOT NULL) Primary key table name
4 COLUMN_NAME VARCHAR (NOT NULL) First primary key column name
5 KEY_SEQ SMALLINT (NOT NULL) Column orders of the first primary key starting with 1
6 PK_NAME VARCHAR First primary key name

[Table 2‑2] Columns Returned by SQLPrimaryKeys()

Diagnosis#

SQLSTATE Desctipiton Comments
08S01 Communication channel error Communication channel failure before the function processing is completed between the Altibase CLI driver and the database
HY000 General error
HY009 Invalid arguments used (null pointer) tNameTransfer NULL pointer
SQLBindCol
SQLFetch
SQLStatistics

Example#

< Refer to: $ALTIBASE_HOME/sample/SQLCLI/demo_meta3.cpp >

if (SQLPrimaryKeys(stmt,
                   NULL, 0,
                   NULL, 0,
                   (char*)"DEMO_META3", SQL_NTS) != SQL_SUCCESS)
{
    execute_err(dbc, stmt, "SQLPrimaryKeys");
    SQLFreeStmt(stmt, SQL_DROP);
    return SQL_ERROR;
}

SQLBindCol(stmt, 1, SQL_C_CHAR, szCatalog, STR_LEN,&cbCatalog);
SQLBindCol(stmt, 2, SQL_C_CHAR, szSchema, STR_LEN, &cbSchema);
SQLBindCol(stmt, 3, SQL_C_CHAR, szTableName, STR_LEN,&cbTableName);
SQLBindCol(stmt, 4, SQL_C_CHAR, szColumnName, STR_LEN, &cbColumnName);
SQLBindCol(stmt, 5, SQL_C_SSHORT, &KeySeq, 0, &cbKeySeq);
SQLBindCol(stmt, 6, SQL_C_CHAR, szPkName, STR_LEN, &cbPkName);
while ( (rc = SQLFetch(stmt)) != SQL_NO_DATA)
{
    if ( rc == SQL_ERROR )
    {
        execute_err(dbc, stmt, "SQLPrimaryKeys:SQLFetch");
        break;
    }
    printf("%-20s%-20s%-3d%-20s\n", szTableName,
                szColumnName, KeySeq, szPkName);
}