Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query inside a function

I am using PostgreSQL with PostGis. I am executing a query like this:

select st_geomfromtext('point(22 232)',432)

It works fine. But now I want to take a value through a query. for example:

select st_geomfromtext('point((select x from data_name where id=1) 232)' , 432)

Here data_name is some table I am using and x stores some values. Now query inside is treated as a string and no value is returned.
Please help.

ERROR: syntax error at or near "select"
like image 501
Naman Avatar asked Apr 06 '13 21:04

Naman


People also ask

Can we write SQL query in function?

A function is a set of SQL statements that perform a specific task. Functions foster code reusability. If you have to repeatedly write large SQL scripts to perform the same task, you can create a function that performs that task.

Can we use SELECT statement in function in SQL Server?

Select statements included within a function cannot return data to a client. FUNCTION SQL SERVER. Attachments: Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can we write SELECT statement in function?

Functions only work with select statements. Functions can be used anywhere in SQL, like AVG, COUNT, SUM, MIN, DATE and so on with select statements. Functions compile every time. Functions must return a value or result.


1 Answers

Try this:

select st_geomfromtext('point(' || x || ' 232)', 432) from data_name where id=1
like image 108
muratgu Avatar answered Oct 02 '22 15:10

muratgu