TADOQuery.ExecSQL所执行的SQL中可否包含PRINT、IF EXISTS等语句?

解决方案 »

  1.   

    一个SQL脚本文件,里面有--注释、GO等行,我写程序将每一个GO隔开的batch读出来用TADOQuery.ExecSQL来执行,读取SQL脚本时会过滤掉--注释。
    发现有些包含IF、PRINT的SQL语句执行会使ExecSQL返回-1。所以我有本贴的疑问。但是现在这个应该是排出了。但为什么会返回-1?当然,SQL语法应该没问题,Copy到查询分析器里就可以执行的。是不是每个batch执行后都需要TADOConnection.CommitTrans?
    举个例子如下:
             adoquery1.Close;
            adoquery1.SQL.Clear;
            adoquery1.SQL.Add('update table1 set col1 = 'test1' where col2= 'xx' ');
            adoquery1.ExecSQL;
            //第一次ExecSQL执行后没有commit
            adoquery1.Close;
            adoquery1.SQL.Clear;
            adoquery1.SQL.Add('update table1 set col1 = 'test2' where col2= 'xx' ');
            adoquery1.ExecSQL;
            //第二次ExecSQL是否就会有死锁问题?
     
      

  2.   

    在一个事务中
    第一条在更新时DBMS已为table1加了排它锁,并且这个锁的生命期为整个事务,所以第二个就会发生阻塞现象(注意:不是死锁)
      

  3.   


    如果是这样的话,那我只能一次将脚本文件里的SQL全部读出作为一个batch来ExecSQL了。之前分开执行,是希望当执行SQL报错时应用程序能提示给用户这个SQL脚本哪一行出现了问题,
    现在如果一次性执行,那我怎样能达到上面的目的呢?希望再赐教!谢谢!
      

  4.   


    原則上只要這個數據集一直使用同一個 AdoConnection , 他就是存在於同一個會話中。
      

  5.   


    简单讲,用应用程序去执行一个SQL脚本文件,怎样才能在SQL执行发生错误时,客户端pop出尽可能详细的信息?
      

  6.   

    并且如果发生错误时要将SQL对DB的影响全部Rollback!
      

  7.   

    try
     AdoQuery1.close;
     adoquery1.sql.text := 'sele[]]]]t getdate()';
     adoquery1.open;
    except
      on E: Exception do 
      Raise Exception.create(E.Message);
    end;
      

  8.   

    我的程序当ExecSQL执行的SQL影响的行数为0的时候,他的返回值是-1,TADOQuery.RowsAffected也是-1。不是返回0?是程序有问题还是??
      

  9.   


    阻塞一定会导致第二次的SQL执行失败?
    刚才我测试了下,第二次的更新有效的。
    我发现我的问题可能是ExecSQL返回值以及RowsAffected属性判断的失误,
    之前以为这两者返回-1的话表示有错误。
      

  10.   

    不明白的地方:
            adoquery1.Close; 
            adoquery1.SQL.Clear; 
            adoquery1.SQL.Add('update table1 set col1 = 'test' where 1=2 '); 
            adoquery1.ExecSQL; 
            //ExecSQL返回值0,RowsAffected =0;         adoquery1.Close; 
            adoquery1.SQL.Clear; 
            adoquery1.SQL.Add('if exists(select * from table1 where 1=2 )'); 
            adoquery1.SQL.Add('update table1 set col1 = 'test' where 1=2 '); 
            adoquery1.ExecSQL; 
            //ExecSQL返回值-1,RowsAffected = -1;
    这两次执行SQL对db的影响结果应该是一样的,但是TADOQuery给的返回值不一样,了解的帮小弟解释下。
            
      

  11.   

    if exists(select * from table1 where 1=2 后面的uodate语句没执行,当然返回-1了
      

  12.   

    Inspect RowsAffected to determine how many rows were updated or deleted by the last query operation. If no rows were updated or deleted, RowsAffected has a value of zero. RowsAffected will have a value of –1 if the execution of the SQL statement could not be executed due to an error condition. This latter situation would typically follow the raising of an exception.  除非執行出錯了。
      

  13.   


    var 
      sqlStr:String;
    begin
      sqlStr:= ' begin '
      sqlStr:= sqlStr+ 'update table1 set col1 = ''test'' where 1=2;';
      sqlStr:= sqlStr+ 'update table1 set col1 = ''test2'' where 1=2;';
      sqlStr:= sqlStr+ ' end ';  adoquery1.Close; 
      adoquery1.SQL.Clear; 
      adoquery1.SQL.Add(sqlStr); 
      adoquery1.ExecSQL;
    end;
    樓主參考一下,把你的sql語句用begin...end包起來,在提交給DB處理,就OK了!
      

  14.   

    if exists是标准的sql语句,当然可执行。print是输出文字,也可以
      

  15.   

    print执行会有意义吗?文字输出到什么地方?