Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Insert into database e ciclo do:

    Salve,
    devo ricavare più campi di uno stesso record di una tabella access, e metterli in un'altra tabella access accodati l'uno all'altro, ma il seguente codice asp non funziona:
    <%
    Set OBJdbConnection = Server.CreateObject("ADODB.Connection")
    OBJdbConnection.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("prenotazioni.mdb")
    sql = "SELECT * FROM tblUsers"
    Set RS=OBJdbConnection.Execute(sql)
    IDUser=rs("IDUser")
    UserID=rs("UserID")
    Password=rs("Password")
    permission=rs("permission")
    Insegnamento=rs("Insegnamento")
    Docente=rs("Docente")
    Anno=rs("Anno")
    Telefono=rs("Telefono")
    Email=rs("Email")
    NID=rs("NID")
    Insegnamento2=rs("Insegnamento2")
    Insegnamento3=rs("Insegnamento3")
    Insegnamento4=rs("Insegnamento4")
    Insegnamento5=rs("Insegnamento5")
    Insegnamento6=rs("Insegnamento6")
    Insegnamento7=rs("Insegnamento7")
    Insegnamento8=rs("Insegnamento8")
    Insegnamento9=rs("Insegnamento9")
    Insegnamento10=rs("Insegnamento10")%>
    <%Do while NOT RS.EOF
    i=i+1%>
    <%if Insegnamento <> "-" then%>
    <%
    sql1="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento&"')"
    Set RS1=OBJdbConnection.Execute(sql1)
    %>
    <%end if%>
    <%if Insegnamento2 <> "-" then%>
    <%
    sql2="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento2&"')"
    Set RS2=OBJdbConnection.Execute(sql2)
    %>
    <%end if%>
    <%if Insegnamento3 <> "-" then%>
    <%
    sql3="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento3&"')"
    Set RS3=OBJdbConnection.Execute(sql3)
    %>
    <%end if%>
    <%if Insegnamento4 <> "-" then%>
    <%
    sql4="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento4&"')"
    Set RS4=OBJdbConnection.Execute(sql4)
    %>
    <%end if%>
    <%if Insegnamento5 <> "-" then%>
    <%
    sql5="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento5&"')"
    Set RS5=OBJdbConnection.Execute(sql5)
    %>
    <%end if%>
    <%if Insegnamento6 <> "-" then%>
    <%
    sql6="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento6&"')"
    Set RS6=OBJdbConnection.Execute(sql6)
    %>
    <%end if%>
    <%if Insegnamento7 <> "-" then%>
    <%
    sql7="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento7&"')"
    Set RS7=OBJdbConnection.Execute(sql7)
    %>
    <%end if%>
    <%if Insegnamento8 <> "-" then%>
    <%
    sql8="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento8&"')"
    Set RS8=OBJdbConnection.Execute(sql8)
    %>
    <%end if%>
    <%if Insegnamento9 <> "-" then%>
    <%
    sql9="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento9&"')"
    Set RS9=OBJdbConnection.Execute(sql9)
    %>
    <%end if%>
    <%if Insegnamento10 <> "-" then%>
    <%
    sql10="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento10&"')"
    Set RS10=OBJdbConnection.Execute(sql10)
    %>
    <%end if%>
    <%
    RS.MoveNext
    Loop
    RS.Close
    OBJdbConnection.Close%>

    praticamente inserisce solo Insegnamento n volte nel campo Moduli.

    Cìè un'altro modo per leggere dei dati da un database e inserirli di nuovo in un'altra tabella?
    Grazie.

  2. #2
    Moderatore di ASP e MS Server L'avatar di Roby_72
    Registrato dal
    Aug 2001
    Messaggi
    19,559
    Esiste la pluripremiata INSERT INTO FROM SELECT... devi adattarla al tuo caso.

    Altrimenti anche la tua dovrebbe funzionare.
    Ma non so che errore ti da...

    Roby

  3. #3
    la INSERT INTO SELECT forse non fa al caso di subysuby, visto che a fronte di UN record ne deve inserire N. conviene innanzitutto farsi stampare i valori, e controllare il contenuto dei campi. farei poi una bella sub, per non appesantire il codice.

  4. #4
    • La prossima volta usa il tag [ CODE ] per inserire il codice.
    • I campi li devi leggere all'interno del ciclo
    • è inutile creare tutti quei recorset
    • è inutile creare tante variabili sql diverse
    • non aprire echiudere i tag asp (<% %>) in continuazione se è tutto codice asp


    Prova a vedere se così va. Poi al massimo vediamo di capire esattamente cosa vuoi fare

    codice:
    <%
    Set OBJdbConnection = Server.CreateObject("ADODB.Connection")
    OBJdbConnection.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("prenotazioni.mdb")
    sql = "SELECT * FROM tblUsers"
    Set RS=OBJdbConnection.Execute(sql)
    
    Do while NOT RS.EOF
    
    	IDUser=rs("IDUser")
    	UserID=rs("UserID")
    	Password=rs("Password")
    	permission=rs("permission")
    	Insegnamento=rs("Insegnamento")
    	Docente=rs("Docente")
    	Anno=rs("Anno")
    	Telefono=rs("Telefono")
    	Email=rs("Email")
    	NID=rs("NID")
    	Insegnamento2=rs("Insegnamento2")
    	Insegnamento3=rs("Insegnamento3")
    	Insegnamento4=rs("Insegnamento4")
    	Insegnamento5=rs("Insegnamento5")
    	Insegnamento6=rs("Insegnamento6")
    	Insegnamento7=rs("Insegnamento7")
    	Insegnamento8=rs("Insegnamento8")
    	Insegnamento9=rs("Insegnamento9")
    	Insegnamento10=rs("Insegnamento10")
    
    
    	i=i+1
    	if Insegnamento <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento2 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento2&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento3 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento3&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento4 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento4&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento5 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento5&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento6 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento6&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento7 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento7&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento8 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento8&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento9 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento9&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	if Insegnamento10 <> "-" then
    		sql="INSERT INTO tblModuli (Moduli) VALUES('"&Insegnamento10&"')"
    		OBJdbConnection.Execute(sql)
    	end if
    
    	RS.MoveNext
    Loop
    RS.Close
    OBJdbConnection.Close
    %>
    ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.