2015-12-04 9 views
8

Was den Fehler sein würde, wenn ich folgende Fehlermeldung erhaltenCOUNT Feld falsche oder Syntaxfehler

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[07002]: [Microsoft][ODBC Driver 11 for SQL Server]COUNT field incorrect or syntax error'...

Dies ist die Abfrage, die ich bin mit

$sql = $pdo->prepare("SELECT stockamount, stockname, stockbalance.stockid, SUM(ABS(reservationtransaction.stockquantity)) AS reservedamount FROM stockbalance 
    JOIN stock ON stockbalance.stockid = stock.stockid 
    LEFT JOIN reservationtransaction ON reservationtransaction.articleid = :artid 
    WHERE stockbalance.articleid = :artid AND ((changeddate > DATEADD(yy,-1,GETDATE()) AND inventorydate > DATEADD(yy,-1,GETDATE())) OR stockbalance.stockamount <> 0) 
    GROUP BY stockbalance.stockid"); 
$sql->bindValue(':artid', $productId); 
$sql->execute(); 

ich Fragen in SO gesucht haben, aber niemand war ähnlich oder hilfreich.
Vielen Dank im Voraus.

Bearbeiten: Diese Abfrage funktioniert einwandfrei, wenn Sie es mit Microsoft SQL Server Management Studio ausführen, aber wenn ich PDO verwende, erhalte ich den Fehler.

+0

Ihre 'stockamount' und' stockname' sind nicht Teil der 'GROUP BY', so dass Sie kann sie nicht ohne eine Aggregatmethode wie "MIN" auswählen. –

+1

Ich vermute, Sie haben MySQL Hintergrund: http: //stackoverflow.com/questions/33629168/group-by-clause-in-mysql-and-postgresql-why-the-error-in-postgresql/33629201#33629201. Der Punkt ist, dass das Aggregationsverhalten von MySQL keine ANSI-Beanstandung ist. – lad2025

+0

Nach der Fehlermeldung ist dies eine PDOException, keine SQL-Ausnahme. Könnte es sein, dass die Ergebnismenge nicht übereinstimmt und das Objekt, an das Sie es binden möchten? –

Antwort

15

The number of parameters specified in SQLBindParameter was less than the number of parameters in the SQL statement contained in *StatementText. SQLBindParameter was called with ParameterValuePtr set to a null pointer, StrLen_or_IndPtr not set to SQL_NULL_DATA or SQL_DATA_AT_EXEC, and InputOutputType not set to SQL_PARAM_OUTPUT, so that the number of parameters specified in SQLBindParameter was greater than the number of parameters in the SQL statement contained in *StatementText. SQLExecute Function

Platzhalter eindeutige Namen haben müssen, selbst wenn sie den gleichen Wert haben

$sql = $pdo->prepare("SELECT stockamount, stockname, stockbalance.stockid, SUM(ABS(reservationtransaction.stockquantity)) AS reservedamount FROM stockbalance 
JOIN stock ON stockbalance.stockid = stock.stockid 
LEFT JOIN reservationtransaction ON reservationtransaction.articleid = :artid 
WHERE stockbalance.articleid = :artid2 AND ((changeddate > DATEADD(yy,-1,GETDATE()) AND inventorydate > DATEADD(yy,-1,GETDATE())) OR stockbalance.stockamount <> 0) 
GROUP BY stockbalance.stockid"); 
$sql->bindValue(':artid', $productId); 
$sql->bindValue(':artid2', $productId); 
$sql->execute(); 
+0

Das war das Problem! Danke für deine Antwort. – lingo

1

Alle Spalten, die keine arithmetische Funktion haben, müssen in die GROUP BY-Klausel gehen. siehe unten:

SELECT stockamount, 
     stockname, 
     stockbalance.stockid, 
     Sum(Abs(reservationtransaction.stockquantity)) AS reservedamount 
FROM stockbalance 
     INNER JOIN stock 
       ON stockbalance.stockid = stock.stockid 
     LEFT JOIN reservationtransaction 
       ON reservationtransaction.articleid = :artid 
WHERE stockbalance.articleid = :artid 
     AND ((changeddate > Dateadd(yy, -1, Getdate()) 
       AND inventorydate > Dateadd(yy, -1, Getdate())) 
       OR stockbalance.stockamount <> 0) 
GROUP BY stockamount, 
      stockname, 
      stockbalance.stockid 
+1

Ok, ich habe das versucht, aber es hilft nicht. Ich bekomme immer noch Fehler "SQLSTATE [07002]: [Microsoft] [ODBC-Treiber 11 für SQL Server] COUNT Feld falsch oder Syntaxfehler". – lingo

Verwandte Themen