{"record":{"id":"fcd1161164cc90d6","repo":"OtterMind/Chat2DB","slug":"unable-to-locate-the-source-create-table-statement","errorCode":null,"errorMessage":"Unable to locate the source CREATE TABLE statement","messagePattern":"Unable to locate the source CREATE TABLE statement","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":282,"sourceCode":"                        targetTable);\n                rewritten = rewriteSelfReference(rewritten, sourceReferences, tableName, targetTable);\n                // Constraint names are schema-scoped, so copied declarations must receive fresh server-generated names.\n                rewritten = removeNamedTableConstraints(rewritten);\n                createTableRewritten = true;\n            } else if (CREATE_INDEX_BATCH.matcher(batch).find()) {\n                rewritten = replaceObjectAfterKeyword(batch, \"ON\\\\s+\", sourceReferences, targetTable);\n            } else if (isExtendedPropertyBatch(batch)) {\n                if (CONSTRAINT_COMMENT.matcher(batch).find()) {\n                    // Auto-generated target constraint names cannot be addressed by the source constraint comment.\n                    continue;\n                }\n                rewritten = rewriteExtendedPropertyTable(batch, tableName, newTableName);\n            }\n            rewrittenBatches.add(rewritten.trim());\n        }\n\n        if (!createTableRewritten) {\n            throw new IllegalArgumentException(\"Unable to locate the source CREATE TABLE statement\");\n        }\n        return rewrittenBatches;\n    }\n\n    static List<String> splitDdlBatches(String ddl) {\n        List<String> batches = new ArrayList<>();\n        StringBuilder batch = new StringBuilder();\n        DdlLexicalState state = new DdlLexicalState();\n        String[] lines = ddl.split(\"\\\\R\", -1);\n        for (String line : lines) {\n            if (state.isCode()) {\n                if (GO_BATCH_LINE.matcher(line).matches()) {\n                    addBatch(batches, batch);\n                    continue;\n                }\n                Matcher inlineBatch = GO_EXTENDED_PROPERTY_LINE.matcher(line);\n                if (inlineBatch.matches()) {\n                    addBatch(batches, batch);","sourceCodeStart":264,"sourceCodeEnd":300,"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#L264-L300","documentation":"Thrown by SqlServerDBManager.prepareCopyDdlBatches when, after splitting and rewriting each DDL batch, no batch was recognized as the source CREATE TABLE statement (the flag createTableRewritten stayed false). The rewriter only flips that flag when a batch matches the 'CREATE\\s+TABLE' keyword prefix and is successfully retargeted.","triggerScenarios":"The supplied DDL, after batch splitting (on GO / lexical state), contains no batch beginning with 'CREATE TABLE'. This happens when the DDL is entirely extended properties/constraints/indexes, when the CREATE keyword is inside a comment or string so the lexical scanner treats it as code-free, or when the statement uses a different phrasing.","commonSituations":"Source definition returned only secondary objects; a tool exports DDL with the CREATE TABLE wrapped or commented; GO-batch splitting mis-segmented a multi-statement batch; case/comment edge cases in the DdlLexicalState scanner.","solutions":["Inspect the split batches (splitDdlBatches) to confirm a batch literally begins with 'CREATE TABLE'.","Ensure the DDL is the raw CREATE TABLE script, not a wrapped/sp_help-style report.","If the DDL legitimately lacks a CREATE TABLE batch, surface a 'cannot copy: definition not in expected form' error instead of relying on the implicit throw."],"exampleFix":"// before: ddl only had ALTER/extended-property batches, no CREATE TABLE\nprepareCopyDdlBatches(ddl, ...);\n// -> Unable to locate the source CREATE TABLE statement\n\n// after\nList<String> batches = splitDdlBatches(ddl);\nboolean hasCreate = batches.stream().anyMatch(b -> startsWithKeyword(b, \"CREATE\\\\s+TABLE\"));\nif (!hasCreate) {\n    throw new IllegalStateException(\"Source DDL has no CREATE TABLE batch\");\n}\nprepareCopyDdlBatches(ddl, ...);","handlingStrategy":"validation","validationCode":"List<String> batches = splitDdlBatches(ddl);\nboolean hasCreate = batches.stream().anyMatch(b -> startsWithKeyword(b, \"CREATE\\\\s+TABLE\"));\nif (!hasCreate) {\n    throw new IllegalStateException(\"Source DDL has no CREATE TABLE batch\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Inspect splitDdlBatches output to confirm a CREATE TABLE batch exists.","Supply raw CREATE TABLE DDL, not wrapped reports.","Surface a clear 'definition not in expected form' error upstream."],"tags":["sqlserver","ddl","table-copy","parsing"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}