熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

解析delta得到sql語句的函數

2013-11-13 16:08:11  來源: Oracle 

  {解析出sql語句}

  function TFormgensqls(AdoCon:TADOConnection; pdelta: OleVariant; const ptablename pkeyfields: WideString): WideString;

  var

  i j: integer;

  s s: string;

  Cmdstr: string;

  FieldList Keylist: TstringList;

  Cdsupdate: TClientDataSet;

  sqlstr: WideString;

  ado: TADOQuery;

  begin

  if varisnull(pdelta) then

  Exit;

  Cdsupdate:=TClientDataSetCreate(nil);

  Cdsupdatedata:=pdelta;

  if not CdsupdateActive then

  CdsupdateOpen;

  try

  FieldList:=TstringListCreate;

  Keylist:=TstringListCreate;

  KeylistDelimiter:=;

  KeylistDelimitedText:=pkeyfields;

  ado:=TADOQueryCreate(nil);

  adoConnection:=AdoCon;

  adosqlText:=select * from +ptablename+ where =;

  adoOpen;

  adoGetFieldNames(FieldList);

  adoFree;

  for i:= to FieldListCount do

  if CdsupdateFindField(FieldList[i])<>nil then

  CdsupdateFindField(FieldList[i])tag:=;

  FieldListFree;

  if CdsupdateRecordCount> then

  begin

  CdsupdateFirst;

  s:=;

  s:=;

  while not CdsupdateEof do

  begin

  Cmdstr:=;

  case CdsupdateUpdateStatus of

  usUnmodified: //從原數據行取得修改條件

  begin

  s:=;

  for j:= to KeylistCount do

  begin

  if s= then

  s:=Keylist[j]+=+vartosql(Cdsupdate[Keylist[j]])

  else

  s:=s+ and +Keylist[j]+=+vartosql(Cdsupdate[Keylist[j]]);

  end;

  end;

  usModified:

  begin

  s:=;

  for i:= to CdsupdateFieldCount do

  begin

  if (not CdsupdateFields[i]isNull)and(CdsupdateFields[i]tag=) then

  begin

  if s= then

  s:=Trim(CdsupdateFields[i]FieldName)+ = +vartosql(CdsupdateFields[i]value)

  else

  s:=s++Trim(CdsupdateFields[i]FieldName)+ = +vartosql(CdsupdateFields[i]value);

  end;

  end;

  if s<> then

  begin

  Cmdstr:= update +ptablename+ set +s+ where +s;

  end;

  end;

  usInserted:

  begin

  s:=;

  s:=;

  for i:= to CdsupdateFieldCount do

  if (not CdsupdateFields[i]isNull)and(CdsupdateFields[i]tag=) then

  begin

  if s= then

  begin

  s:=Trim(CdsupdateFields[i]FieldName);

  s:=vartosql(CdsupdateFields[i]value);

  end

  else

  begin

  s:=s++Trim(CdsupdateFields[i]FieldName);

  s:=s++vartosql(CdsupdateFields[i]value);

  end;

  end;

  if s<> then

  begin

  Cmdstr:= Insert into +ptablename+(+s+) values(+s+);

  end;

  end;

  usDeleted:

  begin

  s:=;

  for j:= to KeylistCount do

  begin

  if s= then

  s:=Keylist[j]+=+vartosql(Cdsupdate[Keylist[j]])

  else

  s:=s+ and +Keylist[j]+=+vartosql(Cdsupdate[Keylist[j]]);

  end;

  Cmdstr:=Delete +ptablename+ where +s;

  end;

  end;

  if Cmdstr<> then

  sqlstr:=sqlstr+Cmdstr+;+chr()+chr();

  CdsupdateNext;

  end;

  end;

  finally

  Cdsupdateclose;

  CdsupdateFree();

  end;

  Result:=sqlstr;

  end;


From:http://tw.wingwit.com/Article/program/Oracle/201311/17878.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.