Hello Everyone,
Please help to understand how i can write the store prodecure which will do following operations:-
suppose there are two tables named as table1 and table2 , the table 1 containting the old records but table2 containing fresh updated records. table2 is the replica of the table1 means having the same table staructure as table 1 having .
1) Now what we have to do is to compare the bothe records and list down all the records which exist in the table1(OLD TABLE) but not in table2(Updated table) .
2) once found the list of the records which are in the old table but not in new table, we have to set the status of those records as closed(which are not exist in the new table) .
Ramachandran MRank #2,660 · 522 reputationPosted Dec 4, 2019, 9:01 PM EST
DECLARE OldTableCursor CURSOR FOR
SELECT ID FROM OLDTABLE WHERE ID NOT INT (select NewTableID from NewTable)
OPEN OldTableCursor
FETCH NEXT FROM OldTableCursor
INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
update oldTable set STATUS='Closed' where ID = @ID
FETCH NEXT FROM OldTableCursor
INTO @ID
END
CLOSE OldTableCursor;
DEALLOCATE OldTableCursor;
Ramachandran MRank #2,660 · 522 reputationPosted Dec 4, 2019, 8:30 PM EST
(
SELECT CTE.ID FROM (
SELECT ID FROM OLDTABLE
EXCEPT
SELECT ID FROM NETABLE
)AS CTE
)
Ramachandran MRank #2,660 · 522 reputationPosted Dec 3, 2019, 11:26 PM EST
Ramachandran MRank #2,660 · 522 reputationPosted Dec 4, 2019, 6:17 PM EST
AS
BEGIN
--SELECT RECORDS TO VALIDATE
-- TABLE1 OLD TABLE AND TABLE2 NEW TABLE
SELECT * FROM TABLE1 WHERE ID NOT IN (SELECT ID FROM TABLE2);
UPDATE TABLE1 SET STATUS= CASE STATUS WHEN 'NEW' THEN 'CLOSED' ELSE STATUS END WHERE ID NOT IN (SELECT ID FROM TABLE2);
END
Ritendra MallRank #5,080 · 312 reputationPosted Dec 4, 2019, 7:57 PM EST
@Ramachandran M
Ritendra MallRank #5,080 · 312 reputationPosted Dec 4, 2019, 8:02 PM EST
Ramachandran MRank #2,660 · 522 reputationPosted Dec 4, 2019, 8:10 PM EST
if you are using triggers then execute below coding
ALTER TABLE your_table DISABLE TRIGGER [the_trigger_name] UPDATE TABLE1 SET STATUS= CASE STATUS WHEN 'NEW' THEN 'CLOSED' ELSE STATUS END WHERE ID NOT IN (SELECT ID FROM TABLE2); ALTER TABLE your_table ENABLE TRIGGER [the_trigger_name]Ritendra MallRank #5,080 · 312 reputationPosted Dec 4, 2019, 8:36 PM EST
Ramachandran MRank #2,660 · 522 reputationPosted Dec 4, 2019, 8:42 PM EST
Ritendra MallRank #5,080 · 312 reputationPosted Dec 4, 2019, 8:47 PM EST
Ramachandran MRank #2,660 · 522 reputationPosted Dec 4, 2019, 8:56 PM EST
Ramachandran MRank #2,660 · 522 reputationPosted Dec 4, 2019, 10:19 PM EST
Ritendra MallRank #5,080 · 312 reputationPosted Dec 4, 2019, 10:46 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts