douqinlu4217 2016-09-12 15:32
浏览 1712

PostgreSQL“?”参数占位符不适用于“ WITH”

I am trying to use ? in the following way (I use it in Golang to generate query, but it seems like it is not Go dependent):

WITH Tmp(name, enabled) AS (
    VALUES(?, ?),(?, ?)
) 
UPDATE table_groups 
    SET enabled = (SELECT enabled 
                   FROM Tmp 
                   WHERE table_groups.name=Tmp.name) 
WHERE table_groups.name IN (SELECT name FROM Tmp)

getting:

syntax error at or near ","

If I substitute ? in the above statement by concrete values, everything works fine. Is there a problem using ? with WITH and how would I get around it? Thanks.

  • 写回答

1条回答 默认 最新

  • duansen6750 2016-09-12 15:42
    关注

    Go doesn't support this at of the box.

    You can use jmoiron/sqlx if you want this functional

    example using sqlx (from docs):

    var levels = []int{4, 6, 7}
    query, args, err := sqlx.In("SELECT * FROM users WHERE level IN (?);", levels)
    
    // sqlx.In returns queries with the `?` bindvar, we can rebind it for our backend
    query = db.Rebind(query)
    rows, err := db.Query(query, args...)
    
    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部