locked
Error reporting The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect ? RRS feed

  • Question

  • In C # .net when running an SQL query to get access data with an error The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect, the problem is incorrect:
    [code]
    SELECT COUNT (MAPB) AS Count
    FROM TABNHANVIEN
    WHERE (HIENAN = 'True') AND (MAPB = 3)
    GROUP BY MAPB;
    [/ code]
    But I copy this query directly to the microsoft access sql query to run normally, with no error, I run another query in C#.Net running normally, no error like this message, you know what is wrong is not ?
    Friday, August 14, 2020 4:24 AM

Answers

  • Just for kicks, can you use a different name for your count column, say

    SELECT COUNT(MAPB) AS cntMapB
    FROM TABNHANVIEN
    WHERE HIENAN = 'True' AND MAPB = 3;
    Also, since you're using static single value for MAPB, you may omit GROUP BY.


    Looking for new opportunities

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by lamtriendong Tuesday, August 25, 2020 7:27 AM
    • Unmarked as answer by lamtriendong Tuesday, August 25, 2020 7:49 AM
    • Marked as answer by lamtriendong Tuesday, August 25, 2020 8:17 AM
    Friday, August 14, 2020 4:47 AM
  • Hi lamtriendong,

    Do you try to change Count to Count1 or change other column names to others to test again?

    Is the issue solved?

    BR,

    Mia

    If the reply is helped, please "Mark Answer" and upvote it.--Mia


    ""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
    We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
    For more information, please refer to the sticky post.

    • Marked as answer by lamtriendong Tuesday, August 25, 2020 7:27 AM
    • Unmarked as answer by lamtriendong Tuesday, August 25, 2020 7:49 AM
    • Marked as answer by lamtriendong Tuesday, August 25, 2020 8:17 AM
    Monday, August 17, 2020 1:18 AM
  • Hi lamtriendong,

    Do you try to change Count to Count1 or change other column names to others to test again?

    Is the issue solved?

    BR,

    Mia

    If the reply is helped, please "Mark Answer" and upvote it.--Mia


    ""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
    We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
    For more information, please refer to the sticky post.

    • Marked as answer by lamtriendong Tuesday, August 25, 2020 7:27 AM
    • Unmarked as answer by lamtriendong Tuesday, August 25, 2020 7:49 AM
    • Marked as answer by lamtriendong Tuesday, August 25, 2020 8:17 AM
    Tuesday, August 18, 2020 1:07 AM

All replies

  • Just for kicks, can you use a different name for your count column, say

    SELECT COUNT(MAPB) AS cntMapB
    FROM TABNHANVIEN
    WHERE HIENAN = 'True' AND MAPB = 3;
    Also, since you're using static single value for MAPB, you may omit GROUP BY.


    Looking for new opportunities

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by lamtriendong Tuesday, August 25, 2020 7:27 AM
    • Unmarked as answer by lamtriendong Tuesday, August 25, 2020 7:49 AM
    • Marked as answer by lamtriendong Tuesday, August 25, 2020 8:17 AM
    Friday, August 14, 2020 4:47 AM
  • Hi Iamtriendong,

    > But I copy this query directly to the microsoft access sql query to run normally, with no error, I run another query in C#.Net running normally, no error like this message, you know what is wrong is not 

    I  think sql query's syntax is correct. You just need to verify the spelling of the column names  or parameters in your query. Modify the code as next and test again:

    SELECT COUNT (MAPB) AS Count
    FROM TABNHANVIEN
    WHERE (HIENAN <> 'True') AND (MAPB = 1)
    GROUP BY MAPB;

    or Replace the column name MAPB with other column name to test.

    SELECT COUNT (OtherColumnName) AS Count
    FROM TABNHANVIEN
    WHERE (HIENAN <> 'True') AND (OtherColumnName = XX)
    GROUP BY OtherColumnName;


    I test with c# .net4.6.1 it works ok, code as next, you can copy it and then change the server’s name and database's name and then use your query string to test. Test on my side: sql version: sql2019  database: AdventureWorks2016

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Data.SqlClient;
    
    namespace TestApp1
    
    {
    
        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                string connString = @"Server =BI\SQL2019DB; Database = Adventureworks2016; Trusted_Connection = True;";
    
                try
    
                {
    
                    using (SqlConnection conn = new SqlConnection(connString))
    
                    {
    
                        //retrieve the SQL Server instance version
    
                        string query = @"SELECT  Count(JobTitle) As Count
    
    
                                         FROM HumanResources.Employee
    
    
                                         WHERE ( Gender <> 'M') and (JobTitle = 'Design Engineer')
    
    
                                        GROUP BY JobTitle;
    
                                        ";
    
                        SqlCommand cmd = new SqlCommand(query, conn);
    
                        //open connection
    
                        conn.Open();
    
                        //execute the SQLCommand
    
                        SqlDataReader dr = cmd.ExecuteReader();
    
                        //check if there are records
    
                        if (dr.HasRows)
    
                        {
    
                            while (dr.Read())
    
                            {
    
                                //display retrieved record (first column only/string value)
    
                                Console.WriteLine(dr.GetInt32(0));
    
                            }
    
                        }
    
                        else
    
                        {
    
                            Console.WriteLine("No data found.");
    
                        }
    
                        dr.Close();
    
                    }
    
                }
    
                catch (Exception ex)
    
                {
    
                    //display error message
    
                    Console.WriteLine("Exception: " + ex.Message);
    
                }
    
            }
    
        }
    
    }

    More information: querying-sql-server-tables-from-net

    BR,

    Mia

    If the reply helped, please "Mark Answer" and upvote it.--Mia



    ""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
    We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
    For more information, please refer to the sticky post.


    • Edited by MIAOYUXI Friday, August 14, 2020 7:44 AM
    Friday, August 14, 2020 7:42 AM
  • The SELECT statement includes a reserved word or 
    You used COUNT as column alias and that's a ODBC Reserved Keyword

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Friday, August 14, 2020 7:55 AM
  • Hi lamtriendong,

    Do you try to change Count to Count1 or change other column names to others to test again?

    Is the issue solved?

    BR,

    Mia

    If the reply is helped, please "Mark Answer" and upvote it.--Mia


    ""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
    We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
    For more information, please refer to the sticky post.

    • Marked as answer by lamtriendong Tuesday, August 25, 2020 7:27 AM
    • Unmarked as answer by lamtriendong Tuesday, August 25, 2020 7:49 AM
    • Marked as answer by lamtriendong Tuesday, August 25, 2020 8:17 AM
    Monday, August 17, 2020 1:18 AM
  • Hi lamtriendong,

    Do you try to change Count to Count1 or change other column names to others to test again?

    Is the issue solved?

    BR,

    Mia

    If the reply is helped, please "Mark Answer" and upvote it.--Mia


    ""SQL Server related"" forum will be migrated to a new home on Microsoft Q&A SQL Server!
    We invite you to post new questions in the "SQL Server related" forum’s new home on Microsoft Q&A SQL Server !
    For more information, please refer to the sticky post.

    • Marked as answer by lamtriendong Tuesday, August 25, 2020 7:27 AM
    • Unmarked as answer by lamtriendong Tuesday, August 25, 2020 7:49 AM
    • Marked as answer by lamtriendong Tuesday, August 25, 2020 8:17 AM
    Tuesday, August 18, 2020 1:07 AM
  • The above statement got an error on C#.NET of access, I tried it in C#.NET SQL Server to run normally but in C#.NET of access it did not let the alias match the COUNT statement, thank you for answering my question, now the above command is running well after I renamed the alias Count to Count1 or name else








    Tuesday, August 25, 2020 7:27 AM