EOleException,带有消息'FROM子句中的语法错误'delphi

时间:2021-06-13 15:44:07

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

嗨,我正在尝试让SQL从用户从列表框中选择的表中进行选择

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

我在SQL代码中的FROM之后不断收到语法错误(:activity)任何帮助都将不胜感激

1 个解决方案

#1


3  

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.

您无法将select的表部分定义为参数,您需要动态构建select语句的该部分。

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.

:iCompNo OTOH可以定义为参数,以防止SQL注入。

#1


3  

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.

您无法将select的表部分定义为参数,您需要动态构建select语句的该部分。

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.

:iCompNo OTOH可以定义为参数,以防止SQL注入。