Copy new table column entries between 2 databases and delete them after - sql-server-2000

I'm using an SQL Server 2000.
We have a live database with over 10gb of data. I want to create a new backup database which is was easy to do. so after creating my new backup database I want to create based on time (eg. 1 day) a query or a procedure to copy new data from live database to backup database.
Live_Database(Table1, Table2, Table3.....)
Backup_Database(Table1, Table2, Table3.....)
So, I need to update records on Backup_database(Table1, Table2, Table3.....) from Live_database(Table1, Table2, Table3.....).
After updating the records in Backup_Database(Table1, Table2, Table3.....) i want to delete records in Live_database(Table1, Table2, Table3.....) where record_id from table1 from Live_database is older than x days.

Related

Rows getting automatically deleted in Oracle database table

Rows for certain column value is automatically deleted within 30 sec from an Oracle database table. I have searched all schedular, procedures, and all_source nowhere I could find any delete or truncate statement against table.
There are other records as well in table but only records against certain column value are getting deleted.

How to combine identical tables into one table?

I have 100s of millions of unique rows spread across 12 tables in the same database. They all have the same schema/columns. Is there a relatively easy way to combine all of the separate tables into 1 table?
I've tried importing the tables into a single table, but given this is a HUGE size of files/rows, SQL Server is making me wait a long time as if I was importing from a flat file. There has to be an easier/faster way, no?
You haven't given much info about your table structure, but you can probably just do a plain old insert from a select, like below. The example would take all records that don't already exist Table2 and Table3, and insert them into Table1. You could do this to merge everything from all your 12 tables into a single table.
INSERT INTO Table1
SELECT * FROM Table2
WHERE SomeUniqueKey
NOT IN (SELECT SomeUniqueKey FROM Table1)
UNION
SELECT * FROM Table3
WHERE SomeUniqueKey
NOT IN (SELECT SomeUniqueKey FROM Table1)
--...
Do what Jim says, but first:
1) Drop (or disable) all indices in the destination table.
2) Insert rows from each table, one table at a time.
3) Commit the transaction after each table is appended, otherwise much disk space will be taken up in case of a possible rollback.
4) Renable or recreate the indices after you are done.
If there is a possibility of duplicate keys, you may need to retain an index on the key field and have a NOT EXISTS clause to hold back the duplicate records from being added.

Compare two database table rows and insert

I am working on a project where my requirement is just update the database from local server to the destination server (all tables, views, functions, rows and stored procedures).
Now I want to compare the local database table with the destination database table and insert the newly inserted rows from local database to the destination table.
E.g. : I have a database dbsource and dbDestination and both contain a table table1. Now I insert new rows into dbsource.table1.
Now I want to compare both database tables and insert the new rows into the destination table.
Please help me .
Why reinvent the wheel?? There are lots of commercial applications out there that already do this for you:
Red-Gate SQL Data Compare
ApexSQL Data Diff
Assuming both Table1 tables have a Primary Key (unique) column here's how you can implement that. I name the PK column ID:
INSERT INTO DBDESTINATION.<SCHEMA_NAME>.TABLE1
(SELECT T1.* FROM DBSOURCE.<SCHEMA_NAME>.TABLE1 AS T1
LEFT OUTER JOIN DBDESTINATION.<SCHEMA_NAME>.TABLE1 AS T2 ON T1.ID=T2.ID
WHERE T2.ID IS NULL)
Hope that helps.

One way syncing of 4 columns in SQL Server

This is my problem:
I have an old database, with no constraints whatsoever. There are a handful of tables I copy from the old database to the new database. This copy is simple, and I'm running that daily at night in a Job.
In my new database (nicely with constraints) I made all these loose tables in constraint with a main table. all these tables have a key made of 3ID's and a string.
My main table would translate these 3ID's and a string to 1 ID, so this table would have 5 columns.
In the loose tables, some records can be double, so to insert the ID's in the main table I'd take a distinct of the 3 ID's and a string, and insert those into my main table.
The tables in the old database are updated daily. the copy is run daily, and from the main table I'd like to make a one-to-many relation with the copied tables.
This gives me the problem:
how do I update the main table, do nothing with the already-inserted keys and add the new keys? Don't remove old keys if removed in old database.
I was thinking to make a distinct view of all keys in the old database, but how would I update this to the main table? This would need to run before the daily copy of the other tables (or it would fail on the constraints)
One other idea is to run this update of the main table in Linq-to-SQL in my website but that doesn't seem very clean.
So in short:
Old DB is SQL Server 2000
New DB is SQL Server 2008
Old db has no constraints, copy of some tables happens daily.
There should be a Main table, translating the 3ID and 1string key to a 1ID key, with a constraint to the other tables.
The main table should be updated before the copy job, else the constraints will fail. The main table will be a distinct of a few columns of 1 table. This distinct will be in a view on the old db.
Only new rows can be added in the main db
Does anyone have some ideas, some guidance?
Visualize the DB:
these loose tables are details about a company. 1 table has address(es) 1 table has contact person, another table has it's username and login for our system (which could be more than 1 for 1 company)
a company is identified by the 3ID's and 1string. the main table would list these unique ID's and string so that they could be translated to 1 ID. this 1 ID is then used in the rest of my DB. the one to many relation would then be made from the main table to all those loose tables. I hope this clears it up a bit :)
I think you could use EXCEPT to insert the ids that aren't in your main table yet http://msdn.microsoft.com/en-us/library/ms188055.aspx
So for example:
insert into MainTable
select Id1,Id2,Id3,String1,NewId from DistinctOldTable
except
select Id1,Id2,Id3,String1,NewId from MainTable

after running insert or update query, need the last inserted record and compare in vb.net sql server

i have 2 queries in vb.net with an if clause -
if x=0 then
insert into table1
else
update table1
both queries have 5 fields. now what i want to do is after this insert or update takes place, i need to look at this inserted/updated record and compare it with another table (table2). Especially for update, i have 5 fields in both tables. if any of the 5 fields dont match with table2, then i insert a new record in table 2 which is the updated record in table 1.
how can i do this?
You could use a trigger and keep this all on the database.