I'm doing a select in Go using the database/sql package and the pq Postgres driver:
rows, err := db.Query("SELECT (name, age) FROM people WHERE id = 1")
I tried retrieving the values in the normal way:
rows.Next()
name := ""
age := 0
err = rows.Scan(&name, &age)
but I got the error:
sql: expected 1 destination arguments in Scan, not 2
The documentation for sql.(*Rows).Scan says that you can pass a byte slice pointer and it will be filled with the raw results. So I did this:
b := make([]byte, 1024*1024)
rows.Scan(&b)
fmt.Println(string(b))
which succeeded, printing:
(John,18)
So I looked at the source code for sql.(*Rows).Scan, and it turns out that the error is returned if the number of arguments doesn't match the number of results returned by the database driver (which makes sense). So, for some reason, the pq driver seems to be returning the result set as a single value. Why would this be?
SELECT (name, age)
returns a single record (an anonymous composite type), butSELECT name, age
will return two columns – NearsightedUPDATE ... SET (a, b) = (x, y)
, and PostgreSQL has just generalized that by treating(a,b)
as shorthand forROW(a,b)
. – HockeyRETURN QUERY SELECT arg1, arg2
removing parentheses doesn't work for functions – Bystreetselect * from function()
there is no need to remove any parentheses – Nearsightedselect from function
! thx for ur response – Bystreet