数据批量提交时,如何判断如果值已在数据库中存在则只更新数量否则将其添加到记录集中?
数据批量提交, <%%> 是批量写入数据库的代码
问题是,如果批量提交的数据中与数据库已存在的值相同,那么
//---------------加数量
Conn.execute( "update 库存信息表 SET 数量 = 数量+ "&cint(sl)& " where 木材种类= ' "&mczn& " ' and 木材规格长= "&cint(mcggc)& " ")
//---------------
同时,数值不同的则写入数据库中.
这个过程代码如何写!!!?
<%
'向数据库写入数据
dim recCnt,i
dim mczn,mcggc,mcggk,mcggh,mcpz,sl,mcjg,ckmc,gysmc,rkbh,rkrq,jsr,czr
recCnt=session( "recnum ") 'request.form( "recCnt ") '.count '取得共有多少条记录
'批量录入数据
for i=1 to recCnt
mczn=trim(request.form( "mczn ")(i))
mcggc=trim(request.form( "mcggc ")(i))
mcggk=trim(request.form( "mcggk ")(i))
mcggh=trim(request.form( "mcggh ")(i))
mcpz=trim(request.form( "mcpz ")(i))
sl=trim(request.form( "sl ")(i))
mcjg=trim(request.form( "mcjg ")(i))
ckmc=trim(request.form( "ckmc "))
gysmc=trim(request.form( "gysmc "))
rkbh=trim(request.form( "rkbh "))
rkrq=trim(request.form( "rkrq "))
jsr=trim(request.form( "jsr "))
czr=trim(request.form( "czr "))
sqlStr= "Insert Into 入库信息表(木材种类,木材规格长,木材规格宽,木材规格厚,木材品质,数量,木材价格,仓库名称,供应商名称,入库编号,入库日期,经手人,操作人) values( ' "
sqlStr=sqlStr & mczn & " ', ' "
sqlStr=sqlStr & mcggc & " ', ' "
sqlStr=sqlStr & mcggk & " ', ' "
sqlStr=sqlStr & mcggh & " ', ' "
sqlStr=sqlStr & mcpz & " ', ' "
sqlStr=sqlStr & sl & " ', ' "
sqlStr=sqlStr & mcjg & " ', ' "
sqlStr=sqlStr & ckmc & " ', ' "
sqlStr=sqlStr & gysmc & " ', ' "
sqlStr=sqlStr & rkbh & " ', ' "
sqlStr=sqlStr & rkrq & " ', ' "
sqlStr=sqlStr & jsr & " ', " _
& " ' " & czr & " ') "
Conn.execute(sqlStr)
next
response.write " <font color=#0000ff> 输入的 " & session( "recnum ") & " 组数据已成功写入数据库 </font> "
%>
------解决方案--------------------建议用这种方法:
'获取表单中数据,要设定一个主键,能唯一确定记录的。
sql= "select * from 入库信息表 where ..... "
set conn=server.createobject( "ADODB.Connection ")
conn.open ...
set rs=server.createobject( "ADODB.Recordset ")
rs.open sql,conn,1,3
if rs.eof then