473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

StoredProcedure help

Hi All,

I am migrating the data from one database to other database.
The tables are similar in structure in both databases.
After the initial migration,if any changes made in a table of database 1,these changes should be updated in similar table of other database.
I need to write a stored procedure that determines the changes made in the data of a table of one databse and load the changes in similar table in the other database.

So how to find the changes made in the data of a table.
Mar 15 '07 #1
4 1136
iburyak
1,017 Expert 512MB
1. You can write a trigger that will instantly update, insert, delete records on other server.

If two servers are linked it is as easy as on the same server. If not let me know I will give you a solution on how to access one server from another.

2. If you have the same unique ID on both tables you can use it to insert\update\delete like this. Assume you have tables A and B


[PHP]delete from B where ID not in (select ID from A)

update B set
col1 = a.col1,
col2 = a.col2
from A a
JOIN B b on a.ID = b.ID

insert into B Select * from A where ID not in (select ID from B)[/PHP]
Mar 15 '07 #2
mivey4
37
1. You can write a trigger that will instantly update, insert, delete records on other server.

If two servers are linked it is as easy as on the same server. If not let me know I will give you a solution on how to access one server from another.

2. If you have the same unique ID on both tables you can use it to insert\update\delete like this. Assume you have tables A and B


[PHP]delete from B where ID not in (select ID from A)

update B set
col1 = a.col1,
col2 = a.col2
from A a
JOIN B b on a.ID = b.ID

insert into B Select * from A where ID not in (select ID from B)[/PHP]
What type of changes do you want to track on the second database table?
Inserts, Updates, Deletes?

Are you attempting to create permanent redundancy between both of the
systems? If so, you may want to explore other options.

To expand on what iburyak has already recommended here is an
Example of a Trigger for Inserts -

CREATE TRIGGER tr_trackInserts ON tableOne
FOR INSERT
AS
INSERT INTO Database2.dbo.tableTwo (columnOne, columnTwo, columnThree)
SELECT inserted.columnOne, inserted.columnTwo, insert.columnThree
FROM Database2.dbo.inserted
WHERE BLAH BLAH.... = BLAH BLAH
GO

Try this link and Research triggers:
http://www.sqlteam.com/item.asp?ItemID=3850
Mar 15 '07 #3
Hi,

I need to keep the values sync in both tables .This means that when a mismatch is detected in phase 1,the new records in table 1(phase 1) will be migrated over table 2(Phase 2).

update tst_atr_sd_p2s.dbo.csdAttributeListDetail
set aldAttributeRef=a.aldAttributeRef,
aldAttrValueRef=a.aldAttrValueRef,
aldProductRef=a.aldProductRef ,
aldSeasonRef=a.aldSeasonRef
from tst_atl_sd1.dbo.csdAttributeListDetail a
inner join tst_atr_sd_p2s.dbo.csdAttributeListDetail b
on b.aldAttributeRef=a.aldAttributeRef and
b.aldAttrValueRef=a.aldAttrValueRef and
b.aldProductRef=a.aldProductRef and
b.aldSeasonRef=a.aldSeasonRef

tst_atr_sd_p2s is the phase 2 database and tst_atl_sd1 is phase 1 database.
The primary key in csdAttributeListDetail in this table is all 4 columns mentioned in query. And the table has approx 60k rows.When i do a join on the tables its taking much time.

IS there any other alternative way to do this in efficient way.
Mar 21 '07 #4
iburyak
1,017 Expert 512MB
You can write a trigger over table 1 which will update table 2 at insert\update time. This way while you update table 1 table 2 will be updated as well.

To speed up such small table just put index over primary key column.
Mar 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: PeteZ | last post by:
what using . . . . statement do I need to get this value (CommandType.StoredProcedure) defined ? even when I have defined the using statements: using System; using System.Data.SqlClient;...
4
by: Brian Parker | last post by:
I've been banging my head on this for hours and it's something that should be very very easy. I have a Stored Procedure in a MS-SQL 2000 server that returns a Varchar(40) and I want to grab that...
0
by: Amol | last post by:
Hello, I am developing an apllication in which i want to change the values of datagrid checkbox column according to parameter passed to the query. But the problem is that, at first time when i...
9
by: Frank | last post by:
Hi, imagine there's a WEB application reading data from an Oracle database to visualize in using DataGrids in the clients browser. Yes, sounds simple, just create OracleConnection + OracleCommand...
3
by: jed | last post by:
I need to pass a value that is inserted by the user in a textbox to a variable in my stored procedure. please help
2
by: anoopgopal007 | last post by:
Hi friends... I have one problem... I am using C#, and CrystalReport9. In my project I used StoredProcedure with one input parameter. ...
1
by: phanimadhav | last post by:
Hello friends,I have one small doubt.Acually myclient asking by using tableAdapetr call the MySQL procedures.How to call the MySQL storedprocedure in TableAdapter.In the Table adapter contain one...
1
by: Tony Johansson | last post by:
Hello! Is it possible to use a StoredProcedure when you have SqlDataAdapter or is it only possible when you have SqlCommand like SqlCommand thisCommand = myConnection.CreateCommand;...
0
by: shivakrishna | last post by:
hi i am having 50 storedprocedures , i want know the names of tables and views used in the storedprocedure so can any one help me in this regard
1
by: shivakrishna | last post by:
how to get the list of all tables and views used in storedprocedure can any one help in this issue
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.