Workaround Reserved Words

-1

I have a table called Candidates with fields C_ID, C_Surname, C_Names and C_Gender. The word NAMES is reserved in Microsoft Jet 4.0 and therefore the SQL statement SELECT C_Names AS Names does not work. Is there any workaround to use the word Names as an identifier?

NOTE: My method of connection is ADO.

Update

Seeing as none of the provided answers have worked, here’s my full SQL statement:

SELECT
  C.C_ID AS ID,
  StrConv(C.C_Surname, 3) AS Surname,
  StrConv(C.C_Names, 3) AS Name,
  Sum(V.V_ClassA * M.M_Multiplier) AS ClassA,
  Sum(V.V_ClassB * M.M_Multiplier) AS ClassB,
  Sum(V.V_ClassC * M.M_Multiplier) AS ClassC
FROM
  ((Candidates AS C
    INNER JOIN Votes AS V ON C.C_ID = V.C_ID)
    INNER JOIN Parties AS P ON V.P_ID = P.P_ID)
    INNER JOIN Multiplier AS M ON P.P_Type = M.P_Type
GROUP BY
  C.C_ID,
  C.C_Surname,
  C.C_Names
ORDER BY
  Sum(V.V_ClassA * M.M_Multiplier) DESC,
  Sum(V.V_ClassB * M.M_Multiplier) DESC,
  Sum(V.V_ClassC * M.M_Multiplier) DESC;

Implementation Code

begin
  with dmBKElections.qryTallyVotes, SQL do
    begin
      Clear;
      Add('SELECT');
      Add('  C.C_ID AS ID,');
      Add('  StrConv(C.C_Surname, 3) AS Surname,');
      Add('  StrConv(C.C_Names, 3) AS Name,');
      Add('  Sum(V.V_ClassA * M.M_Multiplier) AS ClassA,');
      Add('  Sum(V.V_ClassB * M.M_Multiplier) AS ClassB,');
      Add('  Sum(V.V_ClassC * M.M_Multiplier) AS ClassC');
      Add('FROM');
      Add('  ((Candidates AS C');
      Add('    INNER JOIN Votes AS V ON C.C_ID = V.C_ID)');
      Add('    INNER JOIN Parties AS P ON V.P_ID = P.P_ID)');
      Add('    INNER JOIN Multiplier AS M ON P.P_Type = M.P_Type');
      Add('GROUP BY');
      Add('  C.C_ID,');
      Add('  StrConv(C.C_Surname, 3),');
      Add('  StrConv(C.C_Names, 3)');
      Add('ORDER BY');
      Add('  Sum(V.V_ClassA * M.M_Multiplier) DESC,');
      Add('  Sum(V.V_ClassB * M.M_Multiplier) DESC,');
      Add('  Sum(V.V_ClassC * M.M_Multiplier) DESC;');
      Open;
    end;
end;

When I change

Add('  StrConv(C.C_Names, 3) AS Name,');

to

Add('  StrConv(C.C_Names, 3) AS [Names],');

I get the error:

Project BKElections_P.exe raised exception class $C0000005 with message 'access violation at 0x0062ee2a: read of address 0x000000e2'.

sql
ado
ms-access-2013
delphi-xe8
asked on Stack Overflow Oct 5, 2015 by Reginald Greyling • edited Oct 6, 2015 by Reginald Greyling

2 Answers

0

Your have two choise if you are using SQL Server or MySQL on all other there must be one:

SELECT Fieldname as "Name" ...

This ist ANSI

OR

SELECT Fieldname as [Name]

This ist SQL Server and MS Access

On MySQL you can use:

SELECT Fieldname as ´Name´

But is it possible that you get the error in .NET and not in SQL? Normaly you don't get this error if you rename a column to "name", only if you want to access a column with a reserved name in some cases.

So in ADO.NET you can try:

rst![Name]
rst.Fields("Name")

Or the best think: Give us a sample of your code. And please try the SQL in SSMS.

answered on Stack Overflow Oct 5, 2015 by Frank
0

You should wrap any reserved words in square brackets as follows:

[Name] instead of Name

answered on Stack Overflow Oct 5, 2015 by Laurence Frost

User contributions licensed under CC BY-SA 3.0