Re: Move Records to Another Table

March 26, 2003 by Jared Cobb

I can offer a simple explaination, and as far as I know, it's the easiest way to accomplish this.  Be aware though, that this method will move the records one at a time, as you choose them.

Overall, we won't be "Moving" the records from table to table; we're going to Insert them into the new table and then delete them from the old table using a series of different steps.

Step 1) Build Your Page

Create a page which will build a Recordset containing only the record you wish to move.  In this example, the page will receive the Unique ID of the record via the Request.QueryString Variable.  We'll say the SQL for this Recordset looks like this in the DMX Recordset (Advanced View):

SELECT *
FROM tblOLD_TABLE
WHERE RecordID = varRecordID

Variables Section
Name: varRecordID
Default Value: 0
Run-time Value: Request.QueryString("RecordID")

Realize that your own recordset can be whatever you want, as long as it's going to pull only the record you wish to move.  My SQL code above will take only the record whose "Record ID" matches what was passed from the previous page via the GET method.

Step 2) Custom Code

Now for the tricky part; if you can get this part right, you're home free.  We're going to create some Commands (sometimes called Stored Procedures) that will execute ONLY when we want them to.  Here's how:

Your page will initially load with ONLY the Recordset.  On this same page we're going to create a form with a hidden field called FORM_SUBMIT, and a Submit button.  Point the form (Action) to itself (the page we're working on) and use the Post method.  Set the FORM_SUBMIT field equal to the RecordID in your recordset.  In my example this value would be <%=(Recordset.Fields.Item("RecordID").Value)%>

Now copy the following code into your page AFTER your Recordset and BEFORE the <HTML> tag:

<%
' If the form is submitted, process this code
If Request.Form("FORM_SUBMIT") <> "" Then

 ' Assign values to our variables to be inserted in the new table
 cmdINSERT__varRecordID = (Recordset.Fields.Item("RecordID").Value)
 cmdINSERT__varOtherField01 = (Recordset.Fields.Item("OtherField01").Value)
 cmdINSERT__varOtherField02 = (Recordset.Fields.Item("OtherField02").Value)

 ' Create a command to Insert the record into the new table form the old table
 Set cmdINSERT = Server.CreateObject("ADODB.Command")
 ' Be sure to change "MM_Connection_STRING" to your actual Connection String
 cmdINSERT.ActiveConnection = MM_Connection_STRING
 ' Be sure to change your table name and field names to match your case
 cmdINSERT.CommandText = "INSERT INTO tblTABLE_NEW (RecordID,OtherField01,OtherField02)  VALUES ('" + Replace(cmdINSERT__varRecordID, "'", "''") + "','" + Replace(cmdINSERT__varOtherField01, "'", "''") + "','" + Replace(cmdINSERT__varOtherField02, "'", "''") + "') "
 cmdINSERT.CommandType = 1
 cmdINSERT.CommandTimeout = 0
 cmdINSERT.Prepared = false
 cmdINSERT.Execute()
 cmdINSERT.ActiveConnection.Close
 
 ' Assign a value to the variable to delete the old record
 cmdDELETE__varRecordID = Request.Form("FORM_SUBMIT")

 ' Create a command to delete the old record
 Set cmdDELETE = Server.CreateObject("ADODB.Command")
 ' Be sure to change "MM_Connection_STRING" to your actual Connection String
 cmdDELETE.ActiveConnection = MM_Connection_STRING
 ' Be sure to change your table name and field name to match your case
 cmdDELETE.CommandText = "DELETE FROM tblTABLE_OLD  WHERE RecordID = '" + Replace(cmdDELETE__varRecordID, "'", "''") + "'"
 cmdDELETE.CommandType = 1
 cmdDELETE.CommandTimeout = 0
 cmdDELETE.Prepared = True
 cmdDELETE.Execute()
 cmdDELETE.ActiveConnection.Close
 
 ' Once the action is complete, send the user to another page
 Response.Redirect("complete.asp")

End If
%>

This simple graphic illustrates how this page will work:

The Recordset is processed first.  Only when the form is submitted do the Insert and Delete functions get processed.  After that, the user is redirected to a new page (in my case, "complete.asp"

Step 3) Clean Up The Code

My code is customized to my own example.  You will need to go into the code and change the names of anything specific to my example.  Here's a list of what needs to be changed:

1) tblTABLE_OLD - This is your old table
2) tblTABLE_NEW - This is your new table
3) RecordID - This is your unique Record ID of the record being moved
4) MM_Connection_STRING - This is your connection string to your database. It's automatically created when you build your Recordset. Just copy that one.
5) complete.asp - This is the webpage that your user is redirected to after the form is submitted
6) Recordset - If you happen to name your Recordset something else, be sure to change any reference to "Recordset" to your actual Recordset name

And probably the most complicated one(s)...
7) OtherField01, OtherField02, etc. - These are each and every field name of your record that need to be inserted into your new table.  If you leave ANY of these out when you try to run this code, you will lose the data in the fields left out.  You will most likely be needing to ADD more fields than what are listed in my example.  Just be sure you DEFINE then and give them values, and be sure they are listed in the cmdINSERT.CommandText SQL code.

Good luck, and happy coding!

j

RE: Re: Move Records to Another Table

December 4, 2005 by Tom McCarthy
Hi Jared This script saved me a lot of time, Thank You. I only seem to be able to insert the first record though regardless of the value of end_user ID. I've included the code I use, can you help me out, I'm kind of a newbee at ASP. Thanks Tom McCarthy illbe@mac.com <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> <% Dim rs_sales Dim rs_sales_numRows Set rs_sales = Server.CreateObject("ADODB.Recordset") rs_sales.ActiveConnection = MM_conn_products_STRING rs_sales.Source = "SELECT * FROM tbl_end_user" rs_sales.CursorType = 0 rs_sales.CursorLocation = 2 rs_sales.LockType = 1 rs_sales.Open() rs_sales_numRows = 0 %> <% ' *** Recordset Stats, Move To Record, and Go To Record: declare stats variables Dim rs_sales_total Dim rs_sales_first Dim rs_sales_last ' set the record count rs_sales_total = rs_sales.RecordCount ' set the number of rows displayed on this page If (rs_sales_numRows < 0) Then rs_sales_numRows = rs_sales_total Elseif (rs_sales_numRows = 0) Then rs_sales_numRows = 1 End If ' set the first and last displayed record rs_sales_first = 1 rs_sales_last = rs_sales_first + rs_sales_numRows - 1 ' if we have the correct record count, check the other stats If (rs_sales_total <> -1) Then If (rs_sales_first > rs_sales_total) Then rs_sales_first = rs_sales_total End If If (rs_sales_last > rs_sales_total) Then rs_sales_last = rs_sales_total End If If (rs_sales_numRows > rs_sales_total) Then rs_sales_numRows = rs_sales_total End If End If %> <% Dim MM_paramName %> <% ' *** Move To Record and Go To Record: declare variables Dim MM_rs Dim MM_rsCount Dim MM_size Dim MM_uniqueCol Dim MM_offset Dim MM_atTotal Dim MM_paramIsDefined Dim MM_param Dim MM_index Set MM_rs = rs_sales MM_rsCount = rs_sales_total MM_size = rs_sales_numRows MM_uniqueCol = "" MM_paramName = "" MM_offset = 0 MM_atTotal = false MM_paramIsDefined = false If (MM_paramName <> "") Then MM_paramIsDefined = (Request.QueryString(MM_paramName) <> "") End If %> <% ' *** Move To Record: handle 'index' or 'offset' parameter if (Not MM_paramIsDefined And MM_rsCount <> 0) then ' use index parameter if defined, otherwise use offset parameter MM_param = Request.QueryString("index") If (MM_param = "") Then MM_param = Request.QueryString("offset") End If If (MM_param <> "") Then MM_offset = Int(MM_param) End If ' if we have a record count, check if we are past the end of the recordset If (MM_rsCount <> -1) Then If (MM_offset >= MM_rsCount Or MM_offset = -1) Then ' past end or move last If ((MM_rsCount Mod MM_size) > 0) Then ' last page not a full repeat region MM_offset = MM_rsCount - (MM_rsCount Mod MM_size) Else MM_offset = MM_rsCount - MM_size End If End If End If ' move the cursor to the selected record MM_index = 0 While ((Not MM_rs.EOF) And (MM_index < MM_offset Or MM_offset = -1)) MM_rs.MoveNext MM_index = MM_index + 1 Wend If (MM_rs.EOF) Then MM_offset = MM_index ' set MM_offset to the last possible record End If End If %> <% ' *** Move To Record: if we dont know the record count, check the display range If (MM_rsCount = -1) Then ' walk to the end of the display range for this page MM_index = MM_offset While (Not MM_rs.EOF And (MM_size < 0 Or MM_index < MM_offset + MM_size)) MM_rs.MoveNext MM_index = MM_index + 1 Wend ' if we walked off the end of the recordset, set MM_rsCount and MM_size If (MM_rs.EOF) Then MM_rsCount = MM_index If (MM_size < 0 Or MM_size > MM_rsCount) Then MM_size = MM_rsCount End If End If ' if we walked off the end, set the offset based on page size If (MM_rs.EOF And Not MM_paramIsDefined) Then If (MM_offset > MM_rsCount - MM_size Or MM_offset = -1) Then If ((MM_rsCount Mod MM_size) > 0) Then MM_offset = MM_rsCount - (MM_rsCount Mod MM_size) Else MM_offset = MM_rsCount - MM_size End If End If End If ' reset the cursor to the beginning If (MM_rs.CursorType > 0) Then MM_rs.MoveFirst Else MM_rs.Requery End If ' move the cursor to the selected record MM_index = 0 While (Not MM_rs.EOF And MM_index < MM_offset) MM_rs.MoveNext MM_index = MM_index + 1 Wend End If %> <% ' *** Move To Record: update recordset stats ' set the first and last displayed record rs_sales_first = MM_offset + 1 rs_sales_last = MM_offset + MM_size If (MM_rsCount <> -1) Then If (rs_sales_first > MM_rsCount) Then rs_sales_first = MM_rsCount End If If (rs_sales_last > MM_rsCount) Then rs_sales_last = MM_rsCount End If End If ' set the boolean used by hide region to check if we are on the last record MM_atTotal = (MM_rsCount <> -1 And MM_offset + MM_size >= MM_rsCount) %> <% ' *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters Dim MM_keepNone Dim MM_keepURL Dim MM_keepForm Dim MM_keepBoth Dim MM_removeList Dim MM_item Dim MM_nextItem ' create the list of parameters which should not be maintained MM_removeList = "&index=" If (MM_paramName <> "") Then MM_removeList = MM_removeList & "&" & MM_paramName & "=" End If MM_keepURL="" MM_keepForm="" MM_keepBoth="" MM_keepNone="" ' add the URL parameters to the MM_keepURL string For Each MM_item In Request.QueryString MM_nextItem = "&" & MM_item & "=" If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then MM_keepURL = MM_keepURL & MM_nextItem & Server.URLencode(Request.QueryString(MM_item)) End If Next ' add the Form variables to the MM_keepForm string For Each MM_item In Request.Form MM_nextItem = "&" & MM_item & "=" If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then MM_keepForm = MM_keepForm & MM_nextItem & Server.URLencode(Request.Form(MM_item)) End If Next ' create the Form + URL string and remove the intial '&' from each of the strings MM_keepBoth = MM_keepURL & MM_keepForm If (MM_keepBoth <> "") Then MM_keepBoth = Right(MM_keepBoth, Len(MM_keepBoth) - 1) End If If (MM_keepURL <> "") Then MM_keepURL = Right(MM_keepURL, Len(MM_keepURL) - 1) End If If (MM_keepForm <> "") Then MM_keepForm = Right(MM_keepForm, Len(MM_keepForm) - 1) End If ' a utility function used for adding additional parameters to these strings Function MM_joinChar(firstItem) If (firstItem <> "") Then MM_joinChar = "&" Else MM_joinChar = "" End If End Function %> <% ' *** Move To Record: set the strings for the first, last, next, and previous links Dim MM_keepMove Dim MM_moveParam Dim MM_moveFirst Dim MM_moveLast Dim MM_moveNext Dim MM_movePrev Dim MM_urlStr Dim MM_paramList Dim MM_paramIndex Dim MM_nextParam MM_keepMove = MM_keepBoth MM_moveParam = "index" ' if the page has a repeated region, remove 'offset' from the maintained parameters If (MM_size > 1) Then MM_moveParam = "offset" If (MM_keepMove <> "") Then MM_paramList = Split(MM_keepMove, "&") MM_keepMove = "" For MM_paramIndex = 0 To UBound(MM_paramList) MM_nextParam = Left(MM_paramList(MM_paramIndex), InStr(MM_paramList(MM_paramIndex),"=") - 1) If (StrComp(MM_nextParam,MM_moveParam,1) <> 0) Then MM_keepMove = MM_keepMove & "&" & MM_paramList(MM_paramIndex) End If Next If (MM_keepMove <> "") Then MM_keepMove = Right(MM_keepMove, Len(MM_keepMove) - 1) End If End If End If ' set the strings for the move to links If (MM_keepMove <> "") Then MM_keepMove = Server.HTMLEncode(MM_keepMove) & "&" End If MM_urlStr = Request.ServerVariables("URL") & "?" & MM_keepMove & MM_moveParam & "=" MM_moveFirst = MM_urlStr & "0" MM_moveLast = MM_urlStr & "-1" MM_moveNext = MM_urlStr & CStr(MM_offset + MM_size) If (MM_offset - MM_size < 0) Then MM_movePrev = MM_urlStr & "0" Else MM_movePrev = MM_urlStr & CStr(MM_offset - MM_size) End If %> <%Response.Write(rs_sales.Fields.Item("end_user_ID").Value)%> <% ' If the form is submitted, process this code If Request.Form("FORM_SUBMIT") <> "" Then ' Assign values to our variables to be inserted in the new table cmdINSERT__varend_user_ID = (rs_sales.Fields.Item("end_user_ID").Value) cmdINSERT__varOrderDate = (rs_sales.Fields.Item("OrderDate").Value) cmdINSERT__varfirst_name = (rs_sales.Fields.Item("first_name").Value) cmdINSERT__varlast_name = (rs_sales.Fields.Item("last_name").Value) cmdINSERT__vare_mail = (rs_sales.Fields.Item("e_mail").Value) cmdINSERT__varaddress_1 = (rs_sales.Fields.Item("address_1").Value) cmdINSERT__varaddress_2 = (rs_sales.Fields.Item("address_2").Value) cmdINSERT__varcity = (rs_sales.Fields.Item("city").Value) cmdINSERT__varstate = (rs_sales.Fields.Item("state").Value) cmdINSERT__varzip = (rs_sales.Fields.Item("zip").Value) cmdINSERT__varcountry = (rs_sales.Fields.Item("country").Value) cmdINSERT__varphone = (rs_sales.Fields.Item("phone").Value) cmdINSERT__varalt_phone = (rs_sales.Fields.Item("alt_phone").Value) cmdINSERT__varfax = (rs_sales.Fields.Item("fax").Value) cmdINSERT__varair_make = (rs_sales.Fields.Item("air_make").Value) cmdINSERT__varair_model = (rs_sales.Fields.Item("air_model").Value) cmdINSERT__varair_serial = (rs_sales.Fields.Item("air_serial").Value) cmdINSERT__varair_n = (rs_sales.Fields.Item("air_n").Value) cmdINSERT__varengine_make = (rs_sales.Fields.Item("engine_make").Value) cmdINSERT__varengine_model = (rs_sales.Fields.Item("engine_model").Value) cmdINSERT__vartotal_time = (rs_sales.Fields.Item("total_time").Value) cmdINSERT__vareng_serial = (rs_sales.Fields.Item("eng_serial").Value) cmdINSERT__vareng_needed = (rs_sales.Fields.Item("eng_needed").Value) cmdINSERT__varship_company = (rs_sales.Fields.Item("ship_company").Value) cmdINSERT__varship_address_1 = (rs_sales.Fields.Item("ship_address_1").Value) cmdINSERT__varship_address_2 = (rs_sales.Fields.Item("ship_address_2").Value) cmdINSERT__varship_city = (rs_sales.Fields.Item("ship_city").Value) cmdINSERT__varship_state = (rs_sales.Fields.Item("ship_state").Value) cmdINSERT__varship_zip = (rs_sales.Fields.Item("ship_zip").Value) cmdINSERT__varship_country = (rs_sales.Fields.Item("ship_country").Value) cmdINSERT__varship_phone = (rs_sales.Fields.Item("ship_phone").Value) cmdINSERT__varship_alt_phone = (rs_sales.Fields.Item("ship_alt_phone").Value) cmdINSERT__varship_fax = (rs_sales.Fields.Item("ship_fax").Value) cmdINSERT__varcomments = (rs_sales.Fields.Item("comments").Value) cmdINSERT__varhours_flown = (rs_sales.Fields.Item("hours_flown").Value) cmdINSERT__varlearned_pya = (rs_sales.Fields.Item("learned_pya").Value) cmdINSERT__varpublications = (rs_sales.Fields.Item("publications").Value) ' Create a command to Insert the record into the new table form the old table Set cmdINSERT = Server.CreateObject("ADODB.Command") ' Be sure to change "MM_Connection_STRING" to your actual Connection String cmdINSERT.ActiveConnection = MM_conn_products_STRING ' Be sure to change your table name and field names to match your case cmdINSERT.CommandText = "INSERT INTO sales (end_user_ID,OrderDate,first_name,last_name,e_mail,address_1,address_2,city,state,zip,country,phone,alt_phone,fax,air_make,air_model,air_serial,air_n,engine_make,engine_model,total_time,eng_serial,eng_needed,ship_company,ship_address_1,ship_address_2,ship_city,ship_state,ship_zip,ship_country,ship_phone,ship_alt_phone,ship_fax,comments,hours_flown,learned_pya,publications) VALUES ('" + Replace(cmdINSERT__varend_user_ID, "'", "''") + "','" + Replace(cmdINSERT__varOrderDate, "'", "''") + "','" + Replace(cmdINSERT__varfirst_name, "'", "''") + "','" + Replace(cmdINSERT__varlast_name, "'", "''")+ "','" + Replace(cmdINSERT__vare_mail, "'", "''")+ "','" + Replace(cmdINSERT__varaddress_1, "'", "''")+ "','" + Replace(cmdINSERT__varaddress_2, "'", "''")+ "','" + Replace(cmdINSERT__varcity, "'", "''")+ "','" + Replace(cmdINSERT__varstate, "'", "''")+ "','" + Replace(cmdINSERT__varzip, "'", "''")+ "','" + Replace(cmdINSERT__varcountry, "'", "''")+ "','" + Replace(cmdINSERT__varphone, "'", "''")+ "','" + Replace(cmdINSERT__varalt_phone, "'", "''")+ "','" + Replace(cmdINSERT__varfax, "'", "''")+ "','" + Replace(cmdINSERT__varair_make, "'", "''")+ "','" + Replace(cmdINSERT__varair_model, "'", "''")+ "','" + Replace(cmdINSERT__varair_serial, "'", "''")+ "','" + Replace(cmdINSERT__varair_n, "'", "''")+ "','" + Replace(cmdINSERT__varengine_make, "'", "''")+ "','" + Replace(cmdINSERT__varengine_model, "'", "''")+ "','" + Replace(cmdINSERT__vartotal_time, "'", "''")+ "','" + Replace(cmdINSERT__vareng_serial, "'", "''")+ "','" + Replace(cmdINSERT__vareng_needed, "'", "''")+ "','" + Replace(cmdINSERT__varship_company, "'", "''")+ "','" + Replace(cmdINSERT__varship_address_1, "'", "''")+ "','" + Replace(cmdINSERT__varship_address_2, "'", "''")+ "','" + Replace(cmdINSERT__varship_city, "'", "''")+ "','" + Replace(cmdINSERT__varship_state, "'", "''")+ "','" + Replace(cmdINSERT__varship_zip, "'", "''")+ "','" + Replace(cmdINSERT__varship_country, "'", "''")+ "','" + Replace(cmdINSERT__varship_phone, "'", "''")+ "','" + Replace(cmdINSERT__varship_alt_phone, "'", "''")+ "','" + Replace(cmdINSERT__varship_fax, "'", "''")+ "','" + Replace(cmdINSERT__varcomments, "'", "''")+ "','" + Replace(cmdINSERT__varhours_flown, "'", "''")+ "','" + Replace(cmdINSERT__varlearned_pya, "'", "''")+ "','" + Replace(cmdINSERT__varpublications, "'", "''")+ "') " cmdINSERT.CommandType = 1 cmdINSERT.CommandTimeout = 0 cmdINSERT.Prepared = false cmdINSERT.Execute() cmdINSERT.ActiveConnection.Close ' Assign a value to the variable to delete the old record cmdDELETE__varend_user_ID = Request.Form("FORM_SUBMIT") ' Create a command to delete the old record Set cmdDELETE = Server.CreateObject("ADODB.Command") ' Be sure to change "MM_Connection_STRING" to your actual Connection String cmdDELETE.ActiveConnection = MM_conn_products_STRING ' Be sure to change your table name and field name to match your case cmdDELETE.CommandText = "DELETE FROM tbl_end_user WHERE end_user_ID = " + Replace(cmdDELETE__varend_user_ID, "", "") + "" cmdDELETE.CommandType = 1 cmdDELETE.CommandTimeout = 0 cmdDELETE.Prepared = True cmdDELETE.Execute() cmdDELETE.ActiveConnection.Close ' Once the action is complete, send the user to another page Response.Redirect("switchboard.asp") End If %> Untitled Document

">

<%=(rs_sales.Fields.Item("end_user_ID").Value)%>

<%=(rs_sales.Fields.Item("first_name").Value)%>

<%=(rs_sales.Fields.Item("last_name").Value)%>

Next Previous

<% rs_sales.Close() Set rs_sales = Nothing %>