Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table as:

CREATE TABLE tbl_temp (id serial, friend_id int, name varchar(32));  

I wish I could run the following SQL:

PREPARE x AS SELECT {$1,friend_id} FROM tbl_temp WHERE id = ANY($2);  
EXECUTE x(33, ARRAY[1,2,3,4])

I basically looking for a statement that will return me an array of two ints first of which will be user input and second will be from table column like friend_id.

Is it really possible in PostgreSQL?

Please advise.

The results from SELECT ($1, friend_id) FROM tbl_temp;

EXECUTE x(44);
  row     
--------  
 (44,1)  
 (44,2)  
 (44,3)  
(3 rows)

If I use PQgetvalue(PGres, 0, 0) How will the result look like: {44,45} or like(44,45)

Regards,
Mayank

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

I think you want to use the array constructor syntax:

SELECT ARRAY[$1, friend_id] FROM tbl_temp WHERE id = ANY($2)
share|improve this answer
    
Thanks for the reply. I always see a correct answer from you. I wish I could vote up twice :) –  Mayank May 12 '11 at 18:19
add comment

i'm not sure i understand what you want...

to return an array, do this.

SELECT (44, "friend_id") FROM "tbl_temp" WHERE id = ANY(ARRAY[1,2,3,4]);
share|improve this answer
    
Thanks for the reponse. I actually was looking for something like SELECT ARRAY[$1, friend_id] FROM tbl_temp WHERE id = ANY($2) ... Got it from mu is too short –  Mayank May 12 '11 at 18:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.