How to call sql server stored procedure with input parameters in Qt

qt, sql-server

Solution

The problem was solved like this:

I created function:

create function getAppVersions
(
   @appCode nvarchar(128),
   @serialNo nvarchar(128))
   returns table as
   return (
   select v.Version from Version v 
   inner join Application a on a.Code = v.AppCode
   inner join SerialNumber s on a.Code = s.AppCode
   where a.Code = @appCode and s.SerialNo = @serialNo
)

And call that function in Qt like this:

QStringList DBConnection::getQueryResult(QString code, QString serialNo){

    QSqlQuery query;
    QStringList results;


    QString connectionString = connection.arg(serverName).arg(dbName);
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

    db.setDatabaseName(connectionString);

    if (db.open())
    {
        qDebug() << "Opened";
        query = db.exec("Select *from getAppVersions('" + code + "','" + serialNo + "');");

        while(query.next()){
            QString result = query.record().value(0).toString();
            results.append(result);
        }
        db.close();
    }
    else
    {
        qDebug() << "Error = " << db.lastError().text();
    }
    db.close();

    return results;
}

Problem

I have this stored procedure ``` create proc getVersion(@appCode nvarchar(128), @serialNo nvarchar(128)) as select v.Version from Version v inner join Application a on a.Code = v.AppCode inner join SerialNumber s on a.Code = s.AppCode where a.Code = @appCode and s.SerialNo = @serialNo ``` This procedure has to return a result set. I call it in qt like this: ``` QStringList DBConnection::getQueryResult(QString code, QString serialNo){ connect(); QSqlQuery query; QStringList results; query.prepare("CALL getVersion(?,?)"); query.bindValue(1,code); query.bindValue(2,serialNo); query.exec(); while(query.next()){ QString result = query.record().value(1).toString(); results.append(result); } for(QString res:results){ qDebug() << res; } closeConnection(); return results; } ``` But i get the the following error: Incorrect syntax error near '@P1'. SQL Server Statement could not be prepared. Which is the proper way to call a stored procedure with 2 input parameters in Qt? EDIT: I created a FUNCTION which will return a table with the matching data. ``` create function getAppVersions ( @appCode nvarchar(128), @serialNo nvarchar(128)) returns table as return ( select v.Version from Version v inner join Application a on a.Code = v.AppCode inner join SerialNumber s on a.Code = s.AppCode where a.Code = @appCode and s.SerialNo = @serialNo) ``` I can't figure how to call this function using QSqlQuery.

Original source