{"record":{"id":"f9bbf372c5a48c53","repo":"OtterMind/Chat2DB","slug":"unable-to-rewrite-table-extended-property-sql","errorCode":null,"errorMessage":"Unable to rewrite table extended property: {sql}","messagePattern":"Unable to rewrite table extended property: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/SqlServerDBManager.java","lineNumber":484,"sourceCode":"            } else if (current == '/' && next == '*') {\n                state.blockCommentDepth++;\n                i++;\n            } else if (current == '\\'') {\n                state.inString = true;\n            } else if (current == '[') {\n                inBracketIdentifier = true;\n            } else if (current == '\"') {\n                inQuotedIdentifier = true;\n            }\n        }\n        return !inLineComment && state.isCode() && !inBracketIdentifier && !inQuotedIdentifier;\n    }\n\n    private static String rewriteExtendedPropertyTable(String sql, String tableName, String newTableName) {\n        Pattern pattern = Pattern.compile(\"(?i)('TABLE'\\\\s*,\\\\s*N')\" + Pattern.quote(tableName) + \"(')\");\n        Matcher matcher = pattern.matcher(sql);\n        if (!matcher.find()) {\n            throw new IllegalArgumentException(\"Unable to rewrite table extended property: \" + sql);\n        }\n        String replacement = matcher.group(1) + newTableName.replace(\"'\", \"''\") + matcher.group(2);\n        return matcher.replaceFirst(Matcher.quoteReplacement(replacement));\n    }\n\n    static boolean isCopyableColumn(String computedDefinition, String dataType) {\n        return StringUtils.isBlank(computedDefinition)\n                && !\"timestamp\".equalsIgnoreCase(dataType)\n                && !\"rowversion\".equalsIgnoreCase(dataType);\n    }\n\n    static String buildCopyDataSql(String targetTable, String columnList, String sourceTable) {\n        return String.format(SQL_COPY_TABLE_DATA_WITH_COLUMNS, targetTable, columnList, columnList, sourceTable);\n    }\n\n    static void executeIdentityCopy(String targetTable, String insertSql, Consumer<String> executor) {\n        String identityOn = String.format(SQL_SET_IDENTITY_INSERT, targetTable, \"ON\");\n        String identityOff = String.format(SQL_SET_IDENTITY_INSERT, targetTable, \"OFF\");","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-server/chat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/SqlServerDBManager.java#L466-L502","documentation":"Thrown by SqlServerDBManager.rewriteExtendedPropertyTable when the regex 'TABLE'\\s*,\\s*N'<tableName>' does not match anywhere in the extended-property batch. Extended properties (sp_addextendedproperty style) carry the table name as a quoted N'...' argument; if the stored name differs from the supplied tableName, the rewrite cannot proceed and fails.","triggerScenarios":"Processing an extended-property DDL batch whose @value=... or @level1name=N'...' table argument does not contain the exact tableName passed in - e.g. the property references a schema-qualified or differently-cased name, or the batch is a column/level-2 property that has no table-level N'<tableName>' slot.","commonSituations":"Case/bracketing differences between the metadata table name and the name embedded in the extended property; an extended property batch for a different level (column) reaching the table rewriter; partial DDL export.","solutions":["Confirm tableName matches the N'...' argument exactly (case-sensitive, including any bracket characters).","Ensure only table-level extended property batches are routed to rewriteExtendedPropertyTable; route column/level-2 batches through their own handler.","If the batch is unrelated, skip it (continue) instead of attempting the rewrite."],"exampleFix":"// before: property stored N'Order_Header' but tableName passed \"order_header\"\nrewriteExtendedPropertyTable(batch, \"order_header\", \"Order_Header_Copy\");\n// -> Unable to rewrite table extended property\n\n// after: pass the exact stored name\nrewriteExtendedPropertyTable(batch, \"Order_Header\", \"Order_Header_Copy\");","handlingStrategy":"validation","validationCode":"// Confirm the stored table name appears as N'<tableName>' before rewriting\nif (!Pattern.compile(\"(?i)'TABLE'\\\\s*,\\\\s*N'\" + Pattern.quote(tableName) + \"'\").matcher(batch).find()) {\n    // route elsewhere or skip, do not call rewriteExtendedPropertyTable\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Match the exact stored table name (case/brackets) before rewriting.","Route column/level-2 extended properties to their own handler.","Skip unrelated extended-property batches."],"tags":["sqlserver","ddl","table-copy","extended-property","parsing"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}