含有
go 的
drop table table1
go
drop table table2
go
实际是两个脚本,一起提交会出错

解决方案 »

  1.   

    对,含有go 的连续的多个脚本提交不行,不知道能有模拟SQL查询分析器连续执行多个脚本功能的办法吗?
      

  2.   

    不是很明白你的意思---
    我想最好的方法是在DBMS上编好存储过程,在Delhpi中用ADOStoreProcedure加参数用就好了。安全,高效。
      

  3.   

    我觉得既然能够使用存储过程,就应该在服务器端写好,在delphi中调用;
    而没必要在delphi中定义存储过程。
      

  4.   

    将查询文件中转存为文本文件;然后执行如下代码:
    function Easy_CreateDatabaseObjects(Qy:TQuery;
       FileNames:tstringlist):Boolean;
    var
       i:integer;
    function ExecAFile(const FileName:string):Boolean;
    var
       str:string;
       a:tstringlist;
       i:integer;
       PC:Boolean;
    procedure DropNote(strlist:tstringlist);
    var
      f,t,i,j:integer;
    begin
      for i:=0 to strlist.count-1 do
      begin
        f:=Pos('--',strlist.strings[i]);
        if f>0 then
           strlist.strings[i]:=Copy(strlist.strings[i],1,f-1);
      end;  for i:=0 to strlist.count-1 do
      begin
        f:=Pos('/*',strlist.strings[i]);  //判断某一行是否含有“/*”//
        if f>0 then  //如果含有“/*”//
        begin
          t:=Pos('*/',strlist.strings[i]);
          strlist.strings[i]:=Copy(strlist.strings[i],1,f-1); //删除本行从/*开始的字符//
          if t>0 then continue; //如果在本行结束,则退出继续//      j:=i+1;  //从下一行开始搜索“*/”字符串//
          while j<strlist.count do
          begin
            t:=Pos('*/',strlist.strings[j]);
            if t<=0 then   //某行无*/则用“  ”替换全行//
               strlist.strings[j]:=' '
            else      //某行有*/结尾的字符,则删除*/及其前面的字符,结束本次搜索5t//
            begin
              strlist.strings[j]:=Copy(strlist.strings[j],t+2,
                 length(strlist.strings[j])-(t+1));
              break;
            end;
            j:=j+1;
          end;
        end;
      end;
    end;
    其中Qy为执行语句的TQuery构件;FileNames为多个文本文件的名称。
      

  5.   

    上面没写全,下面是完整的:
    function Easy_CreateDatabaseObjects(Qy:TQuery;
       FileNames:tstringlist):Boolean;
    var
       i:integer;
    function ExecAFile(const FileName:string):Boolean;
    var
       str:string;
       a:tstringlist;
       i:integer;
       PC:Boolean;
    procedure DropNote(strlist:tstringlist);
    var
      f,t,i,j:integer;
    begin
      for i:=0 to strlist.count-1 do
      begin
        f:=Pos('--',strlist.strings[i]);
        if f>0 then
           strlist.strings[i]:=Copy(strlist.strings[i],1,f-1);
      end;  for i:=0 to strlist.count-1 do
      begin
        f:=Pos('/*',strlist.strings[i]);  //判断某一行是否含有“/*”//
        if f>0 then  //如果含有“/*”//
        begin
          t:=Pos('*/',strlist.strings[i]);
          strlist.strings[i]:=Copy(strlist.strings[i],1,f-1); //删除本行从/*开始的字符//
          if t>0 then continue; //如果在本行结束,则退出继续//      j:=i+1;  //从下一行开始搜索“*/”字符串//
          while j<strlist.count do
          begin
            t:=Pos('*/',strlist.strings[j]);
            if t<=0 then   //某行无*/则用“  ”替换全行//
               strlist.strings[j]:=' '
            else      //某行有*/结尾的字符,则删除*/及其前面的字符,结束本次搜索5t//
            begin
              strlist.strings[j]:=Copy(strlist.strings[j],t+2,
                 length(strlist.strings[j])-(t+1));
              break;
            end;
            j:=j+1;
          end;
        end;
      end;
    end;begin
      result:=false;
      PC:=Qy.ParamCheck;
      Qy.ParamCheck:=false;
      a:=tstringlist.create;
      try
        try
          a.LoadFromFile(FileName);
          DropNote(a);
          a.SaveToFile('c:\SQLFile_001.txt');
          Qy.sql.clear;
          for i:=0 to a.count-1 do
          begin
            str:=trim(a.strings[i]);
            if str<>'' then
            begin
              if trim(str)<>'GO' then
                Qy.sql.append(str)//建立一个存储过程完了之后,后面紧跟一个GO,且单独成一行
              else
              begin
                Qy.execsql;
                Qy.sql.clear;
              end;
            end;
          end;
          result:=true;
        except
          Qy.SQL.SaveToFile('c:\Test_001.txt');
          showmessage('执行失败!');
          exit;
        end;
      finally
        a.free;
        Qy.ParamCheck:=PC;
      end;
    end;
    begin
      result:=false;
      if FileNames.count=0 then exit;
      for i:=0 to FileNames.count-1 do
         if ExecAFile(FileNames.Strings[i])=false then exit;
      result:=true;
    end;