取clientdataset detal中的 更新数据, 将detal 转 数据库脚本sql
2016-11-25 11:29
337 查看
转自永南博客,更改update 脚本只取变化字段,更改排除blob与数组字段,这两个类型会报错
function vartosql(value: Variant): wideString;
var
tmp: widestring;
begin
if (varisnull(Value)) or (varisempty(Value)) then
Result := 'NULL'
else
case Vartype(value) of
varDate:
begin
tmp := formatDatetime('yyyy-mm-dd hh:mm:ss', VartoDatetime(Value));
Result := Quotedstr(tmp);
end;
varString, varOlestr:
Result := Quotedstr(Trim(Vartostr(Value)));
varboolean:
begin
if Value then
Result := '1'
else
Result := '0';
end;
varSmallint, varInteger, varDouble, varShortInt, varInt64, varLongWord, varCurrency:
begin
Result := trim(Vartostr(Value));
end;
else
Result := Quotedstr(Trim(Vartostr(Value)));
end;
end;
function GetCdsDetail(cdsDelta: TClientDataSet; TableName, KeyField, vWhere: WideString): WideString;
var
i: integer;
s1, s2: string;
CmdStr: string;
lcds: TClientDataSet;
begin
Result := '';
if (not cdsDelta.Active) and (cdsDelta.ChangeCount <= 0) then
Exit;
CmdStr := 'select * from '+TableName+' where 1=2' ;
lcds := TClientDataSet.Create(nil);
lcds.Data := Form1.SocketConnection1.AppServer.getdata('MPL', cmdstr);
for i := 1 to lcds.FieldCount do
if cdsDelta.FindField(lcds.Fields[i - 1].FieldName) <> nil then
cdsDelta.FindField(lcds.Fields[i - 1].FieldName).Tag := 1;
lcds.Close;
if cdsDelta.RecordCount > 0 then
begin
cdsDelta.First;
s1 := '';
s2 := '';
while not cdsDelta.Eof do
begin
CmdStr := '';
case cdsDelta.UpdateStatus of
usUnmodified:
begin
s2 := VarToSql(cdsDelta[KeyField]);
end;
usModified:
begin
s1 := '';
s2 := vWhere;
for i := 1 to cdsDelta.FieldCount do
// if (not cdsDelta.Fields[i - 1].IsNull) and (cdsDel
bb1e
ta.Fields[i - 1].Tag = 1) then
if (not cdsDelta.Fields[i - 1].IsBlob) and (not VarIsArray(cdsDelta.Fields[i - 1].Value)) then
if (cdsDelta.Fields[i - 1].NewValue <> Variants.Unassigned) and (cdsDelta.Fields[i - 1].Tag = 1)
and (cdsDelta.Fields[i - 1].OldValue <> cdsDelta.Fields[i - 1].NewValue) then
begin
if s1 = '' then
s1 := Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value)
else
s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value);
end;
if s1 <> '' then
begin
CmdStr := 'Update ' + TableName + ' Set ' + s1 + ' Where ' + KeyField + ' = ' + s2;
end;
end;
usInserted:
begin
s1 := '';
s2 := '';
for i := 1 to cdsDelta.FieldCount do
if (not cdsDelta.Fields[i - 1].IsNull) and (cdsDelta.Fields[i - 1].Tag = 1) then
begin
if s1 = '' then
begin
s1 := Trim(cdsDelta.Fields[i - 1].FieldName);
s2 := VarToSql(cdsDelta.Fields[i - 1].Value);
end
else
begin
s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName);
s2 := s2 + ',' + VarToSql(cdsDelta.Fields[i - 1].Value);
end;
end;
if s1 <> '' then
begin
CmdStr := 'Insert into ' + TableName + '(' + s1 + ') Values (' + s2 + ')';
end;
end;
usDeleted:
begin
s2 := VarToSql(cdsDelta[KeyField]);
CmdStr := 'Delete ' + TableName + ' Where ' + KeyField + ' = ' + s2;
end;
end;
Result := Result + CmdStr + #13#10;
cdsDelta.Next;
end;
end;
FreeAndNil(lcds);
end;
function vartosql(value: Variant): wideString;
var
tmp: widestring;
begin
if (varisnull(Value)) or (varisempty(Value)) then
Result := 'NULL'
else
case Vartype(value) of
varDate:
begin
tmp := formatDatetime('yyyy-mm-dd hh:mm:ss', VartoDatetime(Value));
Result := Quotedstr(tmp);
end;
varString, varOlestr:
Result := Quotedstr(Trim(Vartostr(Value)));
varboolean:
begin
if Value then
Result := '1'
else
Result := '0';
end;
varSmallint, varInteger, varDouble, varShortInt, varInt64, varLongWord, varCurrency:
begin
Result := trim(Vartostr(Value));
end;
else
Result := Quotedstr(Trim(Vartostr(Value)));
end;
end;
function GetCdsDetail(cdsDelta: TClientDataSet; TableName, KeyField, vWhere: WideString): WideString;
var
i: integer;
s1, s2: string;
CmdStr: string;
lcds: TClientDataSet;
begin
Result := '';
if (not cdsDelta.Active) and (cdsDelta.ChangeCount <= 0) then
Exit;
CmdStr := 'select * from '+TableName+' where 1=2' ;
lcds := TClientDataSet.Create(nil);
lcds.Data := Form1.SocketConnection1.AppServer.getdata('MPL', cmdstr);
for i := 1 to lcds.FieldCount do
if cdsDelta.FindField(lcds.Fields[i - 1].FieldName) <> nil then
cdsDelta.FindField(lcds.Fields[i - 1].FieldName).Tag := 1;
lcds.Close;
if cdsDelta.RecordCount > 0 then
begin
cdsDelta.First;
s1 := '';
s2 := '';
while not cdsDelta.Eof do
begin
CmdStr := '';
case cdsDelta.UpdateStatus of
usUnmodified:
begin
s2 := VarToSql(cdsDelta[KeyField]);
end;
usModified:
begin
s1 := '';
s2 := vWhere;
for i := 1 to cdsDelta.FieldCount do
// if (not cdsDelta.Fields[i - 1].IsNull) and (cdsDel
bb1e
ta.Fields[i - 1].Tag = 1) then
if (not cdsDelta.Fields[i - 1].IsBlob) and (not VarIsArray(cdsDelta.Fields[i - 1].Value)) then
if (cdsDelta.Fields[i - 1].NewValue <> Variants.Unassigned) and (cdsDelta.Fields[i - 1].Tag = 1)
and (cdsDelta.Fields[i - 1].OldValue <> cdsDelta.Fields[i - 1].NewValue) then
begin
if s1 = '' then
s1 := Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value)
else
s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value);
end;
if s1 <> '' then
begin
CmdStr := 'Update ' + TableName + ' Set ' + s1 + ' Where ' + KeyField + ' = ' + s2;
end;
end;
usInserted:
begin
s1 := '';
s2 := '';
for i := 1 to cdsDelta.FieldCount do
if (not cdsDelta.Fields[i - 1].IsNull) and (cdsDelta.Fields[i - 1].Tag = 1) then
begin
if s1 = '' then
begin
s1 := Trim(cdsDelta.Fields[i - 1].FieldName);
s2 := VarToSql(cdsDelta.Fields[i - 1].Value);
end
else
begin
s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName);
s2 := s2 + ',' + VarToSql(cdsDelta.Fields[i - 1].Value);
end;
end;
if s1 <> '' then
begin
CmdStr := 'Insert into ' + TableName + '(' + s1 + ') Values (' + s2 + ')';
end;
end;
usDeleted:
begin
s2 := VarToSql(cdsDelta[KeyField]);
CmdStr := 'Delete ' + TableName + ' Where ' + KeyField + ' = ' + s2;
end;
end;
Result := Result + CmdStr + #13#10;
cdsDelta.Next;
end;
end;
FreeAndNil(lcds);
end;
相关文章推荐
- 取clientdataset detal中的 更新数据, 将detal 转 数据库脚本sql
- 数据库更新Sql脚本总结
- 使用SQL Server Management Studio 2008 将数据库里的数据导成脚本
- sql编程 存储过程 之动态添加数据库表列并更新数据库表数据
- SQL Server 2005 导出包含(insert into)数据的SQL脚本 (使用存储过程) 分类: 数据库
- Scripts:报告物理数据库增长情况(注意脚本是看你数据库添加数据文件的时间哦)dba_db_growth.sql
- Android数据库更新,批量执行大量的sql脚本
- [软件共享]将数据库中的数据导出为SQL脚本
- SQL查找数据库中所有没有主键的数据表脚本
- 分享一段数据库中表数据更新SQL
- sql编程 存储过程 之 动态添加数据库表列并更新数据库表数据
- 数据库更新Sql脚本总结
- 利用SQL脚本向数据库导入EXCEL中的数据
- 编写存储过程将数据库中表中数据转换sql脚本语言
- Sql自动更新不同IP的数据库数据。(link Server)
- SqlServer2005(2000)数据库字典,将表数据生成SQL脚本的存储过程 (转)
- 一条sql语句(关于将一个表里的数据汇总,更新数据库中的另外一张表的列值
- Oracle数据库案例整理-执行Oracle脚本失败或异常-SQL命令中字符串参数使用双引号导致更新数据库表信息失败
- pl/sql中对比不同数据库中相同表,更新数据
- 根据数据库中数据表的内容导出insert... sql脚本的存储过程