How to write a select statement for a table in postgres that is inside a schema? - postgresql

I have a postgres DB and inside of it there are many schemas.
Each one of those schemas contains tables. For example:
Schema Name: personal has tables actions_takes, page_views etc
How can i write a SQL query or ActiveRecord query to query the table inside the schema?
Something like:
select * from actions_takes where user_id = 123;
I can create a model for each table and query it that way, but i want to write a script that passed a user goes over all tables and get the data for that user.

in pgAdmin 4 web console should use double quotation marks like following select statement
SELECT "col1", "col2"
FROM "schemaName".profile;

Point to specific table within a given schema using a dot notation schema.table_name. In your case it translates to
select * from personal.actions_takes where user_id = 123;

For me this query worked : select * from schemaName."Table_Name"

Related

Use criteria or query dsl to find all tables names in a given schema

Is there a way to find all table names that begin with t_ in a given schema name with criteria API or query DSL( or even database metadata)? If it exists, could you please show me how I can do it using a schema name or view? I'm using PostgreSQL for the database.
I don't want to use a native query.
yes, you can use below query:
SELECT table_catalog,table_schema,table_name
FROM information_schema.tables
WHERE table_name LIKE 't\_%'
AND table_type='BASE TABLE' -- to filter out Tables only, remove if you need to see views as well

How to fetch column names of a table using HQL and populate in a list?

I want to fetch column names of a table using HQL and populate the same in a list?..Please guide. I am using IBM DB2 database.
I have not tested, but you can query the DB2 catalog in order to get the metadata. If you execute the equivalent in HQL, it should work
select colname
from syscat.columns
where tabschema like 'MYSCHEMA%' and tabname = 'MYTABLE'
order by colno

Common Table Expression in PostgreSQL

I am just learning the CTE and I want to create the dynamic query inside the WITH clause.
Below is what i have written code.
WITH name1 AS (
SELECT schema_name as my_schema
FROM public.schema_table
), name2 AS (
SELECT num_rows as my_row
FROM my_schema.row_table
)
SELECT my_row
from name2;
From the first query inside the WITH give number of schema exist in one database and that schema name return by
SELECT schema_name as my_schema
FROM public.schema_table
I want to use in second query as I am saving it to my_schema.
but when i run this query then it gives me error like my_schema not exists that correct because I want to use the value my_schema contains.
How can you do that?
Firstly, you do not appear to have used name1 anywhere in your query, so I suspect you may not have understood WITH fully.
However it looks like you might have multiple tables called row_table each living in it's own schema and want to create a query that will let you choose which schema to fetch from.
FROM my_schema.row_table
^^^^^^^^^
my_schema is not a variable, it is the name of the schema. To do what you want you are going to have to use the pg_catalog tables to find the oid for the relation within the schema that you need and look up the information that way.
An alternative solution could be to manipulate the search path to do your bidding:
BEGIN;
SET LOCAL SCHEMA 'my_schema';
SELECT num_rows FROM row_table; -- will use my_schema.
COMMIT;

Postgresql dblink

Trying to be lazy when looking at an example
SELECT realestate.address, realestate.parcel, s.sale_year, s.sale_amount,
FROM realestate INNER JOIN
dblink('dbname=somedb port=5432 host=someserver
user=someuser password=somepwd',
'SELECT parcel_id, sale_year,
sale_amount FROM parcel_sales')
AS s(parcel_id char(10),sale_year int, sale_amount int)
Is there a way of getting the AS section filled in from the table?
I'm copying data from tables of the same name and structure on different servers.
If I can get the structure to copy from the existing table, it will save me a lot of time
Thanks
Bruce
The answer is: No. See the doc:
Since dblink can be used with any query, it is declared to return record, rather than specifying any particular set of columns. This means that you must specify the expected set of columns in the calling query — otherwise PostgreSQL would not know what to expect.
http://www.postgresql.org/docs/9.1/static/contrib-dblink-function.html
Edit: by the way, for a table or a view, you can get the fields name and type in a first query:
select column_name
from information_schema.columns
where table_name = 'your_table_or_view';
You could then use it to fill the fields declaration.
Alexis

SQL join from multiple tables

We've got a system (MS SQL 2008 R2-based) that has a number of "input" database and a one "output" database. I'd like to write a query that will read from the output DB, and JOIN it to data in one of the source DB. However, the source table may be one or more individual tables :( The name of the source DB is included in the output DB; ideally, I'd like to do something like the following (pseudo-SQL ahoy)
select o.[UID]
,o.[description]
,i.[data]
from [output].dbo.[description] as o
left join (select [UID]
,[data]
from
[output.sourcedb].dbo.datatable
) as i
on i.[UID] = o.[UID];
Is there any way to do something like the above - "dynamically" specify the database and table to be joined on for each row in the query?
Try using the exec function, then specify the select as a string, adding variables for database names and tables where appropriate. Simple example:
DECLARE #dbName VARCHAR(255), #tableName VARCHAR(255), #colName VARCHAR(255)
...
EXEC('SELECT * FROM ' + #dbName + '.dbo.' + #tableName + ' WHERE ' + #colName + ' = 1')
No, the table must be known at the time you prepare the query. Otherwise how would the query optimizer know what indexes it might be able to use? Or if the table you reference even has an UID column?
You'll have to do this in stages:
Fetch the sourcedb value from your output database in one query.
Build an SQL query string, interpolating the value you fetched in the first query into the FROM clause of the second query.
Be careful to check that this value contains a legitimate database name. For instance, filter out non-alpha characters or apply a regular expression or look it up in a whitelist. Otherwise you're exposing yourself to a SQL Injection risk.
Execute the new SQL string you built with exec() as #user353852 suggests.