嘗試使用以下代碼運行選擇陳述句。我正在決議一個 SQL 表名引數,其值由 case 陳述句確定。然后,這會將資料集分配給以另一種形式使用的全域資料源。但是,該應用程式正在回傳“FROM 子句中的語法錯誤”對話框。
我已經分配了正確的資料型別,并且在我的測驗期間,我可以確認引數的值是它需要的值,即案例 1 的“帳戶”。
我是使用 ADO 的新手,但 ADOQUERY.SQL.GetText 正在回傳帶有引數占位符“:ATABLE”而不是引數值的 SQL 陳述句,盡管我目前假設這是正常的。
procedure TfrmDataModule.FindAllRecords(Sender: TObject; recordType: Integer);
var
ADOQuery : TADOQuery;
Param : TParameter;
begin
case recordType of
1 : currentRecordType := 'ACCOUNTS';
2 : currentRecordType := 'CONTACTS';
3 : currentRecordType := 'USERS';
end;
{ SQL Query }
SQLStr := 'SELECT * FROM :ATABLE';
{ Create the query. }
ADOQuery := TADOQuery.Create(Self);
ADOQuery.Connection := ADOConn;
ADOQuery.SQL.Add(SQLStr);
{ Update the parameter that was parsed from the SQL query. }
Param := ADOQuery.Parameters.ParamByName('ATABLE');
Param.DataType := ftString;
Param.Value := currentRecordType;
{ Set the query to Prepared--it will improve performance. }
ADOQuery.Prepared := true;
try
ADOQuery.Active := True;
except
on e: EADOError do
begin
MessageDlg('Error while doing query', mtError,
[mbOK], 0);
Exit;
end;
end;
{ Create the data source. }
DataSrc := TDataSource.Create(Self);
DataSrc.DataSet := ADOQuery;
DataSrc.Enabled := true;
end;
編輯:更多資訊。如果我注釋掉 Param 行并將 SQLStr :ATABLE 替換為連接的 SQLStr 和 case 變數 currentRecordType,則查詢確實有效。
uj5u.com熱心網友回復:
SQL 根本不允許FROM子句中的引數提供表名。因此,您只需要使用普通的字串連接,例如:
procedure TfrmDataModule.FindAllRecords(Sender: TObject; recordType: Integer);
var
ADOQuery : TADOQuery;
begin
case recordType of
1 : currentRecordType := 'ACCOUNTS';
2 : currentRecordType := 'CONTACTS';
3 : currentRecordType := 'USERS';
end;
{ Create the SQL query. }
ADOQuery := TADOQuery.Create(Self);
ADOQuery.Connection := ADOConn;
ADOQuery.SQL.Text := 'SELECT * FROM ' currentRecordType;
try
{ Set the query to Prepared--it will improve performance. }
ADOQuery.Prepared := true;
ADOQuery.Active := True;
except
on e: EADOError do begin
MessageDlg('Error while doing query', mtError, [mbOK], 0);
Exit;
end;
end;
{ Create the data source. }
DataSrc := TDataSource.Create(Self);
DataSrc.DataSet := ADOQuery;
DataSrc.Enabled := true;
end;
uj5u.com熱心網友回復:
歸功于@DelphiCoder,似乎不允許將引數作為 SQL 表名傳遞。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/522340.html
標籤:sql德尔福废话
