Forum Discussion

oneeyeman1's avatar
oneeyeman1
Copper Contributor
Nov 14, 2025

Failure to run query

Hi, ALL,

 

First I believe that this question is more suited for a C++ devs,, but I can't find it. Sorry.

 

I'm trying to run  following query:

 

            

query1= L"SELECT name FROM sys.key_constraints WHERE type = 'PK' AND parent_object_id = OBJECT_ID(?.?.?);";

 

with the code below:

        cbLen[0] = cbLen[1] = cbLen[2] = SQL_NTS;
        ret = SQLAllocHandle(  SQL_HANDLE_STMT, m_hdbc, &m_hstmt );
        ret = SQLPrepare( m_hstmt, qry.get(), SQL_NTS );
        ret = SQLBindParameter( m_hstmt, 1, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WCHAR, m_maxIdLen, 0, catalog.get(), 0, &cbLen[0] );
        ret = SQLBindParameter( m_hstmt, 2, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WCHAR, m_maxIdLen, 0, schema.get(), 0, &cbLen[1] );
        ret = SQLBindParameter( m_hstmt, 3, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WCHAR, m_maxIdLen, 0, table.get(), 0, &cbLen[2] );

        ret = SQLExecute( m_hstmt );

that code errors out with the error message "SQL syntax error near p2".

Am I not allowed to arametrize this query like that?

 

Thank you.

 

 

1 Reply

  • Viorel's avatar
    Viorel
    Copper Contributor

    Try this query:

     

    query1= L"SELECT name FROM sys.key_constraints WHERE type = 'PK' AND parent_object_id = OBJECT_ID(concat(quotename(?), '.', quotename(?), '.', quotename(?)))";

     

Resources