0

I think my syntax is correct. Whats wrong with my syntax? I need to use it as search item.

But still it shows the error

Syntax error in FROM clause

procedure TForm9.Button1Click(Sender: TObject);
begin
 ADOQuery1.Close;

ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add ('SELECT DAT20303.NUM,DAT20303.NAMA,DAT20303.MATRIC_ID,DAT20303.SUBJEK,DAT20303.M1,DAT20303.M2,DAT20303.M3,DAT20303.M4,DAT20303.M5,DAT20303.M6,DAT20303.M7, STUDENT.SEKSYEN,STUDENT.MATRIC_ID');
 ADOQuery1.SQL.Add ('FROM STUDENT');
 ADOQuery1.SQL.Add ('JOIN DAT20303');
 ADOQuery1.SQL.Add ('ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID');
 ADOQuery1.SQL.Add ('WHERE SEKSYEN = ' + ComboBox2.Text);



ADOQuery1.Open; {open query + display data}
end;
4
  • @a_horse_with_no_name: and also missing a space before the FROM after the last column in the list of SELECT columns .... Commented May 13, 2014 at 12:35
  • 3
    ADOQuery1.SQL.Add ('WHERE SEKSYEN = ''' + ComboBox2.Text + ''''); but best to use parameters as mentioned earlier. Commented May 13, 2014 at 12:39
  • 1
    Beware of SQL injection Commented May 13, 2014 at 12:42
  • 1
    thankyou @marc_s . im sorry for not noticing that space. -___- okay parameter . im new to delphi. still trying to learn. thanks guys! big thanks! Commented May 13, 2014 at 16:55

2 Answers 2

4

You must not use only JOIN within your query.

This

SELECT *
FROM STUDENT
  JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

leads to

Syntax error in FROM clause

but this will do fine

SELECT *
FROM STUDENT
  LEFT JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

or this one

SELECT *
FROM STUDENT
  INNER JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

Also have a look at this question here on SO

Sign up to request clarification or add additional context in comments.

Comments

4

Do you in fact have a STUDENT table? You refer to both STUDENT and LSTUDENT in your SELECT, but only use STUDENT in the FROM clause. Which one is it?

Also, at a minimum, you need to quote the content of your WHERE condition.

ADOQuery1.SQL.Add ('WHERE SEKSYEN = ' + QuotedStr(ComboBox2.Text));

Much better though would be to stop concatenating SQL in your code, and use parameterized queries instead. It's not only much safer against SQL injection, but it allows the database engine to compile and cache the query content, making it execute much more quickly if you're using the same statement more than one time with different parameter values.

procedure TForm9.Button1Click(Sender: TObject);
begin
  ADOQuery1.Close;

  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('SELECT DAT20303.NUM, DAT20303.NAMA, DAT20303.MATRIC_ID,');
  ADOQuery1.SQL.Add('DAT20303.SUBJEK, DAT20303.M1, DAT20303.M2, DAT20303.M3,');   
  ADOQuery1.SQL.Add('DAT20303.M4, DAT20303.M5, DAT20303.M6,');    
  ADOQuery1.SQL.Add('DAT20303.M7, STUDENT.SEKSYEN, LSTUDENT.MATRIC_ID');
  ADOQuery1.SQL.Add ('FROM STUDENT');
  ADOQuery1.SQL.Add ('JOIN DAT20303');
  ADOQuery1.SQL.Add ('ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID');
  ADOQuery1.SQL.Add ('WHERE SEKSYEN = :SekSyen');
  ADOQuery1.Parameters.ParamByName('SekSyen').AsString := ComboBox1.Text;
  ADOQuery1.Open; {open query + display data}
end;

And please don't be afraid to use whitespace (spaces and line breaks) to make your SQL more readable! I've cleaned it up somewhat in my answer to do so. When it's easier to read, it's easier to understand (and maintain later).

3 Comments

thank you! this is new. i always thought that i can only use the sql command line by line. thank you very much. im bad in english -__-
@Adam: You do have to use all of the SQL. My first code snippet only corrects the one line having the error - you'll still need to use all of the SQL above it.
do check my other question. the last one. :) [link]stackoverflow.com/questions/23640490/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.