4

i have a simple sql join query

SELECT a.*,b.*
FROM Orders a
JOIN Customers b ON a.CustomerID=b.CustomerID

which selects all columns from both tables . I need to achieve the same in Postgresql function,but i am not able to select data from 2nd table

CREATE  FUNCTION get_data (p_pattern VARCHAR,p_year INT) 
 RETURNS TABLE (
orders.*,Customers.*
) 
AS $$
5
  • please elaborate the post. in function?.. what exactly you do and what is the exact problem? Commented Jul 3, 2017 at 9:34
  • select * selects from all tables in the query in every DBMS I have ever used. Does PostGreSQL differ here? That seems weird/unlikely to me. And yes, "I am not able to X" is not a valid problem description. Commented Jul 3, 2017 at 9:35
  • Your function should return a select statement from the desired tables/columns, not just the table names. Also, presumably you really intend for the function arguments to be used in the where clause, as otherwise the function would not achieve anything useful. Commented Jul 3, 2017 at 10:04
  • what if my table has 100 columns??? this is not a viable solution for that Commented Jul 3, 2017 at 10:08
  • Read both of my comments on this post, and then combine them. You should be able to select * from tblA inner join tblB on blah and get all the columns just fine. You then need to incorporate that select into your function in a syntactically correct way, not what you've tried above. Commented Jul 3, 2017 at 10:19

2 Answers 2

4

The one problem is that neither function nor views can return the columns with same names (in your example columns CustomerID presented in both tables). And the another one - syntax:

RETURNS TABLE ( column_name column_type [, ...] )

from the official doc, nothing about table_name.*.

Aside of the obvious solution where you specifying the complete list of columns, there is one trick with composite (row, record) types:

CREATE  FUNCTION get_data (p_pattern VARCHAR,p_year INT) 
  RETURNS TABLE (order orders, customer customers) 
AS $$

Note that you can use table/view names as types in declarations.

And in that case your query could looks like

SELECT a, b
FROM Orders a
JOIN Customers b ON a.CustomerID=b.CustomerID

After that the usage of the function would be:

select
  *, -- two composite columns
  (order).*, -- all columns from table orders
  (customer).*, -- all columns from table customers
  (order).CustomerID -- specific column from specific table  
from
  get_data(<parameters here>);

dbfiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Considering the columns are present on which you are joining, you can do this:

    SELECT * FROM Orders a,Customers b WHERE a.CustomerID=b.CustomerID;

For more see the official docs: https://www.postgresql.org/docs/8.2/static/tutorial-join.html

You can also refer this: https://www.tutorialspoint.com/postgresql/postgresql_using_joins.htm .It has good examples and references what joins are there in postgre and how to do them.

7 Comments

i need to select all columns from both tables as mentioned, this will only return columns from Orders table
According to the official docs, this query should do the trick and return all columns of both the tables.
My point is that while explaining how to do something, you should follow established good practices, rather than recommending outdated, error-prone syntax.
@underscore_d Point accepted. But shouldn't the docs get updated to show which syntax is outdated or recommended. Anyways thanks for the update and info. I didn't know this before.
No need, sorry, I didn't realise you had the 1st link, which does show the old syntax. I hadn't seen that link, only the 2nd one - which is better - and thought you just added the old join syntax yourself. The official docs are weird as they recommend the old syntax and then say inner join is just an 'alternative' and 'less used'. :S Nowdays, the reality is opposite.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.