SQLite Forum

Column name includes quotes used in RETURNING expr
Login
I'm fairly sure this is a bug in SQLite.  Here's a patch that can fix it, though it may break something else.  I don't have any familiarity with the SQLite source code. At least the patch probably needs tests to be accepted.  However, hopefully this indicates how the problem could be fixed.

This patch is against the sqlite3.c file in sqlite-autoconf-3350500.tar.gz.

```
Index: sqlite3.c
--- sqlite3.c.orig
+++ sqlite3.c
@@ -139401,6 +139401,9 @@ static ExprList *sqlite3ExpandReturning(
       if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
         struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
         pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
+        if ( pList->a[i].eEName!=ENAME_NAME && pOldExpr->op==TK_ID ){
+          sqlite3Dequote(pItem->zEName);
+        }
         pItem->eEName = pList->a[i].eEName;
       }
     }
```

Example of usage after patch:

```
.headers on
CREATE TABLE a (s INTEGER, t INTEGER);
INSERT INTO a VALUES (1, 2) RETURNING `t`;
t
2
SELECT `t` FROM a;
t
2
INSERT INTO a VALUES (1, 3) RETURNING "t";
t
3
INSERT INTO a VALUES (1, 4) RETURNING "t" AS `t`;
t
4
```