Click here to Skip to main content
15,890,947 members
Home / Discussions / Database
   

Database

 
AnswerRe: Problem with SQL Pin
Blue_Boy3-Jul-09 4:55
Blue_Boy3-Jul-09 4:55 
AnswerRe: Problem with SQL Pin
Niladri_Biswas3-Jul-09 18:45
Niladri_Biswas3-Jul-09 18:45 
QuestionCheck Which rows are changed Pin
avi_dadi20023-Jul-09 0:38
avi_dadi20023-Jul-09 0:38 
AnswerRe: Check Which rows are changed Pin
Henry Minute3-Jul-09 1:59
Henry Minute3-Jul-09 1:59 
GeneralRe: Check Which rows are changed Pin
avi_dadi20023-Jul-09 9:28
avi_dadi20023-Jul-09 9:28 
GeneralRe: Check Which rows are changed Pin
Henry Minute3-Jul-09 9:50
Henry Minute3-Jul-09 9:50 
GeneralRe: Check Which rows are changed Pin
avi_dadi20023-Jul-09 20:12
avi_dadi20023-Jul-09 20:12 
AnswerRe: Check Which rows are changed Pin
Niladri_Biswas4-Jul-09 2:17
Niladri_Biswas4-Jul-09 2:17 
Hi Avinash,

If I have understood your problem correctly, initially you are having two tables say tblX, tblY.

Initially, tblX and tblY will have the same values , with the exception of the Col4(exclusively for tblY)

i.e.

Col1 Col2 Col3 Col4
1 1234 4781258 0
2 1234 4781258 0
3 1234 4781258 0
4 1234 4781258 0
5 1234 4781258 0
6 1234 4781258 0
7 1234 4781258 0
8 5487 5124873 0
9 5487 5124873 0
10 5487 5124873 0

Now if we change any value of Col3 for tblX, then next time when we want to update tblY, Col4 of tblY will change to 1 and ofcourse Col3(of tblY).

i.e. if I change the 3rd , 5th and 10th row's Col3 values to say 3,5,10

respectively of tblX, then in tblY the values will be

Col1 Col2 Col3 Col4
1 1234 4781258 0
2 1234 4781258 0
3 1234 3 1
4 1234 4781258 0
5 1234 5 1
6 1234 4781258 0
7 1234 4781258 0
8 5487 5124873 0
9 5487 5124873 0
10 5487 10 1

Whereas, Col1 and Col2 will always be the same for both the tables.

If this assumption is correct, then this is the stored procedure I wrote for you

ALTER PROCEDURE dbo.UpdateTableY
	-- Add the parameters for the stored procedure here
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    
	DECLARE @MYCURSOR CURSOR
	DECLARE @ROWID INT
	DECLARE @COL2_X INT
	DECLARE @COL3_X INT
	DECLARE @COL2_Y INT
	DECLARE @COL3_Y INT
    
	
	SET @MYCURSOR = CURSOR FOR

	SELECT A.ROWID,A.COL2,A.COL3
    FROM (SELECT 
				ROW_NUMBER() OVER (ORDER BY COL1) AS ROWID,
				COL2 , COL3 FROM tblX) A 

	OPEN @MYCURSOR
	FETCH NEXT
	FROM @MYCURSOR INTO @ROWID,@COL2_X,@COL3_X

	WHILE @@FETCH_STATUS = 0

	BEGIN

		-- GET THE COL2 VALUES BASED ON ROW ID'S FROM tblY
		SELECT @COL2_Y = COL2 
		FROM tblY
		WHERE COL1 = @ROWID

		--MATCH THE COL2 RECORDS OF BOTH TABLES
		IF(@COL2_Y = @COL2_X)

         BEGIN 
				-- GET THE COL3 VALUES BASED ON ROW ID'S FROM tblY
				SELECT @COL3_Y = COL3 
				FROM tblY
				WHERE COL1 = @ROWID

				--IF A MISMATCH BETWEEN THE COL3 RECORDS OF BOTH TABLES
				-- THEN UPDATE COL4 OF TABLE Y
				IF(@COL3_Y <> @COL3_X)

				BEGIN

					UPDATE tblY

					SET COL3 = @COL3_X,
						COL4 = 1

					WHERE COL1 = @ROWID
				 
				END

		 END	

	FETCH NEXT
	FROM @MYCURSOR INTO @ROWID,@COL2_X,@COL3_X

	END

	CLOSE @MYCURSOR
	DEALLOCATE @MYCURSOR
	
END
GO


It will work as per the logic I depicted to you earlier.

Hope this helps.
Smile | :)

Niladri Biswas

QuestionHow to modify the SP in case of multiple row data into single row Pin
Krishna Aditya2-Jul-09 23:44
Krishna Aditya2-Jul-09 23:44 
AnswerRe: How to modify the SP in case of multiple row data into single row Pin
Niladri_Biswas3-Jul-09 6:24
Niladri_Biswas3-Jul-09 6:24 
GeneralRe: How to modify the SP in case of multiple row data into single row Pin
Krishna Aditya9-Jul-09 3:50
Krishna Aditya9-Jul-09 3:50 
GeneralLike Command in SQL Server 2005 Pin
Isaac Gordon2-Jul-09 23:24
Isaac Gordon2-Jul-09 23:24 
GeneralRe: Like Command in SQL Server 2005 Pin
Eddy Vluggen3-Jul-09 1:17
professionalEddy Vluggen3-Jul-09 1:17 
GeneralRe: Like Command in SQL Server 2005 Pin
David Skelly3-Jul-09 2:17
David Skelly3-Jul-09 2:17 
GeneralRe: Like Command in SQL Server 2005 Pin
Niladri_Biswas3-Jul-09 7:15
Niladri_Biswas3-Jul-09 7:15 
GeneralRe: Like Command in SQL Server 2005 Pin
Isaac Gordon3-Jul-09 19:08
Isaac Gordon3-Jul-09 19:08 
GeneralRe: Like Command in SQL Server 2005 Pin
Niladri_Biswas4-Jul-09 4:49
Niladri_Biswas4-Jul-09 4:49 
Questiontrigger on system tables Pin
sunit_822-Jul-09 22:54
sunit_822-Jul-09 22:54 
AnswerRe: trigger on system tables [modified] Pin
Niladri_Biswas3-Jul-09 17:29
Niladri_Biswas3-Jul-09 17:29 
GeneralRe: trigger on system tables Pin
sunit_825-Jul-09 22:39
sunit_825-Jul-09 22:39 
QuestionHow SQL View can be used in this scenario Pin
Krishna Aditya2-Jul-09 22:29
Krishna Aditya2-Jul-09 22:29 
AnswerRe: How SQL View can be used in this scenario Pin
Krishna Aditya2-Jul-09 22:41
Krishna Aditya2-Jul-09 22:41 
AnswerRe: How SQL View can be used in this scenario Pin
David Skelly3-Jul-09 2:26
David Skelly3-Jul-09 2:26 
GeneralRe: How SQL View can be used in this scenario Pin
Krishna Aditya3-Jul-09 2:58
Krishna Aditya3-Jul-09 2:58 
GeneralRe: How SQL View can be used in this scenario Pin
David Skelly3-Jul-09 5:57
David Skelly3-Jul-09 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.