Charlie
Charlie

Reputation: 315

QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error

I get this error when I try to call stored procedure in QT using QODBC:

QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error

Amount of parameters is correct, syntax looks alright to me. The procedure executes fine in Management Studio. What might be the problem?

QSqlQuery query(db1);
query.exec("SELECT * from Teachers"); //test query
TableWidget *table = ui->tableWidget;
for (int i = 0; i < table->rowCount(); i++)
{    
    QComboBox *combo = static_cast<QComboBox*>(table->cellWidget(i,0));
    qDebug() << query.prepare("{CALL add_syllabus_line (?,?,?,?,?,?,?,?,?)}");
    query.bindValue("teacher_name", teacherName);
    query.bindValue("subject_name", "????");
    query.bindValue("temporary_name", ratingName);
    query.bindValue("type_name", combo->currentText());
    query.bindValue("activity_name", table->item(i, 1)->text());
    query.bindValue("min_score", table->item(i, 2)->text().toInt());
    if (propertiesInstance.fixed) query.bindValue("max_score", 0);
    else query.bindValue("max_score", table->item(i, 3)->text().toInt());
    query.bindValue("max_score_exists", propertiesInstance.fixed);
    query.bindValue("evaluation_by_exam", propertiesInstance.exam);

    if (!query.exec())
    {
       qDebug() << query.lastError();
    }
}

The procedure:

ALTER PROCEDURE [dbo].[add_syllabus_line] 

@teacher_name NVARCHAR(50),
@subject_name NVARCHAR(50),
@temporary_name NVARCHAR(50),
@type_name NVARCHAR(50),
@activity_name NVARCHAR(50),
@min_score int,
@max_score int,
@max_score_exists bit,
@evaluation_by_exam bit

AS
BEGIN
SET NOCOUNT ON;

DECLARE @teacher_id int;
DECLARE @subject_id int; 
DECLARE @type_id int; 

SELECT @teacher_id = Teacher_id FROM Teachers WHERE Teacher_name = @teacher_name;
SELECT @type_id = Activity_type_id FROM Activity_types WHERE Activity_type_name = @type_name;
SELECT @subject_id = subject_id FROM Subjects WHERE Subject_name = @subject_name;

INSERT INTO Syllabi (Teacher_id, 
                    Subject_id, 
                    Temporary_name,
                    Activity_type_id,
                    Activity_title,
                    Activity_min_score,
                    Activity_max_score,
                    Max_score_exists,
                    Evaluation_by_exam) VALUES (@teacher_id,
                                                @subject_id,
                                                @temporary_name,
                                                @type_id,
                                                @activity_name,
                                                @min_score,
                                                @max_score,
                                                @max_score_exists,
                                                @evaluation_by_exam);
END

query.prepare returns true.

I run a profiler trace and the query doesn't even shows there, only the test one.

select 504,c.name,c.description,c.definition from master.dbo.syscharsets c where c.id = convert(tinyint, databasepropertyex ( db_name() , 'sqlcharset')) go

exec sp_datatype_info 11 go

SET QUOTED_IDENTIFIER ON go

declare @p1 int set @p1=180150003
declare @p3 int
set @p3=8 declare @p4 int set @p4=1 declare @p5 int set @p5=3 exec sp_cursoropen @p1 output,N'SELECT * from Teachers',@p3 output,@p4 output,@p5 output select @p1, @p3, @p4, @p5

exec sp_cursorclose 180150003 go

Upvotes: 1

Views: 3277

Answers (2)

Charlie
Charlie

Reputation: 315

The problem turned out to be the syntax, I changed it to

query.prepare("{CALL add_syllabus_line (:teacher_name, :subject_name, 
    :temporary_name, :type_name, :activity_name, :min_score, :max_score, :max_score_exists, 
            :evaluation_by_exam)}");

QComboBox *combo = static_cast<QComboBox*>(table->cellWidget(i,0));
query.bindValue(":teacher_name", teacherName);
query.bindValue(":subject_name", "Физика");
query.bindValue(":temporary_name", ratingName);
query.bindValue(":type_name", combo->currentText());
query.bindValue(":activity_name", table->item(i, 1)->text());
query.bindValue(":min_score", table->item(i, 2)->text().toInt());
query.bindValue(":max_score", 0);
query.bindValue(":max_score_exists", propertiesInstance.fixed);
query.bindValue(":evaluation_by_exam", propertiesInstance.exam);

qDebug() << query.exec();
qDebug() << query.lastError();

and now it works fine.

Upvotes: 1

SQLGobbleDeGook
SQLGobbleDeGook

Reputation: 156

Other than ensuring the exact syntax being passed to SQL Server using either a profiler trace or extended events, DBCC INPUTBUFFER etc, does the table being inserted have any trigger that might be interfering?

Upvotes: 0

Related Questions