Permalink
Browse files

work around a bug in MSSQL

  • Loading branch information...
1 parent 6aeb394 commit a45f5511bbcd1d210ac475423d9f6709c2c5efd8 @ssandler ssandler committed Feb 12, 2013
Showing with 8 additions and 0 deletions.
  1. +8 −0 TeslaSQL/DataUtils/MSSQLDataUtils.cs
@@ -234,6 +234,14 @@ public class MSSQLDataUtils : IDataUtils {
query += " LEFT OUTER JOIN " + sourceDB + "." + table.SchemaName + "." + table.Name + " P ON " + table.PkList;
query += " WHERE (SYS_CHANGE_VERSION <= @stopversion OR SYS_CHANGE_CREATION_VERSION <= @stopversion)";
query += " AND (SYS_CHANGE_OPERATION = 'D' OR " + table.NotNullPKList + ")";
+ /*
+ * This last segment works around a bug in MSSQL. if you have an identity column that is not part of the table's
+ * primary key, and a delete happens on that table, it would break the above with this error:
+ * "Attempting to set a non-NULL-able column's value to NULL."
+ * The workaround is to add this no-op UNION ALL, which prevents SQL from putting the identity column as not-nullable
+ * On the destination table, for some reason.
+ * */
+ query += " UNION ALL SELECT " + table.SimpleColumnList + ", NULL, NULL FROM " + sourceDB + "." + table.SchemaName + "." + table.Name + " WHERE 1 = 0";
SqlCommand cmd = new SqlCommand(query);

0 comments on commit a45f551

Please sign in to comment.