IT猫扑网:您身边最放心的安全下载站! 最新更新|软件分类|软件专题|手机版|论坛转贴|软件发布

您当前所在位置:首页数据库Oracle → 解析delta得到sql语句的函数

解析delta得到sql语句的函数

时间:2015/6/28来源:IT猫扑网作者:网管联盟我要评论(0)

  {解析出sql语句}

  function TForm1.gensqls(AdoCon:TADOConnection; pdelta: OleVariant; const ptablename, pkeyfields: WideString): WideString;

  var

  i, j: integer;

  s1, s2: string;

  Cmdstr: string;

  FieldList, Keylist: TstringList;

  Cdsupdate: TClientDataSet;

  sqlstr: WideString;

  ado: TADOQuery;

  begin

  if varisnull(pdelta) then

  Exit;

  Cdsupdate:=TClientDataSet.Create(nil);

  Cdsupdate.data:=pdelta;

  if not Cdsupdate.Active then

  Cdsupdate.Open;

  try

  FieldList:=TstringList.Create;

  Keylist:=TstringList.Create;

  Keylist.Delimiter:=',';

  Keylist.DelimitedText:=pkeyfields;

  ado:=TADOQuery.Create(nil);

  ado.Connection:=AdoCon;

  ado.sql.Text:='select * from '+ptablename+' where 1=0';

  ado.Open;

  ado.GetFieldNames(FieldList);

  ado.Free;

  for i:=1 to FieldList.Count do

  if Cdsupdate.FindField(FieldList[i-1])<>nil then

  Cdsupdate.FindField(FieldList[i-1]).tag:=1;

  FieldList.Free;

  if Cdsupdate.RecordCount>0 then

  begin

  Cdsupdate.First;

  s1:='';

  s2:='';

  while not Cdsupdate.Eof do

  begin

  Cmdstr:='';

  case Cdsupdate.UpdateStatus of

  usUnmodified: //从原数据行取得修改条件

  begin

  s2:='';

  for j:=1 to Keylist.Count do

  begin

  if s2='' then

  s2:=Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]])

  else

  s2:=s2+' and '+Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]]);

  end;

  end;

  usModified:

  begin

  s1:='';

  for i:=1 to Cdsupdate.FieldCount do

  begin

  if (not Cdsupdate.Fields[i-1].isNull)and(Cdsupdate.Fields[i-1].tag=1) then

  begin

  if s1='' then

  s1:=Trim(Cdsupdate.Fields[i-1].FieldName)+' = '+vartosql(Cdsupdate.Fields[i-1].value)

  else

  s1:=s1+','+Trim(Cdsupdate.Fields[i-1].FieldName)+' = '+vartosql(Cdsupdate.Fields[i-1].value);

  end;

  end;

  if s1<>'' then

  begin

  Cmdstr:=' update '+ptablename+' set '+s1+' where '+s2;

  end;

  end;

  usInserted:

  begin

  s1:='';

  s2:='';

  for i:=1 to Cdsupdate.FieldCount do

  if (not Cdsupdate.Fields[i-1].isNull)and(Cdsupdate.Fields[i-1].tag=1) then

  begin

  if s1='' then

  begin

  s1:=Trim(Cdsupdate.Fields[i-1].FieldName);

  s2:=vartosql(Cdsupdate.Fields[i-1].value);

  end

  else

  begin

  s1:=s1+','+Trim(Cdsupdate.Fields[i-1].FieldName);

  s2:=s2+','+vartosql(Cdsupdate.Fields[i-1].value);

  end;

  end;

  if s1<>'' then

  begin

  Cmdstr:=' Insert into '+ptablename+'('+s1+') values('+s2+')';

  end;

  end;

  usDeleted:

  begin

  s2:='';

  for j:=1 to Keylist.Count do

  begin

  if s2='' then

  s2:=Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]])

  else

  s2:=s2+' and '+Keylist[j-1]+'='+vartosql(Cdsupdate[Keylist[j-1]]);

  end;

  Cmdstr:='Delete '+ptablename+' where '+s2;

  end;

  end;

  if Cmdstr<>'' then

  sqlstr:=sqlstr+Cmdstr+';'+chr(13)+chr(10);

  Cdsupdate.Next;

  end;

  end;

  finally

  Cdsupdate.close;

  Cdsupdate.Free();

  end;

  Result:=sqlstr;

  end;

关键词标签:sql语句

相关阅读

文章评论
发表评论

热门文章 Oracle中使用alter table来增加,删除,修改列Oracle中使用alter table来增加,删除,修改列oracle中使用SQL语句修改字段类型-oracle修oracle中使用SQL语句修改字段类型-oracle修使用低权限Oracle数据库账户得到管理员权限使用低权限Oracle数据库账户得到管理员权限Oracle对user的访问控制Oracle对user的访问控制

相关下载

人气排行 ORACLE SQL 判断字符串是否为数字的语句Oracle中使用alter table来增加,删除,修改列的语法ORACLE和SQL语法区别归纳(1)oracle grant 授权语句如何加速Oracle大批量数据处理Oracle删除表的几种方法ORACLE修改IP地址后如何能够使用Oracle 10g创建表空间和用户并指定权限