Jaynill Gopal
Jaynill Gopal

Reputation: 47

EOleException with message 'Syntax error in FROM clause' delphi

Hi I'm trying to get SQL to select from a table which the user has chosen from a list box

Heres my code

activity := cmbActivity.Text; //this is where the user selects a table to choose from
qryStudents.SQL.Text := 'SELECT * FROM :activity WHERE CompNo = :iCompNo'; //error here
qryStudents.Parameters.ParamByName('activity').Value:= activity; 
qryStudents.Parameters.ParamByName('iCompNo').Value := iCompNo;
qryStudents.Open;

I keep getting a syntax error after FROM in the SQL code (:activity) Any help will be appreciated

Upvotes: 1

Views: 723

Answers (1)

Marc Guillot
Marc Guillot

Reputation: 6455

You can't define the table part of your select as a parameter, you'll need to dynamically build that part of the select statement.

activity := cmbActivity.Text;
qryStudents.Close;
qryStudents.SQL.Text := 'SELECT * FROM '  + activity + ' WHERE CompNo = :iCompNo';
qryStudents.Parameters.ParamByName('iCompNo').Value := iCompNo;
qryStudents.Open;

:iCompNo OTOH is fine to be defined as a parameter, in order to prevent SQL injection.

Upvotes: 3

Related Questions