Hello,
I have a stored procedure declared in a SQL Server 2005 database, declared as followed:
CREATE PROCEDURE insertCustomer
@CustomerMap varchar(32),
@Result int OUT,
@Message nvarchar(255) OUT AS
SELECT * FROM Customers WHERE MapName = @CustomerMap
IF @@ROWCOUNT <> 0 BEGIN
SET @Message='Er bestaat al een klantmap met deze naam...'
SET @Result=1
RETURN
END
INSERT INTO Customers (MapName, Status) VALUES (@CustomerMap, 'N')
SET @Message='OK'
SET @Result=0
RETURN
CREATE PROCEDURE insertCustomer
@CustomerMap varchar(32),
@Result int OUT,
@Message nvarchar(255) OUT AS
SELECT * FROM Customers WHERE MapName = @CustomerMap
IF @@ROWCOUNT <> 0 BEGIN
SET @Message='Er bestaat al een klantmap met deze naam...'
SET @Result=1
RETURN
END
INSERT INTO Customers (MapName, Status) VALUES (@CustomerMap, 'N')
SET @Message='OK'
SET @Result=0
RETURN
To copy to clipboard, switch view to plain text mode
In Qt, I want to execute this procedure, and get my result and message back.
I amusing the following code:
{
m_dbDriver = OdbcDriver;
m_dbHost = Host;
m_dbDatabase = Database;
m_dbUser = User;
m_dbPass = Password;
db.setDatabaseName("Driver={" + OdbcDriver + "};Server=" + Host + ";Database=" + Database + ";");
db.setUserName(m_dbUser);
db.setPassword(m_dbPass);
}
bool open()
{
}
bool execProcedure (DbProcedure Procedure)
{
if (!db.isOpen())
{
if (!db.open())
return false;
}
query.prepare("{ CALL insertCustomer (?, ?, ?) }");
query.bindValue(0, Map);
query.bindValue(1, Result, QSql::Out);
query.bindValue(2, Message, QSql::Out);
if(!query.exec())
{
QString error
= query.
lastError().
text();
return false;
}
query.nextResult();
Result = query.boundValue(1);
Message = query.boundValue(2);
return true;
}
OdbcDbHandler(QString OdbcDriver, QString Host, QString Database, QString User, QString Password)
{
m_dbDriver = OdbcDriver;
m_dbHost = Host;
m_dbDatabase = Database;
m_dbUser = User;
m_dbPass = Password;
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={" + OdbcDriver + "};Server=" + Host + ";Database=" + Database + ";");
db.setUserName(m_dbUser);
db.setPassword(m_dbPass);
}
bool open()
{
return QSqlDatabase::database().open();
}
bool execProcedure (DbProcedure Procedure)
{
QSqlDatabase db = QSqlDatabase::database();
if (!db.isOpen())
{
if (!db.open())
return false;
}
QSqlQuery query;
query.prepare("{ CALL insertCustomer (?, ?, ?) }");
QVariant Map = "customermap";
QVariant Result = -1;
QVariant Message("");
query.bindValue(0, Map);
query.bindValue(1, Result, QSql::Out);
query.bindValue(2, Message, QSql::Out);
if(!query.exec())
{
QString error = query.lastError().text();
return false;
}
query.nextResult();
Result = query.boundValue(1);
Message = query.boundValue(2);
return true;
}
To copy to clipboard, switch view to plain text mode
The stored procedures executes as expected and my result code is set, to 0 or 1, depending on what i put int.
However, Message always seems to be empty, while this should be set too.
Anyone any clue what I'm doing wrong?
Bookmarks