数据插入的问题

深圳还是一样的深圳 2009-09-16 02:17:27
'主要是把表shop_product里面四个字段bookname,huiyuanjia,isbn,bookchuban记录插入到表Purchaseprice里面对应的四个字段,条件是:当shop_product里面的bookname记录在表Purchaseprice里面PurchaseName字段存在时不用再插入,不能重复插入!
<%

if request("action")="save" then

set rs=server.CreateObject("adodb.recordset")
sql="select * from shop_product "
rs.open sql,conn,1,1
do while not rs.eof
bookname=rs("bookname")
huiyuanjia=rs("huiyuanjia")
isbn=rs("isbn")
bookchuban=rs("bookchuban")
%>

<%
set rs2=server.CreateObject("adodb.recordset")
rs2.open "select count(*) into nCount from Purchaseprice where PurchaseName="&bookname&" ",conn,1,3

'主要是把表shop_product里面四个字段bookname,huiyuanjia,isbn,bookchuban记录插入到表Purchaseprice里面对应的四个字段,条件是:当shop_product里面的bookname记录在表Purchaseprice里面PurchaseName字段存在时不用再插入,不能重复插入!

if ncount>0 then

else

rs2.addnew
rs2("PurchaseName")=bookname
rs2("PurchaseRetail")=huiyuanjia
rs2("PurchaseSpecifications")=isbn
rs2("PurchaseUnits")=bookchuban
rs2.update

end if



rs2.close
set rs2=nothing

%>



<%
rs.movenext
loop
rs.close
set rs=nothing

response.write "<script language=javascript>alert('导入完成!');window.location.href='protaoru.asp';</script>"
response.End

end if
%>
...全文
57 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
凡夫与俗子 2009-09-16
  • 打赏
  • 举报
回复
<%

'如果是表单提交过来,则先获取要更新的数据
action =Trim(request("action"))
bookname =Trim(request.Form("bookname"))
huiyuanjia =Trim(request.Form("huiyuanjia"))
isbn =Trim(request.Form("isbn"))
bookchuban =Trim(request.Form("bookchuban"))
'action=save则开始下面操作
if request("action")="save" then
'判断这四项是否为空,为空则返回填写完整。可按需求来是否必填
if bookname="" or huiyuanjia="" or isbn="" or bookchuban="" then
response.Write "<script>alert('四项不能为空,请填写完整!');history.go(-1);</"&"script>"
response.End()
end if

'判断表Purchaseprice里面PurchaseName字段是否已存在,不能重复插入!
if conn.execute("select count(*) from Purchaseprice where PurchaseName='"&bookname&"'")(0)>0 then
response.Write "<script>alert('该数据已存在,不需重复添加!');history.go(-1);</"&"script>"
response.End()
end if

'如果满足了上述要求,则开始添加数据
set rs=server.CreateObject("adodb.recordset")
sql="select top 1 * from Purchaseprice" '查询第一条通过if not (rs.bof or rs.eof) then 判断是否可以存在,不存在则在这个位置添加新的记录,top 1 * 主要是为了省资源
rs.open sql,conn,1,3
if not (rs.bof or rs.eof) then
rs.addnew '开始添加
rs("PurchaseName") =bookname
rs("PurchaseRetail") =huiyuanjia
rs("PurchaseSpecifications")=isbn
rs("PurchaseUnits") =bookchuban
rs.update '更新记录
end if
response.Write "添加成功!"
end if

rs.close
set rs=nothing

%>
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xxwood 的回复:]
不是4个表吗?咋又成字段了。。。上个帖子我不是给出sql了吗。。。

insert into Purchaseprice (PurchaseName,huiyuanjia,isbn,bookchuban) (select bookname,huiyuanjia,isbn,bookchuban from shop_product where bookname not in (select PurchaseName from Purchaseprice))
[/Quote]

你能加我QQ吗:410834255
  • 打赏
  • 举报
回复
xxwood
你能加我QQ吗:410834255
zzhqiao 2009-09-16
  • 打赏
  • 举报
回复
问题在那里?
xxwood 2009-09-16
  • 打赏
  • 举报
回复
不是4个表吗?咋又成字段了。。。上个帖子我不是给出sql了吗。。。

insert into Purchaseprice (PurchaseName,huiyuanjia,isbn,bookchuban) (select bookname,huiyuanjia,isbn,bookchuban from shop_product where bookname not in (select PurchaseName from Purchaseprice))

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧