laura
laura

Reputation: 2135

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

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.

Upvotes: 4

Views: 4812

Answers (2)

laura
laura

Reputation: 2135

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;
}

Upvotes: 2

Nejat
Nejat

Reputation: 32665

First of all the placeholders for QSqlQuery should be like :code or :serialNo.

Also a procedure returns a void. It only has a processing role. When you need returning a value, you should use a function.

Depending on the Qt sql driver, you have to use a QSqlQuery or a direct call to get the result.

query.prepare("getVersion(:code,:serialNo)");
query.bindValue(":code",code);
query.bindValue(":serialNo",serialNo);
query.exec();

Here getVesion is a function created using CREATE FUNCTION.

Direct call is somthing like:

QSqlQuery q = db->exec( QString("getVersion(%1,%2) ;").arg(code).arg(serialNo) ) ;

Upvotes: 0

Related Questions