注册 登录
编程论坛 ASP技术论坛

从A表中读出数据写入B表

wwm123456789 发布于 2014-01-17 21:58, 973 次点击
A.B表分别位于不同数据库中,AB表名称结构字段完全一样,现在想将A表中的信息写入B表中,代码如下
<!-- #include file = "../common/site.inc" -->
<!-- #include file = "../common/site1.inc" -->
<%
 dim rs
 set rs=cn.execute ("select StudentID,password,teacher,xb,xy,zy,bj,nj,ph,sfz,bmsj from student")
 if not (rs.eof and rs.bof) then '如果账号存在
    do while not rs.eof   ’循环开始
    dim A_StudentID,A_password,A_xb,A_xy,A_zy,A_bj,A_nj,A_ph,A_sfz,A_bmsj
    A_StudentID=rs("StudentID")
     A_password=rs("password")
      A_xb=rs("xb")
       A_xy=rs("xy")
        A_zy=rs("zy")
         A_bj=rs("bj")
          A_nj=rs("nj")
           A_ph=rs("ph")
            A_sfz=rs("sfz")
             A_bmsj=rs("bmsj")
    set rs1=server.createobject("adodb.recordset")
     sql="select StudentID,password,teacher,xb,xy,zy,bj,nj,ph,sfz,bmsj from student where StudentID='"&A_StudentID&"'" '用A的账号作为查询条件在B表里查询;
    rs1.open sql1,cn1,1,3
            rs1("password")= A_password
            rs1("xb")= A_xb
            rs1("xy")= A_xy
            rs1("zy")= A_zy
            rs1("nj")= A_nj
            rs1("bj")= A_bj
            rs1("ph")= A_ph
            rs1("sfz")= A_sfz
            rs1("bmsj")= A_bmsj
        rs1.update
       rs1.close
     rs.movenext
 loop
 else
response.write ("A表没有数据")
end if
 rs.close
 set rs=nothing
 %>
提示出错,帮忙看看,谢谢、
6 回复
#2
yms1232014-01-20 14:32

如果两个表在同一个数据库内,而且除了名字结构完全一样,一句SQL语句就搞定了啊
dim rs
set rs=cn.execute ("select StudentID,password,teacher,xb,xy,zy,bj,nj,ph,sfz,bmsj from student")
if not (rs.eof and rs.bof) then '如果账号存在
   cn.execute("insert into B表 select * from A表")
else
response.write ("A表没有数据")
end if
rs.close
set rs=nothing
%>
#3
yms1232014-01-20 14:39
<%
'或者你一定要用程序来写入
dim rs,i
set rs=cn.execute ("select StudentID,password,teacher,xb,xy,zy,bj,nj,ph,sfz,bmsj from student")
if not (rs.eof and rs.bof) then '如果账号存在
   do while not rs.eof
      set rs1=server.createobject("adodb.recordset")
      rs1.open sql1,cn1,1,3
      rs1.addnew
      for i=0 To rs.fields.count-1
         rs1.Fields(i).Value=rs.Fields(i).Value   
      next
      rs1.update
      rs1.close
      rs.movenext
   loop
else
response.write ("A表没有数据")
end if
rs.close
set rs=nothing
%>
#4
wwm1234567892014-01-20 17:47
回复 3楼 yms123
我是同一网站下的不同数据库里的两个表,但这两个表的结构是相同的,连名字都相同,我是想让一个表与另一个表的某些字段的数据相同。
用你的代码运行,没有提示出错,但也没有更新成功,麻烦您再看看,谢谢。
#5
yms1232014-01-21 10:49
以下是引用wwm123456789在2014-1-20 17:47:48的发言:

我是同一网站下的不同数据库里的两个表,但这两个表的结构是相同的,连名字都相同,我是想让一个表与另一个表的某些字段的数据相同。
用你的代码运行,没有提示出错,但也没有更新成功,麻烦您再看看,谢谢。

你用的是3楼的代码也错吗?
#6
wwm1234567892014-01-21 16:06
谢谢,yms123大神,已经解决了,按3楼,是我自己中间没注意,写错了一个地方。
#7
lsmmh2017-07-03 15:37
新手学习了
1