Compare two tables on two sql servers - sql

i wanted to compare and identify certain [transaction ids] that exist in one table which is located in server 1 but does not exist in a similar transformed table on a different server (server 2).
These two tables were supposed to be the same but we noticed some transaction ids were missing on the table that is located in server 2. My goal is to identify all the missing transaction ids.
The query that i am looking to write is as below
SELECT
server_1.table_1.[transaction_id] from server_1.table_1
JOIN
server_2.table_2 ON
Table_1.[transaction_id] = Table_2.[transaction_id]
WHERE
server_1.table_1.[transaction_id] NOT IN server_2.table_2.[transaction_id]
one recommendation I have received was
"Let's select the IDs from the source select the result, right click and in SQL complete select general temp table script
Then you can run the scripts on the target server to create a temptable and perform your analysis
Because of the size of the table perhaps you need to do its in batches"
if anyone can help me explain this process as well please?
Thank you very much for your help in advance
i am new for this , i apologize if I have not explained it enough or if i am not clear with my ask

You can do that with a simple OUTER join (LEFT JOIN):
SELECT table1.transaction_id as missing_in_table2
FROM table1 LEFT JOIN table2
ON table1.transaction_id=table2.transaction_id
WHERE table2.transaction_id IS NULL

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:

Conditional Join act as a mater table

Hi sorry I could not find a way to best title what I am looking for.
Anyway I have an idea of how to do something but I just need a new pair of eyes to look at what I am trying to do to see if it is possible. I basically have two tables one which has a load of text and numbers and another one which acts like a master table so if anything is found in the second table use that otherwise only use what is found in the first table. However I am unsure how to complete the select statement for this, I know i can go down the route of doing two separate select statements and union them up together but there must be an easier way. After playing around I have a query which I think may work but I am unsure if I have missed something. For example we have Table A and Table B (B holding the master data)
SELECT DISTINCT
A.ID,
COALESCE(B.PROD, A.PROD) AS PROD
COALESCE(B.TEXT1, A.TEXT1) AS TEXT1,
COALESCE(B.NUMBER, A.NUMBER) AS NUMBER
FROM
TABLEA A
FULL OUTER JOIN TABLEB B ON A.PROD = B.PROD
Now what I want is the statement to pick up the following information
Anything found in Table A but not in Table B
Anything found in Table B not in Table A
Anything in Table B as the master which is found in
in Table A
I added a full outer join as there maybe items in table B not in Table A
Will the query work, i have checked against out data and it seems to work however I am not sure if i have missed something.
Thanks

SQL Join table does not display results in SQL FIddle

I am trying to complete a project for school and the last step has me do a complex join between all of my tables. I have been going over this so many times, but I can't understand why it won't display the table when I run the SQL. Any help would be awesome.
you can use left join to test if system is able to find any record.
add left join instead of join so you can see where you are missing data it might possible that system was not able to find records from sub table that's why its empty.

SQL Combine two tables in select statement

I have a situation where I want to combine two tables for queries in a select statement, and I haven't found a working solution yet.
The Situation:
Table A
Table B
Both A and B have identical fields but distinct populations. I have other queries that are pulling from each table separately.
I want to build a query that pulls from them as if they were one table. There are no instances of records being in both tables.
My research so far led me to think the FULL OUTER JOIN was what I wanted, but I'm not entirely sure how to do that when I'm not really joining them on any field and it failed in my tests. So I searched for append options thinking that might more accurately represent what I'm trying to do and the INSERT INTO looked promising but less so for select statements. Is there a way to do this or do I need to create a third table that's the combination of the first two to query from?
.
This is being done as an Excel VBA query to Access via DAO. I'm building up SQL statements piece by piece in my VBA code based on user-selected options and then pulling the results into Excel for use. AS such my hope is to be able to only alter the FROM statement (since I'm building up the queries piecemeal) to effect this so that any other aspects of the select statement won't be impacted. Any suggestions or help will be much appreciated!
You can UNION the tables to do this:
SELECT StuffYouWant
FROM (SELECT *
FROM TableA
UNION ALL
SELECT *
FROM TableB) c
Something like this:
SELECT * FROM a
UNION
SELECT * FROM b
Make sure the a table and the b table have the same number of columns and the corresponding columns have the same data type

SAS SQL join criteria

I am working on a project where I have inherited an SQL Join that uses join
criteria in a format I have not seen before. The basic format of the join
is this:
Proc Sql;
create table mytest as
select t1.var1,
t1.var2,
t1.var3
from mysource1 t1
left join mysource2 t2 on
(t1.var1 = t2.var1), myparam t3;
quit;
The bit I am confused about is why myparam is included as a join
condition within the ON statement of the LEFT JOIN. The contents of
'myparam' is derived from the SAS Parameter File we have defined on our
system and contains just one row, with two columns. One contains month
start date, the other month end date.
None of the columns in this parameter file are in the other two source
tables and none of the columns in the parameter file appear in the final
output (they aren't referenced in the SELECT statement so they won't do).
I'm guessing that including the 'myparam' dataset in this context is
somehow using the date values within in it to cut the data in mysource1 and
mysource2, but could someone please provide confirmation that this is the
case and the exact mechanism at work please?
Thanks
This is an unusual construction for a join in SAS, but it's basically a Cartesian product. The myparam table isn't part of the LEFT JOIN condition but a new table, starting a new join. Any table included using a comma and no join condition causes it to be joined with all rows from one table matching to all rows in the other. This can be dangerous when two large tables are used (as the amount of rows is multiplied) but in your case the myparam table has one row, so it's only 1 x n.
However, saying all that, the query you have come across doesn't use any values from myparam (or mysource2 for that matter), so I don't see why these tables are being joined on at all. I'm fairly certain the following query would be equivalent:
proc sql;
select var1,var2,var3
from mysource1;
quit;
I'm aware this answer might come across as incomplete, so please feel free to comment...