主从表提交(200分)

  • 主题发起人 主题发起人 hxy2002
  • 开始时间 开始时间
H

hxy2002

Unregistered / Unconfirmed
GUEST, unregistred user!
MSSQL主从表,<br>ID为自增<br>主从表提交,主表新增的ID如何取到,是否要用储存过程或触发器实现<br>或者有什么好的见意.
 
用事务吧!<br>dm1.adoconnection1.begintranaction;<br>.....<br>.....<br>dm1.adoconnection1.committransaction;<br>用主表提交后产生的ID填充从表
 
获取主表ID呢?
 
insert into ...<br>set @id=@@identity<br><br>记住:一定要在insert以后马上得到
 
主从架构应该要有一个编号,这个编号也是唯一的、并提供关联的。
 
如果主从表你用ID关联的话会出现很多麻烦问题的,这样的关联一般采用编号来完成.
 
function Tform1.updatetable(ados:Tadoquery;lcTable:string):Boolean;<br>var<br>&nbsp; i:integer;<br>&nbsp; msql:string;<br>&nbsp; lnparm:string;<br>&nbsp; lnkey:variant;<br>&nbsp; msql1,msql2,msql3:string;<br>&nbsp; adod:Tadoquery;<br>// &nbsp;lcold,lcnew:variant;<br>begin<br>&nbsp; lnparm:='';<br>&nbsp; msql:='';<br>&nbsp; adod:=Tadoquery.Create(nil);<br>&nbsp; adod.Connection:=form1.ADOConnection1;<br>&nbsp; adod.Close;<br>&nbsp; adod.SQL.Clear;<br>&nbsp; lnkey:=ados.Fields[0].AsInteger;<br>&nbsp; if (lnkey&gt;0) &nbsp;then<br>&nbsp; begin<br>&nbsp; msql1:='update '+lcTable +' set ';<br>&nbsp; msql2:='';<br>&nbsp; msql3:='';<br>&nbsp; for i:=1 to ados.FieldCount-1 do<br>&nbsp; &nbsp; if ((copy(ados.Fields.FieldName,0,2) &lt;&gt; 'tmp') and (ados.Fields.FieldName &lt;&gt; 'iren') &nbsp;and &nbsp;(ados.Fields.FieldName &lt;&gt; 'idate') and (ados.Fields.FieldName &lt;&gt; 'uren') &nbsp;and &nbsp;(ados.Fields.FieldName &lt;&gt; 'udate')) then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ados.Fields.OldValue&lt;&gt;ados.Fields.Value then &nbsp;----&gt; ERROR "Invaild variant operation" please help me<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msql2:=msql2+ados.Fields.FieldName+'=:'+ados.Fields.FieldName+',';<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;// &nbsp;end;<br>&nbsp; msql2:=copy(trim(msql2),0,length(trim(msql2))-1);<br>&nbsp; if (length(msql2) &lt;&gt; 0) then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; msql1:=msql1+msql2+',uren=:uren,udate=:udate where id=:id';<br>&nbsp; &nbsp; &nbsp; &nbsp; adod.SQL.add(msql1);<br>&nbsp; &nbsp; &nbsp; &nbsp; for i:=1 to ados.FieldCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((copy(ados.Fields.FieldName,0,2) &lt;&gt; 'tmp') and (ados.Fields.FieldName &lt;&gt; 'iren') &nbsp;and &nbsp;(ados.Fields.FieldName &lt;&gt; 'idate') and (ados.Fields.FieldName &lt;&gt; 'uren') &nbsp;and &nbsp;(ados.Fields.FieldName &lt;&gt; 'udate')) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if [red]ados.Recordset.Fields.OriginalValue&lt;&gt;ados.Recordset.Fields.Value[/red] then ---<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lnparm:=ados.Fields.FieldName ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adod.Parameters.ParamByName(lnparm).Value:=ados.Fields.Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; adod.Parameters.ParamByName('uren').Value :='admin';<br>&nbsp; &nbsp; &nbsp; &nbsp; adod.Parameters.ParamByName('udate').Value:=now();<br>&nbsp; &nbsp; &nbsp; &nbsp; adod.Parameters.ParamByName('id').value:=ados.Fields[0].Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; adod.Prepared:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adod.ExecSQL;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; adod.Free;<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp;else<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; msql1:=' insert into '+lcTable+ '(';<br>&nbsp; &nbsp; &nbsp; for i:=1 to ados.FieldCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((copy(ados.Fields.FieldName,0,2) &lt;&gt; 'TMP') and (ados.Fields.FieldName &lt;&gt; 'UREN') &nbsp;and &nbsp;(ados.Fields.FieldName &lt;&gt; 'UDATE')) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msql2:=msql2+ ados.Fields.FieldName+',';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msql3:=msql3+':'+ados.Fields.FieldName+',';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp;msql2:=copy(trim(msql2),0,length(trim(msql2))-1);<br>&nbsp; &nbsp; &nbsp; &nbsp;msql3:=copy(trim(msql3),0,length(trim(msql3))-1);<br>&nbsp; &nbsp; &nbsp; msql:=msql1+msql2+') values('+msql3+')';<br>&nbsp; &nbsp; &nbsp; &nbsp;adod.SQL.Add(msql);<br>&nbsp; &nbsp; &nbsp; &nbsp;for i:=1 to ados.FieldCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((copy(ados.Fields.FieldName,0,2) &lt;&gt; 'TMP') and (ados.Fields.FieldName &lt;&gt; 'UREN') &nbsp;and &nbsp;(ados.Fields.FieldName &lt;&gt; 'UDATE')) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lnparm:=ados.Fields.FieldName ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adod.Parameters.ParamByName(lnparm).Value:=ados.Fields.Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;adod.Prepared:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adod.ExecSQL;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp;adod.Free;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp;end;
 
建议<br>1.不要使用delphi自己的主键关联<br>2.明细记录的关联id,自己填充<br>&nbsp; zqryDetail.fieldbyname('masterid').asstring := zQryMaster.fieldbyname('id').asstring;<br>3.如果明细记录也是一个关联查询,用betterado / zeoslib(可以用updatesql组件生成insert,delete,modify语句,update/post和单表相同) ,不要使用ado ,ado 没有 updatesql 组件<br>4.明细表可以使用自动编号字段做primary key ,也可以自己编(使用单字段做primary key)<br>5.最好全部使用非感知控件
 
如果ID字段不用自增长,而用取最大值的方式(当然还要用个表专门存放每个表的ID当前最大值,以免多用户环境下冲突),分别写入主从表,这样有什么问题没有?我目前就用的这个方法
 
最好不要使用自增字段,在删除记录或是取最大值等会碰到很多麻烦。<br>建议主键自己定义,如字符串等,生成主键后再同记录一起写入数据库。
 
"当然还要用个表专门存放每个表的ID当前最大值,以免多用户环境下冲突"<br>这样能免吗?
 
delphi的adotable可以设置主从表吧
 
你可以在BefoPost或AfterPost确定是否要进行新增子表数据呀..
 
1. 切记不要用自增字段<br>2. 切记不要用编号、自增字段作为表关联,用GUID吧,很好用<br>3. 编号可以创建流水账号表进行维护,写函数取得下一个编号
 
问题不明,不知道想表达什么样的问题(明细一点的)
 
在主表的DataSet中的AfterPost,AfterInsert,AfterApplyUpdate你挨个试试。。。
 
单用户环境下,想用什么就用什么,在多用户环境下(网络版),本人认为适当地用一下自增量还是比较有效率的。
 
主单更新后就可以取到最大的ID号<br>select max(id) as id from 主单表名
 
我觉得用一个专用表存放主ID,然后用一个存储过程通过锁此表,设定最新值、解锁、然后返回新值,感觉这样可以解决并发问题、拙见,请其他大侠指教。
 
你到以下地址去找找,可能找得到你需要的答案。<br>http://iinsnian.cnblogs.com/<br>http://del.cnblogs.com/ <br>这两个地址里介绍了很多DELPHI的技巧,不知道你的运气好不好。
 

Similar threads

回复
0
查看
848
不得闲
D
回复
0
查看
579
DelphiTeacher的专栏
D
D
回复
0
查看
827
DelphiTeacher的专栏
D
D
回复
0
查看
784
DelphiTeacher的专栏
D
后退
顶部