(PostgreSQL) Stored Procedure to move data from Table1 to Table 2 - sql

How would you move all the data in Table1 to Table2 on PostgreSQL using a stored procedure when Table1 and Table2 have different column names (cannot simply JOIN).
Assume both tables are already made in the DB and the purpose is once data from Table1 is moved to Table2, Table1 gets wiped (using a trigger?), and assume Table2 is blank before any data is moved from Table1.
Please refer to image below for a simplified example.
Was thinking using a cursor in some way or form. Would that be efficient?
Much thanks!
Click for image sample

Related

Is it possible to update a local table with values from a linked table?

I have two identical tables in my database, T1 and T2. T1 is a local table while T2 is a linked table from a live SQL database. At the moment the two tables are identical.
In short, I would like to be able to run a query that will update T1 will all new records that have been added to T2. So once I have run the query, the two tables should be identical again. Is this at all possible? I need to have the data in T1 locally available as I need to be able to query that table even when the data in T2 is not available. The SQL database in question is off site so I will not always be able to run my queries as the link is unreliable.
Any assistance will be greatly appreciated.
If you do have a unique ID for every record and are sure records already entered will never be changed this can actually be quite simple:
INSERT INTO [TBL_INVOICES_LOCAL]
SELECT TBL_INVOICES.*
FROM [TBL_INVOICES_LOCAL] RIGHT JOIN TBL_INVOICES ON [TBL_INVOICES_LOCAL].InvoiceID = TBL_INVOICES.InvoiceID
WHERE ((([TBL_INVOICES_LOCAL].InvoiceID) Is Null));
All you need to do is join the two tables with the relationship being set to:
Include ALL records from LINKED_TABLE and only records from LOCAL_TABLE where the joined fields are equal.
Setting the criteria of the Local Tables ID field to "Is Null" will only show missing records. If you do this in an append query you can update your table in only one query as seen below:

SQL data migration

I'm just going to use an illustration to explain my problem. SQL Migration:
In the attached image are 2 SQL tables, Table 2 is referencing a primary key of table 1. Table 1 however had many duplicates so I deleted all the duplicates using Excel and imported the data into a new table with a new set of IDs. I now have to import table 2 into a new table and reference table 1 again like before. Now in the image it looks fairly easy to do that but I am dealing with a database of over 2000 rows after eliminating around 700 duplicates. Besides manually editing each row and matching them is there any way of doing this quickly. This is the first time I doing database migration but guessing there a quick ways of doing this. Google searches did not really bear any results. I appreciate any help on this.
To preview the data you need to delete from 2nd table please run this:
select * from names
where place_id NOT in (select id from places)
If you are happy with results you can run
delete from names
where place_id NOT in (select id from places)
after copying table2 into table2New
update table2New
set PlaceNameID = (select t1N.ID
from table2 as t2
join table1 as t1
on t1.ID = t2.PlaceNameID
join table1new as t1N
on t1.PlaceName = t1N.PlaceName
where table2New.ID = t2.ID
);

MS SQL Server 2017- Merging large tables automatically

Using Microsoft SQL server 2017, I want to merge two tables into a 3rd table.
Is there a way to have it defined automatically, without a query?
If it is not possible, I would like to mention that both tables contain a large amount of columns, therefore I look for an efficient query that will not make me write each column name.
Demonstration:
Original tables are Table1,Table2, column structure is as below:
Table1:
(Column1,Column2,Column3)
Table2:
(Column4,Column5,Column6)
My goal is to create Table3. Table3 is based on Table1 LEFT JOIN Table2 ON Column1=Column4.
Table 3:
(Column1,Column2,Column3,Column4,Column5,Column6)
Create it as a view.
CREATE VIEW table3 AS
SELECT Column1,Column2,Column3,Column4,Column5,Column6
FROM Table1 LEFT JOIN Table2 ON Column1=Column4
You can then reference table3 in other queries exactly like an ordinary table.

Merge multiple tables in SSIS or SQL command

I'm facing a problem with fetching data from multiple sources. It would be great if you can provide your ideas to design the SQL query.
I have to take data from two tables and INSERT it into a third table.
INPUT
TABLE 1
- TaskOrderNumer
- MemberID
TABLE 2
- ReferenceID
- MemberID
OUTPUT
TABLE 3
- TaskRefID
- PatID
My input table has TaskOrderNumber and MemberID. Right now I'm joining TABLE1 and TABLE2 based on MemberID. I'm getting the corresponding ReferenceID from TABLE2 and mapping it into PatID of TABLE3. The TaskOrder Number in TABLE1= TaskRefID in TABLE3.
I'm currently doing this using SSIS components. I want to make sure that the correct data is MERGED. I'm not able to map the TaskOrderNumber to TaskRefID. Can you please help me design the solution.
You can simply query the information you are trying to display. I am not sure I would even bother with SSIS here unless your sources aren't SQL.
select t1.TaskOrderID as TaskRefID
,t2.ReferenceID as PatID
into Table3 --Added this as edit.
from Table1 t1
join Table2 t2 on t1.MemberID=t2.MemberID

Comparing the data of two tables in the same database in sqlserver

I need to compare the two table data with in one database.match the data using some columns form table.
Stored this extra rows data into another table called "relationaldata".
while I am searched ,found some solutin.
But it's not working to me
http://weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx
can any one help how to do this.
How compare two table data with in one database using redgate(Tool)?
Red Gate SQL Data Compare lets you map together two tables in the same database, provided the columns are compatible datatypes. You just put the same database in the source and target, then go to the Object Mapping tab, unmap the two tables, and map them together.
Data Compare used to use UNION ALL, but it was filling up tempdb, which is what will happen if the table has a high row count. It does all the "joins" on local hard disk now using a data cache.
I think you can use Except clause in sql server
INSERT INTO tableC
(
Col1
, col2
, col3
)
select Col1,col2,col3from tableA
Except
select Col1,col2,col3 from tableB
Please refer for more information
http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
Hope this helps