Search code examples
sql-serverasp-classic

How to update multiple records in SQL using Classic ASP


I'm trying to update a two records with the same social in Classic ASP but I'm not sure how. This is what I have... but it don't update both records.

    set rsTblEmpl=Server.CreateObject("ADODB.Recordset")
    if 1=1 then
        sql = "select * from tblEmpl where ssn=" & strSsn & ";"
    else
        sql = "select * from tblEmpl where eight_id=" & intEight_id & ";"
    end if 
    rsTblEmpl.open sql, conn, 2, 3
    if not rsTblEmpl.eof then
        rsTblEmpl("posid")   = StrPosId
        rsTblEmpl("posname") = StrPosName
        rsTblEmpl.update
    end if
    rsTblEmpl.close
    set rsTblEmpl=nothing

Solution

  • The reason why it's only updating 1 value is because you're not looping over all records. You're only going to be updating the first record that it finds. So if there are multiple records with the same SSN you will have to change the if statement to a loop.

    do until rsTblEmpl.eof
        rsTblEmpl("posid")   = StrPosId
        rsTblEmpl("posname") = StrPosName
        rsTblEmpl.update
        rsTblEmpl.movenext
    loop
    

    Or you can do them all in one go using a parameterized UPDATE statement:

    UPDATE tblEmpl SET posid = ?, posname = ? WHERE ssn = ?
    

    The if 1=1 at the top I'll assume is a placeholder, because now it will always be true, so the else is not needed.