sql - 带有表名 bindvar 的 Postgres sqlx 准备语句

标签 sql postgresql go prepared-statement

我正在尝试使用 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?

不,source of quote.

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/

相关文章:

sql - 丢弃该组中不是 MAX 的行

ruby-on-rails - Rails 应用试图更新不存在的列

arrays - 如何在 PostgreSQL 12 中将元素添加到 jsonb 数组中?

go - 当非默认是输入 channel 时,为什么 select in go 总是进入默认情况?

json - 使用不同的键解码相同的 json 对象以进行 slice 结构

sql - 连接两个表的问题

sql - 如何在 SQL Server 中获取 MMYYYY 格式?

PHP MySQL 查询在酒店预订系统中选择可用房间

php - 使用 PDO 准备好插入后获取最后一个插入 ID

Golang项目目录结构