Comparing if a stack of data exists in two different tables - sql

im new to Oracle and sql but I was assigned this job and I hope someone can help me out with this one.
Basically I am given a database link to connect to a remote database and I extract some information from a single table in there and a few other tables from a local database, and then process it and insert it into a table in the local database. I`ve managed to do this succesfully but now I need a way to confirm that all of the data from the remote database was actually copied into the local database. How would I go about doing this?
This is the code I have to insert the information to my local db.
INSERT INTO kcrt_requests_int RI
RI.TRANSACTION_ID,
RI.DESCRIPTION,
RI.CREATED_USERNAME,
RI.REQUEST_TYPE_ID,
RI.STATUS_ID,
RI.WORKFLOW_ID,
RI.WORKFLOW_STEP_ID,
RI.RELEASED_FLAG,
RI.USER_DATA1,
RI.USER_DATA2,
RI.USER_DATA3,
RI.USER_DATA4,
RI.USER_DATA7)
SELECT
KCRT_TRANSACTIONS_S.NEXTVAL,
RD.PARAMETER13||' '||R.DESCRIPTION,
'[SYS.USERNAME]',
'0001',
'31876',
'34987', '1234',
'Y',
PP.PROJECT_ID,
VP.REVIEWDATE,
RD.PARAMETER9,
R.REQUEST_ID,
RD.PARAMETER13
FROM
KCRT_REQUEST_TYPES_NLS RT,
KCRT_REQUESTS R,
KCRT_REQUEST_DETAILS RD,
v_projects#XXXXX VP,
PM_PROJECTS PP
WHERE
R.REQUEST_TYPE=RT.REQUEST_TYPE_ID
AND R.REQUEST_ID=RD.REQUEST_ID
AND RD.BATCH_NUMBER=1
AND RT.REQUEST_TYPE_NAME 'AAAAA'
AND R.STATUS_CODE = 'BBBBB'
AND RD.PARAMETER13 = to_char(VP.IDBANK)
AND VP.REVIEWDATE=(SELECT MAX (VP.REVIEWDATE) FROM v_projects#XXXXX VP)
AND R.REQUEST_ID=PP.PFM_REQUEST_ID
AND RD.BATCH_NUMBER=1
So pretty much I will try to compare RI.USER_DATA7 to VP.IDBANK and see if KCRT_REQUESTS_INT has every row that v_projects#XXXXX has.
Thanks for any help!

If there is a unique identifier column which is defined as primary key.if yes,you can join both tables on Primary key and see if the count matches with count without join on source table.
assumes ,Table A is your source and Table B is where you have loaded data. let P_key be primary key column.
You can match:
select count(1)
from Table_A with
select count (1)
from Table_A,Table_B
where Table_A.P_key=Table_B.P_Key
If they match ,you have all the records. Hope this helps.

Related

Can i copy data from one database table to another already existing database table sql server?

So i have 2 very similar databases, they are identical except for the data that exists in the tables. I want to copy the data from the EQUIP_MODEL table that exists in the PILOT database to the EQUIP_MODEL table that exists in the DOMAIN database.
Is this even possible? or do i have to do manual inserts for all the data?
You can use fully qualified names in Insert statement
INSERT INTO DOMAIN.SCHEMANAME.EQUIP_MODEL (col1,col2,col3...)
SELECT col1,col2,col3.. FROM PILOT.SCHEMANAME.EQUIP_MODEL
To get the foreign key values (not the exact code you have to alter based on column name and mapping)
INSERT INTO DOMAIN.SCHEMANAME.EQUIP_MODEL
(id,col2,col3)
SELECT sp.id,
col2,
col3
FROM PILOT.SCHEMANAME.EQUIP_MODEL em
JOIN PILOT.SCHEMANAME.Prent_table p
ON em.id = p.id
JOIN DOMAIN.SCHEMANAME.parent_table sp
ON sp.somename_number_col = p.somename_number_col

Adding Row in existing table (SQL Server 2005)

I want to add another row in my existing table and I'm a bit hesitant if I'm doing the right thing because it might skew the database. I have my script below and would like to hear your thoughts about it.
I want to add another row for 'Jane' in the table, which will be 'SKATING" in the ACT column.
Table: [Emp_table].[ACT].[LIST_EMP]
My script is:
INSERT INTO [Emp_table].[ACT].[LIST_EMP]
([ENTITY],[TYPE],[EMP_COD],[DATE],[LINE_NO],[ACT],[NAME])
VALUES
('REG','EMP','45233','2016-06-20 00:00:00:00','2','SKATING','JANE')
Will this do the trick?
Your statement looks ok. If the database has a problem with it (for example, due to a foreign key constraint violation), it will reject the statement.
If any of the fields in your table are numeric (and not varchar or char), just remove the quotes around the corresponding field. For example, if emp_cod and line_no are int, insert the following values instead:
('REG','EMP',45233,'2016-06-20 00:00:00:00',2,'SKATING','JANE')
Inserting records into a database has always been the most common reason why I've lost a lot of my hairs on my head!
SQL is great when it comes to SELECT or even UPDATEs but when it comes to INSERTs it's like someone from another planet came into the SQL standards commitee and managed to get their way of doing it implemented into the final SQL standard!
If your table does not have an automatic primary key that automatically gets generated on every insert, then you have to code it yourself to manage avoiding duplicates.
Start by writing a normal SELECT to see if the record(s) you're going to add don't already exist. But as Robert implied, your table may not have a primary key because it looks like a LOG table to me. So insert away!
If it does require to have a unique record everytime, then I strongly suggest you create a primary key for the table, either an auto generated one or a combination of your existing columns.
Assuming the first five combined columns make a unique key, this select will determine if your data you're inserting does not already exist...
SELECT COUNT(*) AS FoundRec FROM [Emp_table].[ACT].[LIST_EMP]
WHERE [ENTITY] = wsEntity AND [TYPE] = wsType AND [EMP_COD] = wsEmpCod AND [DATE] = wsDate AND [LINE_NO] = wsLineno
The wsXXX declarations, you will have to replace them with direct values or have them DECLAREd earlier in your script.
If you ran this alone and recieved a value of 1 or more, then the data exists already in your table, at least those 5 first columns. A true duplicate test will require you to test EVERY column in your table, but it should give you an idea.
In the INSERT, to do it all as one statement, you can do this ...
INSERT INTO [Emp_table].[ACT].[LIST_EMP]
([ENTITY],[TYPE],[EMP_COD],[DATE],[LINE_NO],[ACT],[NAME])
VALUES
('REG','EMP','45233','2016-06-20 00:00:00:00','2','SKATING','JANE')
WHERE (SELECT COUNT(*) AS FoundRec FROM [Emp_table].[ACT].[LIST_EMP]
WHERE [ENTITY] = wsEntity AND [TYPE] = wsType AND
[EMP_COD] = wsEmpCod AND [DATE] = wsDate AND
[LINE_NO] = wsLineno) = 0
Just replace the wsXXX variables with the values you want to insert.
I hope that made sense.

access 2010 append data into table without overwriting existing records

Sorry for the long question/post but need some help as I've been searching for several days but havent found anything that helps. Seems like it should be easy but..here goes
I have table1 in my (Access 2010) database that has exising records. I have another table2 that after I run a query, it first deletes the data in table 2, then imports new records into that table. I run the import into table 2 on a semi regular basis but have yet to copy all those records into table 1 successfully.
I need to copy only the records from table 2 to table 1 if the records don't already exist in table1. So, each time the query or vba code would run, it would be continuing to grow table 1 without duplicating existing data.
To clarify further, it's data from the Outlook GAL so each time table2 imports that data (lname,fname,phone,email) it needs to be added to table1, but only if it doesn't already exist in table 1.
I have a small start of SQL but cannot get it to work properly because I'm not sure how to add the other fields into this SQL statement properly (unfortunately I don't know a whole lot about SQL or creating an append query):
INSERT INTO [Current] ( FirstName )
SELECT DISTINCT tblglobaladdresslistimport.First
FROM tblglobaladdresslistimport LEFT JOIN [Current] ON tblglobaladdresslistimport.First = Current.FirstName
WHERE Current.FirstName Is Null;
How about this :
INSERT INTO [Current](FirstName, LastName, Phone, Email)
SELECT DISTINCT
tblglobaladdresslistimport.First
, tblglobaladdresslistimport.Last
, tblglobaladdresslistimport.Phone
, tblglobaladdresslistimport.Email
FROM
tblglobaladdresslistimport LEFT JOIN [Current]
ON tblglobaladdresslistimport.First = Current.FirstName
AND tblglobaladdresslistimport.Last = Current.LastName
AND tblglobaladdresslistimport.Phone = Current.Phone
AND tblglobaladdresslistimport.Email = Current.Email
WHERE Current.FirstName Is Null
AND Current.LastName Is Null
AND Current.Phone Is Null
AND Current.Email Is Null;
Adjust column names if I guessed it wrong. That assumed that you don't have primary key, so data in tblglobaladdresslistimport considered already exists if there is a row in Current having same value for all columns.

Removing duplicate account names from an SQLite database (accidental case sensitivity created some duplicates)

I already searched around for this question but couldn't really find a good answer.
I run dev multiplayer game server mod and I made a huge mistake with the accounts database at first: case sensitivity, so if a player joined with the name 'bob' and 'Bob' was already in the database, a new account was registered (this caused problems!) There's also no 'PRIMARY KEY' column on the database.
So now there's quite a few 'bob', 'Bob', 'BOB', etc duplicates in my database and was wondering if there's a nice way I can clean up the database and remove these duplicates.
The only way I could think of would involve running a loop over the database to store each name in memory (12k records) then running over it again and marking names as "found" then deleting records that were already marked as "found".
Anyway, thanks in advance for any help :)
Insert the result of this into another table then you will not have duplicates
SELECT DISTINCT UPPER(name) FROM table
This is how you do this in sqlite using a simple delete statement:
delete from test where rowid in
( select test.rowid from test
inner join (select upper(name) name, min(rowid) rowid from test group by upper(name)) t
on upper(test.name) = t.name and test.rowid <> t.rowid);
SQLFiddle
I would copy all data to a new table, ignoring duplicates:
ALTER TABLE players RENAME TO players_old;
CREATE TABLE players (name TEXT UNIQUE NOT NULL COLLATE NOCASE, ...);
INSERT OR IGNORE INTO players SELECT * FROM players_old;
This way you will end up with a new table with unique names and a old table with your previous data.

Select date which is bigger than another date on a second afinity

I have two identical tables in 2 different DBs ( one local and one remote ). Both of them have a Datetime ( or Datetime2 or anything else, I can still change that ) column which specifies when they were added. What I want is to get the records from the remote table to the local table that have not already been brought.
For this a get the max value from the local table, then I tried to put the condition WHERE remoteTable.CreateTime > maxLocalValue, but I don't know why this seems to overwrite some records on the boundry. Any better way to do this ? On a second or minute level ?
since we know which values have been added already to the local table, only add the values from the remote table that are not in the local table. Let local_table repersents the local table and remote_table repersents remote table and that rcd_1 is a column in both tables that repersents the DATE type.
INSERT INTO local_table (col_1,.....(how evermany columns you need), rcd_1)
SELECT rcd_1 FROM remote_table
where remote_table.rcd_1 <> local_table.rcd_1;
the way you explained you question would suggest that only newer dates are added to local from remote so as long as this hold true you should get the desired results.