分析from以后where以前的句子。常见的几种table1,table2 table1 a,table2 b table1 as a,table2 as b
procedure TForm1.Button1Click(Sender: TObject); var mSql,Str : String; MyList,tableList : TStringList; I : integer; begin try mSql:= 'select * from vTable1 , vTable2 where vtable1.a=vtable2.a' ; Str:= mSql; ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add(mSql); ADOQuery1.Open; myList:=TStringList.Create; ADOQuery1.Connection.GetTableNames(MyList); I:=pos('from',Str); if I>0 then delete(Str,1,I+3); I:=Pos('Where',str); if I>0 then delete(str,I,length(str)); str:=Stringreplace(str,',',#13,[rfReplaceAll]); tableList:=TStringList.create; tableList.Text:=Str; str:=''; for I:=0 to tableList.Count-1 do begin if myList.IndexOf(Trim(tableList[I]))>=0 then str:=str+ tableList[I]+#13; end; Showmessage(str); finally freeandnil(myList); freeandnil(tableList); end;end;
table1 a,table2 b
table1 as a,table2 as b
var
mSql,Str : String;
MyList,tableList : TStringList;
I : integer;
begin
try
mSql:= 'select * from vTable1 , vTable2 where vtable1.a=vtable2.a' ;
Str:= mSql;
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(mSql);
ADOQuery1.Open;
myList:=TStringList.Create;
ADOQuery1.Connection.GetTableNames(MyList);
I:=pos('from',Str);
if I>0 then
delete(Str,1,I+3);
I:=Pos('Where',str);
if I>0 then
delete(str,I,length(str));
str:=Stringreplace(str,',',#13,[rfReplaceAll]);
tableList:=TStringList.create;
tableList.Text:=Str;
str:='';
for I:=0 to tableList.Count-1 do
begin
if myList.IndexOf(Trim(tableList[I]))>=0 then
str:=str+ tableList[I]+#13;
end;
Showmessage(str);
finally
freeandnil(myList);
freeandnil(tableList);
end;end;
http://www.anylib.com
如果我把sql语句在sqlserver的视图分析器中执行,就回看到对应表的图形,其中就列出sql语句中所有表名,如果表是别名,则显示别名 ;因此,我认为SqlServer能做到,Delphi也能做得到,Delphi取得表名的函数或许没有公布(只是个人意见)。
database1.GetTableNames();
ADO:
adoconnection1.GetTableNames();