%
'=============================================================================
' FILE: insertallprintermachines.asp
' PURPOSE: Insert ALL printer machines from printers table
' CREATED: 2025-10-20
'
' This will create machine records for all printers that don't already exist
'=============================================================================
Response.Write("
Insert All Printer Machines
")
Dim strSQL, executeInsert, totalQualifying, totalExisting, totalToInsert, rowCount
' Check if we should execute (look for ?execute=1 in URL)
executeInsert = (Request.QueryString("execute") = "1")
If Not executeInsert Then
Response.Write("")
Response.Write("
PREVIEW MODE
")
Response.Write("
This page is in PREVIEW mode. Review the printers below, then click the button to execute the INSERT.
")
Response.Write("
")
End If
' Check 1: How many printers qualify
Response.Write("Step 1: Qualifying Printers
")
strSQL = "SELECT COUNT(*) as cnt FROM printers p " &_
"INNER JOIN machines m ON p.machineid = m.machineid " &_
"WHERE p.isactive = 1 " &_
"AND m.isactive = 1 " &_
"AND m.mapleft IS NOT NULL " &_
"AND m.maptop IS NOT NULL"
Set rs = objConn.Execute(strSQL)
totalQualifying = rs("cnt")
Response.Write("Total printers that qualify: " & totalQualifying & "
")
rs.Close
' Check 2: How many already exist
Response.Write("Step 2: Already Exist
")
strSQL = "SELECT COUNT(*) as cnt FROM machines WHERE machinetypeid = 15"
Set rs = objConn.Execute(strSQL)
totalExisting = rs("cnt")
Response.Write("Printer machines already in database: " & totalExisting & "
")
rs.Close
' Check 3: How many will be inserted (printers that don't already have machine records)
Response.Write("Step 3: Will Be Inserted
")
strSQL = "SELECT COUNT(*) as cnt FROM printers p " &_
"INNER JOIN machines m ON p.machineid = m.machineid " &_
"WHERE p.isactive = 1 " &_
"AND m.isactive = 1 " &_
"AND m.mapleft IS NOT NULL " &_
"AND m.maptop IS NOT NULL " &_
"AND NOT EXISTS (" &_
" SELECT 1 FROM machines m2 " &_
" WHERE m2.machinenumber = CONCAT(m.machinenumber, '-PRINTER')" &_
")"
Set rs = objConn.Execute(strSQL)
totalToInsert = rs("cnt")
Response.Write("Printers that will be inserted: " & totalToInsert & "
")
rs.Close
' Show preview of what will be inserted
Response.Write("Step 4: Preview (First 20)
")
strSQL = "SELECT " &_
"p.printerid, " &_
"p.printercsfname, " &_
"p.printerwindowsname, " &_
"m.machinenumber, " &_
"m.alias, " &_
"m.mapleft, " &_
"m.maptop, " &_
"p.ipaddress, " &_
"CONCAT(m.machinenumber, '-PRINTER') AS new_machinenumber, " &_
"COALESCE(NULLIF(NULLIF(p.printercsfname, ''), 'NONE'), p.printerwindowsname, m.alias, m.machinenumber) AS new_alias " &_
"FROM printers p " &_
"INNER JOIN machines m ON p.machineid = m.machineid " &_
"WHERE p.isactive = 1 " &_
"AND m.isactive = 1 " &_
"AND m.mapleft IS NOT NULL " &_
"AND m.maptop IS NOT NULL " &_
"AND NOT EXISTS (" &_
" SELECT 1 FROM machines m2 " &_
" WHERE m2.machinenumber = CONCAT(m.machinenumber, '-PRINTER')" &_
") " &_
"ORDER BY m.machinenumber " &_
"LIMIT 20"
Set rs = objConn.Execute(strSQL)
Response.Write("")
Response.Write("")
Response.Write("| Printer ID | ")
Response.Write("CSF Name | ")
Response.Write("Windows Name | ")
Response.Write("Current Machine | ")
Response.Write("NEW Machine Number | ")
Response.Write("NEW Alias | ")
Response.Write("IP Address | ")
Response.Write("Map Coords | ")
Response.Write("
")
rowCount = 0
While Not rs.EOF
rowCount = rowCount + 1
Response.Write("")
Response.Write("| " & rs("printerid") & " | ")
Response.Write("" & Server.HTMLEncode(rs("printercsfname") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("printerwindowsname") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("machinenumber") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("new_machinenumber") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("new_alias") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("ipaddress") & "") & " | ")
Response.Write("" & rs("mapleft") & ", " & rs("maptop") & " | ")
Response.Write("
")
rs.MoveNext
Wend
Response.Write("
")
rs.Close
If rowCount = 0 Then
Response.Write("✓ All printers already have machine records! Nothing to insert.
")
Else
If CLng(totalToInsert) > 20 Then
Response.Write("Showing first 20 of " & totalToInsert & " total printers...
")
End If
End If
' Execute the INSERT if requested
If executeInsert = True And CLng(totalToInsert) > 0 Then
Response.Write("
")
Response.Write("EXECUTING INSERT...
")
On Error Resume Next
strSQL = "INSERT INTO machines (" &_
"machinenumber, alias, machinetypeid, mapleft, maptop, " &_
"isactive, businessunitid, modelnumberid, ipaddress1" &_
") " &_
"SELECT " &_
"CONCAT(m.machinenumber, '-PRINTER') AS machinenumber, " &_
"COALESCE(NULLIF(NULLIF(p.printercsfname, ''), 'NONE'), p.printerwindowsname, m.alias, m.machinenumber) AS alias, " &_
"15 AS machinetypeid, " &_
"m.mapleft, " &_
"m.maptop, " &_
"1 AS isactive, " &_
"m.businessunitid, " &_
"p.modelid AS modelnumberid, " &_
"p.ipaddress AS ipaddress1 " &_
"FROM printers p " &_
"INNER JOIN machines m ON p.machineid = m.machineid " &_
"WHERE p.isactive = 1 " &_
"AND m.isactive = 1 " &_
"AND m.mapleft IS NOT NULL " &_
"AND m.maptop IS NOT NULL " &_
"AND NOT EXISTS (" &_
" SELECT 1 FROM machines m2 " &_
" WHERE m2.machinenumber = CONCAT(m.machinenumber, '-PRINTER')" &_
")"
objConn.Execute(strSQL)
If Err.Number <> 0 Then
Response.Write("")
Response.Write("
ERROR!
")
Response.Write("
Error Number: " & Err.Number & "
")
Response.Write("
Error Description: " & Server.HTMLEncode(Err.Description) & "
")
Response.Write("
")
Err.Clear
Else
Response.Write("")
Response.Write("
SUCCESS!
")
Response.Write("
" & totalToInsert & " printer machine(s) were inserted successfully!
")
Response.Write("
")
' Show sample of what was inserted
Response.Write("Sample of Inserted Records:
")
strSQL = "SELECT m.machineid, m.machinenumber, m.alias, mt.machinetype, c.address as ipaddress, m.mapleft, m.maptop " &_
"FROM machines m " &_
"INNER JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid " &_
"LEFT JOIN communications c ON m.machineid = c.machineid AND c.comstypeid = 1 " &_
"WHERE m.machinetypeid = 15 " &_
"ORDER BY m.machineid DESC " &_
"LIMIT 10"
Set rs = objConn.Execute(strSQL)
Response.Write("")
Response.Write("")
Response.Write("| Machine ID | Machine Number | Alias | Type | IP | Map | ")
Response.Write("
")
While Not rs.EOF
Response.Write("")
Response.Write("| " & rs("machineid") & " | ")
Response.Write("" & Server.HTMLEncode(rs("machinenumber") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("alias") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("machinetype") & "") & " | ")
Response.Write("" & Server.HTMLEncode(rs("ipaddress") & "") & " | ")
Response.Write("" & rs("mapleft") & ", " & rs("maptop") & " | ")
Response.Write("
")
rs.MoveNext
Wend
Response.Write("
")
rs.Close
End If
On Error Goto 0
End If
' Show execute button if there are printers to insert and not executing
If executeInsert <> True And CLng(totalToInsert) > 0 Then
' Show execute button
Response.Write("
")
Response.Write("")
Response.Write("
Ready to Execute?
")
Response.Write("
This will insert " & totalToInsert & " printer machine(s) into the database.
")
Response.Write("
")
Response.Write("
")
End If
objConn.Close
%>