ce_nort
2nd February 2020, 19:56
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 (https://forum.qt.io/topic/80428/how-to-handle-the-return-values-of-sql-stored-procedures-in-qt-application-for-inserting-delete-update) thread and this (https://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values) 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
Function definition for calling stored procedure:
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;
}
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.
Any thoughts? Let me know if you need more context or have any questions. Help much appreciated in advance!
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 (https://forum.qt.io/topic/80428/how-to-handle-the-return-values-of-sql-stored-procedures-in-qt-application-for-inserting-delete-update) thread and this (https://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values) 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
Function definition for calling stored procedure:
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;
}
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.
Any thoughts? Let me know if you need more context or have any questions. Help much appreciated in advance!