Consolidated ASP Classic MVC framework from best components
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

91 行
2.8KB

  1. ' migrate_isbusiness_to_households.vbs
  2. ' Moves IsBusiness from HouseholderNames to Households.
  3. '
  4. ' Usage:
  5. ' cscript //nologo scripts\migrate_isbusiness_to_households.vbs "C:\path\to\myAccessFile.accdb"
  6. '
  7. ' What it does:
  8. ' 1) Adds Households.IsBusiness (SMALLINT) if missing
  9. ' 2) Copies data: sets Households.IsBusiness=1 if any related HouseholderNames.IsBusiness<>0
  10. ' 3) Sets NULLs to 0
  11. ' 4) Drops HouseholderNames.IsBusiness if present
  12. '
  13. Option Explicit
  14. Dim dbPath
  15. If WScript.Arguments.Count < 1 Then
  16. WScript.Echo "ERROR: missing db path."
  17. WScript.Echo "Usage: cscript //nologo scripts\migrate_isbusiness_to_households.vbs ""C:\path\to\db.accdb"""
  18. WScript.Quit 1
  19. End If
  20. dbPath = WScript.Arguments(0)
  21. Dim conn
  22. Set conn = CreateObject("ADODB.Connection")
  23. conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath & ";Persist Security Info=False;"
  24. On Error Resume Next
  25. If Not ColumnExists(conn, "Households", "IsBusiness") Then
  26. Exec conn, "ALTER TABLE [Households] ADD COLUMN [IsBusiness] SMALLINT"
  27. If Err.Number <> 0 Then
  28. WScript.Echo "ERROR adding Households.IsBusiness: " & Err.Description
  29. WScript.Quit 1
  30. End If
  31. WScript.Echo "Added Households.IsBusiness"
  32. Else
  33. WScript.Echo "Households.IsBusiness already exists"
  34. End If
  35. ' Copy data (only if the old column exists)
  36. If ColumnExists(conn, "HouseholderNames", "IsBusiness") Then
  37. ' Normalize all existing households first so the column is never left NULL.
  38. Exec conn, "UPDATE [Households] SET [IsBusiness]=0"
  39. If Err.Number <> 0 Then
  40. WScript.Echo "ERROR initializing Households.IsBusiness: " & Err.Description
  41. WScript.Quit 1
  42. End If
  43. ' Promote households to business when any related name was previously marked as a business.
  44. Exec conn, "UPDATE [Households] SET [IsBusiness]=1 WHERE [Id] IN (SELECT [HouseholdId] FROM [HouseholderNames] WHERE [IsBusiness]<>0)"
  45. If Err.Number <> 0 Then
  46. WScript.Echo "ERROR copying IsBusiness to Households: " & Err.Description
  47. WScript.Quit 1
  48. End If
  49. WScript.Echo "Copied IsBusiness values to Households"
  50. Exec conn, "ALTER TABLE [HouseholderNames] DROP COLUMN [IsBusiness]"
  51. If Err.Number <> 0 Then
  52. WScript.Echo "ERROR dropping HouseholderNames.IsBusiness: " & Err.Description
  53. WScript.Quit 1
  54. End If
  55. WScript.Echo "Dropped HouseholderNames.IsBusiness"
  56. Else
  57. WScript.Echo "HouseholderNames.IsBusiness does not exist; nothing to drop"
  58. End If
  59. conn.Close
  60. Set conn = Nothing
  61. WScript.Echo "Done."
  62. ' --- helpers ---
  63. Sub Exec(c, sql)
  64. Err.Clear
  65. c.Execute sql
  66. End Sub
  67. Function ColumnExists(c, tableName, colName)
  68. Dim rs
  69. ColumnExists = False
  70. Err.Clear
  71. Set rs = c.OpenSchema(4, Array(Empty, Empty, tableName, colName)) ' adSchemaColumns=4
  72. If Err.Number <> 0 Then
  73. Err.Clear
  74. Exit Function
  75. End If
  76. If Not rs.EOF Then ColumnExists = True
  77. rs.Close
  78. Set rs = Nothing
  79. End Function

Powered by TurnKey Linux.