QODBCResult :: exec:无法执行语句:" [Microsoft] [ODBC SQL Server驱动程序] COUNT字段不正确或语法错误

时间:2015-06-19 00:01:10

标签: c++ sql-server qt tsql odbc

当我尝试使用QODBC在QT中调用存储过程时出现此错误:

  

QODBCResult :: exec:无法执行语句:" [Microsoft] [ODBC SQL   服务器驱动程序] COUNT字段不正确或语法错误

参数数量是正确的,语法看起来不错。该过程在Management Studio中执行正常。可能是什么问题?

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

程序:

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返回true。

我运行了一个探查器跟踪,查询甚至没有在那里显示,只有测试一个。

  

从中选择504,c.name,c.description,c.definition   master.dbo.syscharsets c其中c.id = convert(tinyint,   databasepropertyex(db_name(),&#39; sqlcharset&#39;))go

     

exec sp_datatype_info 11 go

     

SET QUOTED_IDENTIFIER ON   去

     

声明@ p1 int   设置@ p1 = 180150003
  声明@ p3 int
  设为@ p3 = 8   声明@ p4 int   设为@ p4 = 1   声明@ p5 int   设为@ p5 = 3   exec sp_cursoropen @ p1   输出,N&#39; SELECT *来自教师&#39;,@ p3输出,@ p4输出,@ p5输出   选择@ p1,@ p3,@ p4,@ p5

     

exec sp_cursorclose 180150003 go

2 个答案:

答案 0 :(得分:1)

问题原来是语法,我把它改成了

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();

现在它运作正常。

答案 1 :(得分:0)

除了确保使用分析器跟踪或扩展事件(DBCC INPUTBUFFER等)传递给SQL Server的确切语法之外,正在插入的表是否具有可能干扰的任何触发器?

相关问题