资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

菜鸟提问:有什么方法解决这个问题啊?马上给分!

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

我的代码如下:  
  delcare   yy   cursor   for  
  select   b_name,b_mph  
  from   table_1;  
  open   yy;  
  do   while   sqlca.sqlcode=0  
  fetch   yy   into   :ls_name,:ls_mph;  
  insert   into   table_2(s_name,s_mph)  
  values   (:ls_name,:ls_mph)  
  fetch   yy   into:ls_name,:ls_mph;  
  loop  
  close   yy;  
  实现的是把表一table_1中的值添加到表二table_2中去,  
  可是添加完后如果把table_1中某列的值修改,表二table_2的中的相应的值并没有被修改,  
  请问:该如何把修改后的值也保存到表二中去?  
  谢谢拉!

网友回答:

发表者:daixf_csdn

写的不对吧:  
  应该是:  
  delcare   yy   cursor   for  
  select   b_name,b_mph  
  from   table_1;  
  open   yy;  
  fetch   yy   into   :ls_name,:ls_mph;  
   
  do   while   sqlca.sqlcode=0  
  insert   into   table_2(s_name,s_mph)  
  values   (:ls_name,:ls_mph)  
  fetch   yy   into:ls_name,:ls_mph;  
  loop  
  close   yy;  
 

发表者:zwd1975

添加完了,表一和表二就没有关系了,所以表二不会被修改。  
  1.你可以考虑做一个触发器,连你这段程序也省了。  
  2.也可以再写一段代码,修改表一时也修改表二。

发表者:luotitan

当然是用触发器。不知道楼主用的什么数据库。如果是SQL   SERVER,可以参考SQL   SERVER   帮助中的下面这个例子。  
   
  下例创建两个表:一个   employeeData   表和一个   auditEmployeeData   表。人力资源部的成员可以修改   employeeData   表,该表包含敏感的雇员薪水信息。如果更改了雇员的社会保险号码   (SSN)、年薪或银行帐户,则生成审核记录并插入到   auditEmployeeData   审核表。  
   
  通过使用   COLUMNS_UPDATED()   功能,可以快速测试对这些包含敏感雇员信息的列所做的更改。只有在试图检测对表中的前   8   列所做的更改时,COLUMNS_UPDATED()   才起作用。  
   
  USE   pubs  
  IF   EXISTS(SELECT   TABLE_NAME   FROM   INFORMATION_SCHEMA.TABLES  
        WHERE   TABLE_NAME   =   employeeData)  
        DROP   TABLE   employeeData  
  IF   EXISTS(SELECT   TABLE_NAME   FROM   INFORMATION_SCHEMA.TABLES  
        WHERE   TABLE_NAME   =   auditEmployeeData)  
        DROP   TABLE   auditEmployeeData  
  GO  
  CREATE   TABLE   employeeData   (  
        emp_id   int   NOT   NULL,  
        emp_bankAccountNumber   char   (10)   NOT   NULL,  
        emp_salary   int   NOT   NULL,  
        emp_SSN   char   (11)   NOT   NULL,  
        emp_lname   nchar   (32)   NOT   NULL,  
        emp_fname   nchar   (32)   NOT   NULL,  
        emp_manager   int   NOT   NULL  
        )  
  GO  
  CREATE   TABLE   auditEmployeeData   (  
        audit_log_id   uniqueidentifier   DEFAULT   NEWID(),  
        audit_log_type   char   (3)   NOT   NULL,  
        audit_emp_id   int   NOT   NULL,  
        audit_emp_bankAccountNumber   char   (10)   NULL,  
        audit_emp_salary   int   NULL,  
        audit_emp_SSN   char   (11)   NULL,  
        audit_user   sysname   DEFAULT   SUSER_SNAME(),  
        audit_changed   datetime   DEFAULT   GETDATE()  
        )  
  GO  
  CREATE   TRIGGER   updEmployeeData    
  ON   employeeData    
  FOR   update   AS  
  /*Check   whether   columns   2,   3   or   4   has   been   updated.   If   any   or   all   of   columns   2,   3   or   4   have   been   changed,   create   an   audit   record.   The   bitmask   is:   power(2,(2-1))+power(2,(3-1))+power(2,(4-1))   =   14.   To   check   if   all   columns   2,   3,   and   4   are   updated,   use   =   14   in   place   of   >0   (below).*/  
   
        IF   (COLUMNS_UPDATED()   &   14)   >   0  
  /*Use   IF   (COLUMNS_UPDATED()   &   14)   =   14   to   see   if   all   of   columns   2,   3,   and   4   are   updated.*/  
              BEGIN  
  --   Audit   OLD   record.  
              INSERT   INTO   auditEmployeeData  
                    (audit_log_type,  
                    audit_emp_id,  
                    audit_emp_bankAccountNumber,  
                    audit_emp_salary,  
                    audit_emp_SSN)  
                    SELECT   OLD,    
                          del.emp_id,  
                          del.emp_bankAccountNumber,  
                          del.emp_salary,  
                          del.emp_SSN  
                    FROM   deleted   del  
   
  --   Audit   NEW   record.  
              INSERT   INTO   auditEmployeeData  
                    (audit_log_type,  
                    audit_emp_id,  
                    audit_emp_bankAccountNumber,  
                    audit_emp_salary,  
                    audit_emp_SSN)  
                    SELECT   NEW,  
                          ins.emp_id,  
                          ins.emp_bankAccountNumber,  
                          ins.emp_salary,  
                          ins.emp_SSN  
                    FROM   inserted   ins  
        END  
  GO  
   
  /*Inserting   a   new   employee   does   not   cause   the   UPDATE   trigger   to   fire.*/  
  INSERT   INTO   employeeData  
        VALUES   (   101,   USA-987-01,   23000,   R-M53550M,   NMendel,   NRoland,   32)  
  GO  
   
  /*Updating   the   employee   record   for   employee   number   101   to   change   the   salary   to   51000   causes   the   UPDATE   trigger   to   fire   and   an   audit   trail   to   be   produced.*/  
   
  UPDATE   employeeData  
        SET   emp_salary   =   51000  
        WHERE   emp_id   =   101  
  GO  
  SELECT   *   FROM   auditEmployeeData  
  GO  
   
  /*Updating   the   employee   record   for   employee   number   101   to   change   both   the   bank   account   number   and   social   security   number   (SSN)   causes   the   UPDATE   trigger   to   fire   and   an   audit   trail   to   be   produced.*/  
   
  UPDATE   employeeData  
        SET   emp_bankAccountNumber   =   133146A0,   emp_SSN   =   R-M53550M  
        WHERE   emp_id   =   101  
  GO  
  SELECT   *   FROM   auditEmployeeData  
  GO  
   
   
 

发表者:ghostagain

楼主的问题我遇见过,你可以看看SQLERRTEXT就晓得了,“事务对象已经打开!”  
  原因是你在游标内部也使用了SQL语句,而游标和SQL语句同时都是用的是SQLCA这个默认的事务对象,所以你的游标也只执行了一次,不信你可以自己写代码调试:  
   
  在fetch   yy   into:ls_name,:ls_mph;和加上:  
  messagebox   (   ls_name,   lsm_mph)  
  这个你只能看到一次。  
   
  不晓得楼上几位说的方法行不行,但你可以自己创建一是事务对象MY_SQQLCA,在insert语句用自定义USNIG   MY_SQLCA   ;   就要得了

发表者:ghostagain

this   is   a   eg.   :  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
  //  
  //   boolean   uf_subject_stat_update   (string   ls_subjectNum,   string   Ls_debitOrCredit,   double   ld_money)      
  // ¹¦ÄÜ£º  
  // Ð޸ĿÆÄ¿±íµÄµ±Ç°½è/´û·½    
  //   ²ÎÊý£º  
  // string   ls_subjectNum ÒªÐ޸ĵĿÆÄ¿ºÅ  
  // string   ls_debitOrcredit Ð޸Ľ跽»¹ÊÇ´û·½  
  // double   ld_money µ±Ç°½è/´û·½Óà¶î½«Ôö¼ÓµÄ½ð¶î  
  //   ·µ»Ø£º  
  // boolean       ͳ¼Æ³É¹¦£ºTRUE   ʧ°Ü£ºFALSE  
  // ×÷Õߣº  
  // YBW  
  //  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
   
   
  //ÊÇÔÚÓαêµÄ´¦Àí¹ý³ÌÖжÔsubject±íÐ޸ģ¬Ê¹ÓÃ×Ô¶¨ÒåÊÂÎñ¶ÔÏó´¦Àí  
  Transaction   my_sqlca  
  my_sqlca   =   create   Transaction  
  my_SQLCA.DBMS   =   "ODBC"  
  my_SQLCA.AutoCommit   =   true  
  my_sqlca.lock="RU"  
  mY_SQLCA.DBParm   =   "ConnectString=DSN=cw;UID=dba;PWD=sql"  
  connect   using   my_sqlca;  
  if   my_sqlca.sqlcode   <>   0   then    
  messagebox   ("&sup2;&Ugrave;×÷&Ograve;ì&sup3;&pound;","&sup2;&raquo;&Auml;&Uuml;&ETH;&acute;&iquest;&AElig;&Auml;&iquest;±í&pound;&not;&Ccedil;&euml;&Eacute;&Ocirc;&ordm;ó&Ocirc;&Ugrave;&Ecirc;&Ocirc;&pound;&iexcl;~n~n&Iuml;ê&Iuml;&cedil;&ETH;&Aring;&Iuml;&cent;&pound;&ordm;"   +   my_sqlca.sqlErrText)  
  destroy   my_sqlca;  
  return   false  
  end   if  
     
  //&ETH;&THORN;&cedil;&Auml;&iquest;&AElig;&Auml;&iquest;&micro;&Auml;&micro;±&Ccedil;°&frac12;è&acute;&ucirc;·&frac12;&Oacute;à&para;&icirc;.....  
  if   trim   (   ls_debitOrCredit   )   =   "&frac12;è·&frac12;"   then       //&frac12;è·&frac12;  
  update   "subject"  
  set   "currdebitbalance"   =   "currdebitbalance"   +   :ld_money  
  where   "num"   =   :   ls_subjectNum  
  using   my_sqlca;  
  else   /////////////////////////&acute;&ucirc;·&frac12;  
  update   "subject"  
  set   "currlenderbalance"   =   "currlenderbalance"   +   :ld_money  
  where   "num"   =   :   ls_subjectNum    
  using   mY_sqlca;  
  end   if  
   
  if   my_sqlca.sqlcode   <>   0   then  
  messageBox   ("&Ograve;ì&sup3;&pound;&Ccedil;é&iquest;&ouml;","&raquo;&atilde;×&Uuml;&iquest;&AElig;&Auml;&iquest;·&cent;&Eacute;ú&acute;í&Icirc;ó&pound;&iexcl;~n~n"   +&  
  "&acute;í&Icirc;ó&Ocirc;&shy;&Ograve;ò&pound;&ordm;"   +   my_sqlca.sqlErrText)  
  rollBack   using   my_sqlca;  
  disconnect   using   my_sqlca;  
  destroy   my_sqlca  
  return   false    
  end   if  
   
  commit   using   my_sqlca;  
  disconnect   using     my_sqlca   ;  
  destroy   my_sqlca  
  return   true


 

 
热门推荐笔记本: IBM笔记本
相关文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved