怎样提取表中主键的值(100分)

  • 主题发起人 主题发起人 jt11
  • 开始时间 开始时间
J

jt11

Unregistered / Unconfirmed
GUEST, unregistred user!
表中设有主键是整型并且自增,我向表中插入一行后怎样能够取得该字段值?
 
//取得最新添加的记录的自增列序号<br>Function GetNewRecordPos(Conn:TADOConnection; CDS:TClientDataSet; TableName,AutoField:String):Integer;<br>var I:Integer;<br>&nbsp; tpWhere:String;<br>&nbsp; tpVal:Variant;<br>begin<br>&nbsp; Result := -1;<br>&nbsp; tpWhere:='';<br>&nbsp; with CDS do begin<br>&nbsp; &nbsp; For i := 0 to Fields.Count - 1 do<br>&nbsp; &nbsp; &nbsp; if Not StrIsSame(Fields.FieldName,AutoField) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Not (StrIsEmpty(tpWhere) or StrIsEnd(' And',Trim(tpWhere))) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpWhere:=tpWhere + ' and ';<br>&nbsp; &nbsp; &nbsp; &nbsp; if Fields.IsNull then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpWhere:= tpWhere + '('+Fields.FieldName +' is null)'<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if Fields.DataType in [ftString,ftDate,ftWideString,ftFixedChar] then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpWhere:= tpWhere + '('+Fields.FieldName +'='+QuotedStr(Fields.AsString)+')'<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if Fields.DataType in [ftSmallint, ftInteger, ftWord,ftFloat, ftCurrency, ftBCD,ftBytes] then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpWhere:= tpWhere + '('+Fields.FieldName +'='+ Fields.AsString+')'<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if Fields.DataType in [ftBoolean] then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Fields.AsBoolean then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpWhere:= tpWhere + '('+Fields.FieldName +'=1)'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpWhere:= tpWhere + '('+Fields.FieldName +'=0)';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; if Not StrIsEmpty(tpWhere) then begin<br>&nbsp; &nbsp; tpWhere := tpWhere +' Order by '+AutoField+' Desc';<br>&nbsp; &nbsp; tpVal := ValueHelp(Conn, TableName, AutoField,tpWhere, Null);<br>&nbsp; &nbsp; if VarIsArray(tpVal) then begin<br>&nbsp; &nbsp; &nbsp; tpVal:=tpVal[0];<br>&nbsp; &nbsp; &nbsp; if Not VarIsNull(tpVal) then<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:= tpVal<br>&nbsp; &nbsp; &nbsp; &nbsp; Except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; end;<br>end;<br><br>Function ValueHelp(Conn:TADOConnection; Const TableName,ReturnFeilds,KeyName:String;KeyValue:Variant):Variant;<br>var i:integer;<br>&nbsp; tpQuery:TADOQuery;<br>&nbsp; tpVal:Variant;<br>&nbsp; LocalHelp:Boolean;<br>begin<br>&nbsp; tpQuery:=nil;<br>&nbsp; LocalHelp:=False;<br>&nbsp; try<br>&nbsp; &nbsp; tpQuery:=tADOQuery.Create(Nil);<br>&nbsp; &nbsp; tpQuery.EnableBCD := False;<br>&nbsp; &nbsp; if isTableExist(utLocalConn,TableName) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; tpQuery.Connection:=utLocalConn;<br>&nbsp; &nbsp; &nbsp; &nbsp; LocalHelp:=True;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; tpQuery.Connection:=Conn;<br>&nbsp; &nbsp; tpQuery.CommandTimeout:=Conn.CommandTimeout;<br>&nbsp; &nbsp; with tpQuery do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; Sql.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp; if varisNull(keyValue) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if StrIsEmpty(TableName) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQL.Add('Select '+ReturnFeilds + ' Where '+Keyname)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQL.Add('Select '+ReturnFeilds + ' From '+TableName+' Where '+Keyname);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if StrIsEmpty(TableName) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQL.Add('Select '+ReturnFeilds + ' Where '+Keyname+'= :p1')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQL.Add('Select '+ReturnFeilds + ' From '+TableName+' Where '+Keyname+'= :p1');<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Parameters.Refresh;<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Parameters.ParamByName('p1').Value:=KeyValue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Parameters.ParamValues['p1']:=KeyValue;<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Parameters.Items[0].Value:=KeyValue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>// &nbsp; &nbsp; &nbsp; &nbsp;showMessage(sql.Text);<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; Except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=Null;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; &nbsp; if (RecordCount=0) and LocalHelp then //如果本地没查到,则到服务器上查<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Connection:=Conn;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=Null;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; First;<br>&nbsp; &nbsp; &nbsp; &nbsp; tpVal:=Null;<br>&nbsp; &nbsp; &nbsp; &nbsp; //showmessage(fields[0].Value);<br>&nbsp; &nbsp; &nbsp; &nbsp; if (FieldCount&gt;0) and not eof then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpVal:=VarArrayCreate([0,FieldCount-1],VarVariant);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i:=0 to Fields.Count-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tpVal:=Fields.value;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; VarCopy(Result,tpVal);<br>&nbsp; finally<br>&nbsp; &nbsp; tpQuery.Free;<br>&nbsp; end;<br>end;<br><br>给你两个函数,参考下,有些简单的自定义函数没写全,自已稍微做下修改。
 
在DELPHI中还是SQL Server的存储过程中??<br><br>假定一个表,ID(自动增长)、Name(字符串)。<br><br>在DELPHI中:<br>var<br>&nbsp; id:integer;<br>begin<br>&nbsp; adoquery1.close;<br>&nbsp; adoquery1.sql.text:='select * from 表';<br>&nbsp; adoquery1.open;<br>&nbsp; adoquery1.append;<br>&nbsp; adoquery1.fieldvalues['Name']:='123';<br>&nbsp; adoquery1.post;<br>&nbsp; id:=adoquery1.fieldbyname('id').asinteger;//取出ID的值。<br><br>如果在SQL Server的存储过程中:<br>&nbsp; insert into 表(Name) values('123')<br>&nbsp; 之后用一个系统变更@@INDE……这样的一个变量就可以取出这个自动增长的列的最新值,变量名我忘了,可以查一下。
 
后退
顶部