Hello,
I've been banging my head against this for a while and can only seem to get a blank value returned. I have followed the advice in this thread and this documentation by binding a parameter to QSql::Out. I still seem to get a blank result, even though I know that is wrong as I can call the procedure with a result in SSMS. Am I constructing the query incorrectly? See relevant code below:
Stored procedure definition:
CREATE procedure [dbo].[usp_SetGetNextRecordID] ( @TableName varchar(50),
@RecordID int OUTPUT )
as begin
set nocount on
update mt_Tables
set LastRecordID = LastRecordID+1,
@RecordID = LastRecordID+1
where TableName = @TableName
return
end
GO
CREATE procedure [dbo].[usp_SetGetNextRecordID] ( @TableName varchar(50),
@RecordID int OUTPUT )
as begin
set nocount on
update mt_Tables
set LastRecordID = LastRecordID+1,
@RecordID = LastRecordID+1
where TableName = @TableName
return
end
GO
To copy to clipboard, switch view to plain text mode
Function definition for calling stored procedure:
QString DbController
::runProcedureWithOutput(QString procedure_name, QMap<int, QString> values_list
) {
QMapIterator<int, QString> i(values_list);
while (i.hasNext()) {
i.next();
if (i.hasNext() == true) {
query_string += "?,";
} else {
query_string += "?";
}
}
query_string.append(")}");
query.prepare(query_string);
query.setForwardOnly(true);
int bound_out_value = 0; // Default
//Bind values...
QMapIterator<int, QString> j(values_list);
while (j.hasNext()) {
j.next();
if (!j.hasNext()) {
query.bindValue(j.key(), j.value(), QSql::Out); // Bind out value if last parameter
bound_out_value = j.key();
} else {
query.bindValue(j.key(), j.value());
}
}
query.exec();
while(query.next()) {
return_value = query.value(bound_out_value).toString();
}
return return_value;
}
QString DbController::runProcedureWithOutput(QString procedure_name, QMap<int, QString> values_list)
{
QString return_value;
QString query_string = QString("{CALL " + procedure_name + " (");
QMapIterator<int, QString> i(values_list);
while (i.hasNext()) {
i.next();
if (i.hasNext() == true) {
query_string += "?,";
} else {
query_string += "?";
}
}
query_string.append(")}");
QSqlQuery query;
query.prepare(query_string);
query.setForwardOnly(true);
int bound_out_value = 0; // Default
//Bind values...
QMapIterator<int, QString> j(values_list);
while (j.hasNext()) {
j.next();
if (!j.hasNext()) {
query.bindValue(j.key(), j.value(), QSql::Out); // Bind out value if last parameter
bound_out_value = j.key();
} else {
query.bindValue(j.key(), j.value());
}
}
query.exec();
while(query.next()) {
return_value = query.value(bound_out_value).toString();
}
return return_value;
}
To copy to clipboard, switch view to plain text mode
Usage of function:
QMap<int, QString> params;
params.insert(0, "table_name");
params.insert(1, "@RecordID");
QString result
= db_controller
->runProcedureWithOutput
("usp_SetGetNextRecordID", params
);
qDebug() << result; // Always empty string when it shouldn't be, no SQL Errors thrown.
QMap<int, QString> params;
params.insert(0, "table_name");
params.insert(1, "@RecordID");
QString result = db_controller->runProcedureWithOutput("usp_SetGetNextRecordID", params);
qDebug() << result; // Always empty string when it shouldn't be, no SQL Errors thrown.
To copy to clipboard, switch view to plain text mode
Any thoughts? Let me know if you need more context or have any questions. Help much appreciated in advance!
Bookmarks