HERE I WILL TRY TO SYNC THE TARGET TABLE WITH SOURCE BY USING A PROCEDURE
1. FOR THAT I WILL CREATE TWO TEMP TABLES ONE FOR SOURCE AND ANOTHER FOR TAGET ...
CREATE TABLE #T(ID INT ,NAME VARCHAR(50))
CREATE TABLE #S(ID INT ,NAME VARCHAR(50))
2. I WILL WRITE A PROCEDURE TO INSERT,UPDATE,DELETE IN THE TARGET BASED ON THE SOURCE
MERGE #T AS T
USING #S AS S ON S.ID=T.ID
WHEN NOT MATCHED BY TARGET THEN
INSERT(ID,NAME)VALUES(S.ID,S.NAME)
WHEN MATCHED THEN
UPDATE SET NAME=S.NAME
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
SELECT * FROM #S
SELECT * FROM #T
1. FOR THAT I WILL CREATE TWO TEMP TABLES ONE FOR SOURCE AND ANOTHER FOR TAGET ...
CREATE TABLE #T(ID INT ,NAME VARCHAR(50))
CREATE TABLE #S(ID INT ,NAME VARCHAR(50))
2. I WILL WRITE A PROCEDURE TO INSERT,UPDATE,DELETE IN THE TARGET BASED ON THE SOURCE
CREATE PROC USP_SOURCE_TARGET
AS
BEGIN
INSERT INTO #T
SELECT * FROM #S WHERE ID NOT IN(SELECT ID FROM #T)
UPDATE B SET NAME=A.NAME FROM #S A INNER JOIN #T B ON A.ID=B.ID
WHERE B.NAME<>A.NAME
DELETE FROM #T WHERE ID NOT IN (SELECT ID FROM #S)
END
3. I WILL INSERT,UPDATE,DELETE IN THE SOURCE TABLE MANUALLY AND THEN EXECUTE THE PROC THEN INSERT ,DELETE AND UPDATE WILL AUTOMATICALLY INTO THE TARGET ALSO
INSERT INTO #S VALUES (3,'MANJU1'),(4,'HARI')
UPDATE #S SET NAME='HHH' WHERE ID=2
DELETE FROM #S WHERE ID=2
UPDATE #S SET NAME='HHH' WHERE ID=2
DELETE FROM #S WHERE ID=2
EXEC USP_SOURCE_TARGET
IN THE SAME WAY I USED MERGE TO DO THE ABOVE THINGS
MERGE #T AS T
USING #S AS S ON S.ID=T.ID
WHEN NOT MATCHED BY TARGET THEN
INSERT(ID,NAME)VALUES(S.ID,S.NAME)
WHEN MATCHED THEN
UPDATE SET NAME=S.NAME
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
SELECT * FROM #S
SELECT * FROM #T
No comments:
Post a Comment