last day (15 days later) » 

7:46 PM
1
A: how to convert a fetch row to a single string in postgresql

max taldykinYou can convert row to text just by casting it to text: BEGIN; DECLARE cur1 CURSOR FOR SELECT t.*::text FROM Table t; FETCH 1 FROM cur1; Row values will be separated by comma and whole row will be enclosed into parenthesis. If you also want column names it is possible to convert row to JSON: ...

 
yes, the codes can convert row to text, but they cannot used in the cursor situation.
 
Why not? Just prepend DECLARE cur1 CURSOR FOR and you will get a cursor.
 
how do you select the table? I used cur1 as the table (SELECT t.rec::text FROM cur1 t), but it does not work, showing: relation "cur1" does not exist. If I selected the original table, all records are showed, instead of just 1 by 1.
 
I just updated my answer with full commands. Hope this will be more clear.
 
One more issue that I cannot use the results of FETCH 1 FROM cur1 for string functions, if I set more than one cursor. For example, SELECT (FETCH 1 FROM cur_1)||","||FETCH 1 FROM cur_2), although the cursors are in text.
 
7:46 PM
fetch does not fit into select. You can join two tables when declaring cursor: declare ... select (t1.*::text)||','||(t2.*::text) from table1 t1 join table2 t2 on t1.xx = t2.yy.
Also, please note that PostgreSQL requires single quotes to enclose text strings. It uses double quotes to enclose table/column names.
 
Thanks for point out the double quote issue!
My intention is to input those strings into a postgresql function.
The input for this function is like FUNCTION geocode_intersection(IN roadway1 text, IN roadway2 text, IN in_state text, IN in_city text DEFAULT '', IN in_zip text DEFAULT ''
But until now, neither separating those inputs into several columns nor combining in the same columns does not work.
If I do not use fetch, can you suggest what can I do?
 
Let us start from the very beginning. You have two tables and want to combine rows of those two tables side-by-side into single string?
 
8:02 PM
two columns in the same table
 
you need to concatenate two columns of some table into single string? then why not just select col1 || ',' || col2 from table?
 
yes, I did that
Then, I need to input the new column into the function
 
I think it will be easier to understand if you show relevant part of your db schema and some example data.
select geocode_intersection(roadway1, roadway2, in_state) from table
 
The data is sensitive to be showed.
Here is the description of this function.
http://postgis.net/docs/Geocode_Intersection.html
The code works for only one input
I have tried to apply to the whole table:
UPDATE TABLE1 SET (rating, lon, lat, new_address) = (rating, ST_X(geomout), ST_Y(geomout), pprint_addy(addy)) FROM geocode_intersection(SELECT address FROM TABLE1);
it showed an error: ERROR: syntax error at or near "SELECT"
 
ok, I see now
do yo have parts of the address in separate columns in TABLE1 or all of them are just in single column named address?
 
8:18 PM
I created two kinds of columns: one is "street1", "street2","state", "city", and the other one is in separate columns, since I am not sure how to input.
 
select rating, ST_X(geomout), ST_Y(geomout), pprint_addy(addy) from TABLE1 t, geocode_intersection(t.street1, t.street2, t.state, t.city)
And, as understand you are trying to update some other fields in the TABLE1 with the result of this function. This will not work because geocode_intersection returns set of records. You need to pick one of them (e.g. the one with highest raing).
 
Do you mean put LIMIT 1 in the back of the query?
 
Yep, you can put LIMIT1, or, as I can see from the doc, there is max_results argument in geocode_intersection. You can set it to 1.
 
8:34 PM
I see! I will set it in the source code.
It finally works. Thanks for the great helps. I am trying to figure out this problems for several days
 
ok, bye
 
bye!
 

last day (15 days later) »