時間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評論(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: //從原數(shù)據(jù)行取得修改條件
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;
關(guān)鍵詞標簽:sql語句
相關(guān)閱讀
熱門文章 Oracle中使用alter table來增加,刪除,修改列的語法 oracle中使用SQL語句修改字段類型-oracle修改SQL語句案例 誤刪Oracle數(shù)據(jù)庫實例的控制文件 為UNIX服務(wù)器設(shè)置Oracle全文檢索
人氣排行 oracle中使用SQL語句修改字段類型-oracle修改SQL語句案例 Oracle中使用alter table來增加,刪除,修改列的語法 ORACLE SQL 判斷字符串是否為數(shù)字的語句 ORACLE和SQL語法區(qū)別歸納(1) oracle grant 授權(quán)語句 ORACLE修改IP地址后如何能夠使用 如何加速Oracle大批量數(shù)據(jù)處理 Oracle刪除表的幾種方法