douqinlu4217 2016-09-12 23: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 23: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...)
    
    评论

报告相同问题?