我正在尝试使用 Golang sqlx 库创建准备好的语句。我想让表名成为一个 bindVar
stmt, err := stmtTx.Preparex("SELECT * FROM $1 WHERE question_id=$2;")
然而,这给了我关于 /$1/
的语法错误。我可以不使用绑定(bind)变量作为表名吗?
最佳答案
Can I not use a bind var for the table name?
The arguments can only be used as data values, not as identifiers. Thus for example this is reasonable:
INSERT INTO mytable VALUES ($1);
but this will not work:
INSERT INTO $1 VALUES (42);
但如果需要,您可以使用 fmt.Sprintf
作为表名,但保留 $1、$2... 作为数据值。
关于sql - 带有表名 bindvar 的 Postgres sqlx 准备语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631006/