これはあなたの質問に対する直接的な回答ではありませんが、さまざまなコマンドをデータベースに送信できるようにする SQL コマンド インタープリター ツールを作成しました。クエリの最初の単語が「select」でない場合、「execsql」がコマンドです。「select」については、そのクエリが Borland がライブ クエリと呼んでいる、インタラクティブな編集が可能なものかどうかを判断しました。私のプログラムの「クエリ」変数は、TDataSetProvider を介して TSQLDataSet に接続されている TClientDataSet ですが、これは AdoQuery でも機能するはずです。
procedure TForm1.Button1Click(Sender: TObject); // this is the 'execute query' button
var
i: integer;
cmd: string[6];
tmp: string;
begin
tmp:= mem.lines[0];
i:= 1;
while tmp[i] = ' ' do inc (i);
dec (i);
if i > 0 then tmp:= copy (tmp, i + 1, length (tmp) - i);
cmd:= '';
for i:= 1 to 6 do cmd:= cmd + upcase (tmp[i]);
query.close;
sdsquery.commandtext:= mem.text;
if cmd = 'SELECT' then
begin
if livequery then
begin
dbgrid.options:= dbgrid.Options + [dgEditing];
dbNavigator1.visiblebuttons:= [nbfirst, nbprior, nbNext, nbLast, nbInsert,
nbDelete, nbedit, nbpost, nbcancel,
nbrefresh];
end
else
begin
dbgrid.options:= dbgrid.Options - [dgEditing];
dbNavigator1.visiblebuttons:= [nbfirst, nbprior, nbNext, nbLast];
end;
query.open
end
else sdsquery.ExecSQL;
end;
function TForm1.LiveQuery: boolean;
// check what the second token after 'from' is
const
EOI = #26;
var
cmdlen, curpos: integer;
ch: char;
tmp: string;
Procedure GetChar;
begin
inc (curpos);
if curpos <= cmdlen
then ch:= mem.text[curpos]
else ch:= EOI;
end;
Function Token: string;
const
punct: set of char = [' ', ',', ';', EOI, #10, #13];
begin
result:= '';
while ch in punct do getchar;
while not (ch in punct) do
begin
result:= result + upcase (ch);
getchar
end
end;
begin
ch:= ' ';
cmdlen:= length (mem.text);
curpos:= 0;
tmp:= token;
while tmp <> 'FROM' do tmp:= token;
tmp:= token; // this should be the first table name
if ch = ',' then result:= false // select ... from table1, table2
else if ch = EOI then result:= true // select * from table1
else
begin
tmp:= token;
result:= (tmp = 'WHERE') or (tmp = 'ORDER')
end;
end;