Need to Create a stored procedure to update table - postgresql

I am looking to update a Table B from Table A (Master Table) when ever a new record is published.
the table data is same and just a dump to table B to maintain more data volume.
I am looking to write a stored proc for this and below is what i have, but it throws an error at the begin statement
ID column is the unique identifier here. and DB is PostgreSQL
create or replace procedure proc 1 ()
as
begin
INSERT into table B (Col1, Col2, Col3, etc)
select (Col1, Col2, Col3, etc) from table A
where not exists (
select (Col1, Col2, Col3, etc)
from table B
where table A.id = TableB.id
end
Any help is much appreciated.

Related

How to use the same common table expression in two consecutive psql statements?

I'm trying to perform a pretty basic operation with a few steps:
SELECT data from table1
Use id column from my selected table to remove data from table2
Insert the selected table from step 1 into table2
I would imagine that this would work
begin;
with temp as (
select id
from table1
)
delete from table2
where id in (select id from temp);
insert into table2 (id)
select id from temp;
commit;
But I'm getting an error saying that temp is not defined during my insert step?
Only other post I found about this is this one but it didn't really answer my question.
Thoughts?
From Postgres documentation:
WITH provides a way to write auxiliary statements for use in a larger
query. These statements, which are often referred to as Common Table
Expressions or CTEs, can be thought of as defining temporary tables
that exist just for one query.
If you need a temp table for more than one query you can do instead:
begin;
create temp table temp_table as (
select id
from table1
);
delete from table2
where id in (select id from temp_table);
insert into table2 (id)
select id from temp_table;
commit;

Postgres INSERT trigger (same row)

Assume I have the following postgresql table, i.e. Tbl1:
In Tbl1 I have the following attributes (C_ID is the unique ID field updated by means of a sequence):
C_ID, Col2, Col3, Col4, C_IDr, Col5, etc.
I want to create a trigger that when I INSERT a new record, the trigger must fire and set field C_IDr (5th column) equal to C_ID (based on a certain condition - the condition is the easy part), thus INSERT new record: C_IDr = C_ID
How do I go about achieving that?

Issue Insert Into (BigTable1) SELECT From (BiggerTable2)

I am selecting a set of data from a bigTable1 (index-ed) and then inserting them into another bigTable2 (index-ed)
I have two options: Which is a good idea:
Option: 1
INSERT INTO bigTable2 (bigTable2.Col1, bigTable2.Col2)
SELECT bigTable1.Col1, bigTable1.Col2 FROM bigTable1 (nolock)
WHERE bigTable1.col3 between #value1 and #value2
Option: 2
CREATE #TEMP (Col1 int, Col2 varchar(200))
INSERT INTO #TEMP (Col1, Col2)
SELECT bigTable1.Col1, bigTable1.Col2 FROM bigTable1 (nolock)
WHERE bigTable1.col3 between #value1 and #value2
INSERT INTO bigTable2 (bigTable2.Col1, bigTable2.Col2)
SELECT Col1, Col2 FROM #TEMP
I do not want to lock the bigTable1. Please advise which one is the better one between the two? Is there any other suggestion?
If you don't want to lock the table, then go with the first approach. It's a one-step procedure and, no matter how long it takes, you keep the table unlocked, so you don't block others.
The second option would be appropriate in case you wanted the table locked during the select. Having to fill an empty, non-indexed table would be faster, thus keeping your table locked for a shorter period.

PostgreSQL - INSERT INTO statement

What I'm trying to do is select various rows from a certain table and insert them right back into the same table. My problem is that I keep running into the whole "duplicate PK" error - is there a way to skip the PK field when executing an INSERT INTO statement in PostgreSQL?
For example:
INSERT INTO reviews SELECT * FROM reviews WHERE rev_id=14;
the rev_id in the preceding SQL is the PK key, which I somehow need to skip. (To clarify: I am using * in the SELECT statement because the number of table columns can increase dynamically).
So finally, is there any way to skip the PK field?
Thanks in advance.
You can insert only the values you want so your PK will get auto-incremented
insert into reviews (col1, col2, col3) select col1, col2, col3 from reviews where rev_id=14
Please do not retrieve/insert the id-column
insert into reviews (col0, col1, ...) select col0, col1, ... from reviews where rev_id=14;

Copy three columns from one database table to another

I'm updating an iPhone app with a SQLite3 database. The user's have a database on their phone currently, and I need to update three of the columns with new data (stored in a separate database) if the id of the rows match.
I've been able to attach the two tables and copy an entire table, but not only three columns.
database1
table1
id, col1, col2, col3, col4
database2
table1
id, col1, col2, col3, col4
I want to copy col1, col2, & col3 (not col4) from database1, table1 to database2, table1 if the ids match.
You could use a query along the following lines:
//Select the database, table and table values
SELECT INTO 'db2.table2' (field1, field2, field3)
VALUES
//Insert data into second database using nested SQL
(SELECT * FROM 'db1.table1' (field1, field2, field3) WHERE field1 = 1)
Hope this helps (and works for you) :)