怎样取得 Sql 语句中的所有表名
如   : 
Sql语句为:  'select * from vTable1 , vTable2 where vtable1.a=vtable2.a';需要取得表名: vtable1和vtable2;

解决方案 »

  1.   

    这个不好取,Sql语法中哪里都有可能出现表,如果非要找规则,建议你先读完联机帮助
      

  2.   

    分析from以后where以前的句子。常见的几种table1,table2
    table1 a,table2 b
    table1 as a,table2 as b
      

  3.   

    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;
      

  4.   

    自由界面和报表的完美解决方案!
    http://www.anylib.com
      

  5.   

    我也想过对Sql语句进行分析,取得表名称,但感觉比较复杂,而且比较容易出错;
    如果我把sql语句在sqlserver的视图分析器中执行,就回看到对应表的图形,其中就列出sql语句中所有表名,如果表是别名,则显示别名 ;因此,我认为SqlServer能做到,Delphi也能做得到,Delphi取得表名的函数或许没有公布(只是个人意见)。
      

  6.   

    BDE:
    database1.GetTableNames();
    ADO:
    adoconnection1.GetTableNames();