ado拼接sql中间如果有错误无法rollback
qryTemp2是ADOQuery
1.如果sql一句一句执行,这个是没问题的,比如。这种情况能正常回滚,一句也插不进
qryTemp2.Connection.BeginTrans;
try
qryTemp2.Close;
qryTemp2.SQL.Text := 'insert sql1,这个不会出错';
qryTemp2.ExecSQL;
qryTemp2.Close;
qryTemp2.SQL.Text :='insert sql2,这个会出错';
qryTemp2.ExecSQL;
qryTemp2.Connection.CommitTrans;
except
on e:Exception do
begin
qryTemp2.Connection.RollbackTrans;
Exit;
end;
end;
2.如果是拼接sql,结果sql1还是插入了,没有回滚,为什么?有办法解决么,有时候按照1方案写太麻烦了
qryTemp2.Connection.BeginTrans;
try
qryTemp2.Close;
qryTemp2.SQL.Text := 'insert sql1,这个不会出错'+#13#10+
'insert sql2,这个会出错'; //这里加不加begin end都不会回滚
qryTemp2.ExecSQL;
qryTemp2.Connection.CommitTrans;
except
on e:Exception do
begin
qryTemp2.Connection.RollbackTrans;
Exit;
end;
end;