Execute a String Sql in PostgreSQL [duplicate] - postgresql

This question already has answers here:
dynamic query postgres
(2 answers)
Closed 8 years ago.
In a function, I have a SELECT query in a string, for example:
sql='SELECT * FROM A'
I want to execute sql output result of: SELECT * FROM A
How can I execute the string sql in PostgreSQL?

Inside a function use EXECUTE.
http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

below one works fine in postgres 8.4
UDBI=> PREPARE query as select 1 as a;
PREPARE
UDBI=> PREPARE query
UDBI=> EXECUTE query;
a
---
1
(1 row)
UDBI=>

Related

how to solve ** relation "dual" does not exist ** problem in PostgreSQL [duplicate]

This question already has answers here:
Equivalence of from dual in PostgreSQL
(5 answers)
Closed 7 months ago.
I'm trying to convert Oracle to PostgreSQL. While converting I'm getting error like
relation "dual" does not exist .
EXECUTE 'SELECT DBMS_RANDOM.VALUE*(POWER(10,6)) FROM DUAL'
please help me to solve this error.
Dual is a dummy table specific for oracle. In PostgreSQL you can either just use
SELECT random()*power( 10, 6 );
or create your own dual table if you want to keep the dual. (and insert 1 row in it)
You can create a view named dual like:
CREATE VIEW public.dual AS SELECT 'X'::varchar AS dummy;
REF: https://pgpedia.info/d/dual-dummy-table.html

What is the exact way of using where clause in PostgreSQL 12? [duplicate]

This question already has answers here:
PostgreSQL "Column does not exist" but it actually does
(6 answers)
sql statement error: "column .. does not exist"
(1 answer)
Postgres column does not exist
(1 answer)
Closed 1 year ago.
I am trying to query a table using where clause in Postgres 12
case 1
SELECT *
FROM schema.e_employee_table
WHERE FK_EMPLOYEE=100;
so when i query the above query i get "FK_EMPLOYEE" column is missing.
case2:
SELECT *
FROM schema.e_employee_table
WHERE "FK_EMPLOYEE"=100;
when i query the above one i got the query results
So can someone explain do we need to put our column name inside "",is it the syntax. If so where it is mentioned in the official documentation.
And I can also see the column name we are mentioning is case sensitive.

I am having a syntax error in the MERGE statement [duplicate]

This question already has answers here:
Need to convert Oracle "merge into" query to PostgreSQL
(1 answer)
Migrating an Oracle MERGE statement to a PostgreSQL UPSERT statement
(3 answers)
Merge statement with two conditions in same CLAUSE alternative in Postgres
(1 answer)
PostgreSQL Upsert with a WHERE clause
(1 answer)
Closed 2 years ago.
I have postgres 12.3 and find an error in a simple MERGE statement like below:
MERGE INTO lkup_language a
USING (SELECT *
FROM dblc_stg.stg_lkup_home_language
WHERE home_lang_code NOT IN (SELECT home_lang_code
FROM dblc_stg.stg_lkup_home_language
GROUP BY home_lang_code
HAVING COUNT (*) > 1)) b
ON (a.language_cd = b.home_lang_code)
WHEN NOT MATCHED THEN
INSERT (a.language_key, a.language_cd, a.language_desc)
VALUES (NEXTVAL('SEQ_LKUP_LANGUAGE'),b.home_lang_code, b.home_lang_desc)
WHEN MATCHED THEN
UPDATE
SET a.language_desc = b.home_lang_desc ;
I hope I get some help
Thanks
Ajay

How to search for a variable name in PostgreSQL? [duplicate]

This question already has answers here:
How to find a table having a specific column in postgresql
(6 answers)
Closed 5 years ago.
I want to search for a particular variable name in ‘PostgreSQL’ database. Similar to the following ‘Teradata’ query
Select TableName, ColumnName from
DBC.Columns
Where ColumnName like (‘%profile%’)
Is there a similar query in PostgreSQL?
Postgres documentation
SELECT table_name,column_name
FROM information_schema.columns
WHERE column_name like '%profile%'

Insert script to auto populate numbers 1-1000? [duplicate]

This question already has an answer here:
Populate Postgres database with numbers 1-1000?
(1 answer)
Closed 5 years ago.
I have a single table with a single column called id as shown below. I need to populate the table with numbers 1-1000, how can I do that with the scripting?
Use generate_series to this:
--Without column declaration
INSERT INTO "MyTable" SELECT generate_series(1, 1000);
--With column declaration
INSERT INTO "MyTable"(id) SELECT generate_series(1, 1000);