转自永南博客,更改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 (cdsDelta.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;

 
 
http://blog.csdn.net/y281252548/article/details/53332584

取clientdataset detal中的 更新数据, 将detal 转 数据库脚本sql的更多相关文章

  1. echarts异步数据加载(在下拉框选择事件中异步更新数据)

    接触echarts 大半年了,从不会到熟练也做过不少的图表,隔了一段时间没使用这玩意,好多东西真心容易忘了.在接触echarts这期间也没有总结什么东西,今天我就来总结一下如何在echart中异步加载 ...

  2. Apex 中插入更新数据的事件执行顺序

    在使用 Apex 代码插入或更新数据的时候,若干事件会被按顺序执行.了解这些顺序可以提高调试程序的效率,也可以避免不必要的错误. 可以参考官方文档. 事件的执行顺序 从数据库中读取要更新的数据记录或初 ...

  3. scrapy项目3:爬取当当网中机器学习的数据及价格(spider类)

    1.网页解析 当当网中,人工智能数据的首页url如下为http://category.dangdang.com/cp01.54.12.00.00.00.html 点击下方的链接,一次观察各个页面的ur ...

  4. 根据从redis缓存的数据查询出来,在从数据库中取出所有的数据,俩个数据进行比较,去掉重复,剩下库中新插入的数据,取出新数据,然后把redis中的缓存数据清空把从数据库中查出来的所有数据放到redis缓存中

    参考代码: public String getNewCenter(HttpServletRequest request,HttpServletResponse resonse){ JSONObject ...

  5. 爬取网贷之家平台数据保存到mysql数据库

    # coding utf-8 import requests import json import datetime import pymysql user_agent = 'User-Agent: ...

  6. telerik:RadGrid 在表格中编辑更新数据

    对于 telerik 这个框架 我也不是很熟悉 也是刚刚开始学习  有兴趣的可以去官网看下 https://www.telerik.com/ 啥也不多说 直接上代码 首先是 telerik:RadGr ...

  7. scrapy项目4:爬取当当网中机器学习的数据及价格(CrawlSpider类)

    scrapy项目3中已经对网页规律作出解析,这里用crawlspider类对其内容进行爬取: 项目结构与项目3中相同如下图,唯一不同的为book.py文件 crawlspider类的爬虫文件book的 ...

  8. mysql中实现更新数据+1,再次更新数据-1

    $sql="update article set comment=comment^1 where a_id=2"; 这条语句你会发现当你更新comment字段时为1,再次更新时为0 ...

  9. magento中取不同store中的产品数据

    $products = Mage::getResourceModel('catalog/product_collection')                    ->setStoreId( ...

随机推荐

  1. hdu 1189 并查集

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations

    得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...

  3. MySQL什么时候会使用内部临时表?

    1.union执行过程 首先我们创建一个表t1 create table t1(id int primary key, a int, b int, index(a)); delimiter ;; cr ...

  4. ROS之:log文件

    1.log文件的位置: log file: /home/wang/.ros/log/... 1)显示隐藏文件(.文件),Ctrl+h 2)该文件夹下的文件夹是运行launch文件产生的 3)该文件夹下 ...

  5. python3.x之print()

    1.print内容 #!/usr/bin/python print('hello world')    //print("hello world") 2.print变量 #!/us ...

  6. mysql explain 的type解释

    原文:http://blog.csdn.net/github_26672553/article/details/52058782 Explain命令 用于分析sql语句的执行情况和成本预估 今天我们重 ...

  7. 【Windows7】win7启动 报错 AutoIt错误,不能打开脚本文件

    解决方法: 1.开始-->运行-->regedit 2.按照下图 3.

  8. windows redis 服务安装坑

    环境 winserver 2012 最新版的redis:3.0.503 redis-server.exe   --service-install   redis.windows.conf    --m ...

  9. 空暇时候思考2(&#39;\0&#39;等价于数字0还是字符0)

    /********************************************************************** * * Copyright (c)2015,WK Stu ...

  10. HDOJ How many ways?? 2157【矩阵高速幂】

    How many ways? ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...