SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Compare two Table data and insert changed field to the third table


Compare two Table data and insert changed field to the third table

Author
Message
F/-\R//-\Z
F/-\R//-\Z
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 18
hi
I want Compare two Table data and insert changed field to the third table
who can help me
thanks a lot:-)Hehe
John Rowan
John Rowan
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6226 Visits: 4551
Can you give us table DDL for your 3 tables as well as some sample data for the two that you need to compare?

John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
F/-\R//-\Z
F/-\R//-\Z
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 18
yes I want get changed raw in new table

my 3 table struc. is:

CREATE TABLE [dbo].[BedriddenBed2](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[ReferralCode] [char](8) NOT NULL,
[BedDate] [char](10) NOT NULL,
[BedTime] [char](8) NOT NULL,
[BedCode] [char](5) NULL,
[BedDays] [numeric](18, 0) NULL,
[UserCode] [char](7) NULL,
[ISVirtual] [bit] NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[BeneficiaryName] [nvarchar](50) NULL,
[ReferralDate] [char](10) NULL,
CONSTRAINT [PK_BedriddenBed2] PRIMARY KEY CLUSTERED
(
[ReferralCode] ASC,
[BedDate] ASC,
[BedTime] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15450 Visits: 18622
F/-\R//-\Z (8/12/2014)
hi
I want Compare two Table data and insert changed field to the third table
who can help me
thanks a lot:-)Hehe


While I suspect that the requirements might possibly need refining, here is my initial interpretation.
Cool
USE tempdb;
GO
/*
Select records from the first table that do not match
the second table and insert the results into the
third table.
*/
INSERT INTO dbo.BedriddenBed3
(
[ReferralCode]
,[BedDate]
,[BedTime]
,[BedCode]
,[BedDays]
,[UserCode]
,[ISVirtual]
,[FirstName]
,[LastName]
,[BeneficiaryName]
,[ReferralDate]

)
SELECT
B1.[ReferralCode]
,B1.[BedDate]
,B1.[BedTime]
,B1.[BedCode]
,B1.[BedDays]
,B1.[UserCode]
,B1.[ISVirtual]
,B1.[FirstName]
,B1.[LastName]
,B1.[BeneficiaryName]
,B1.[ReferralDate]
FROM dbo.BedriddenBed1 B1
INNER JOIN dbo.BedriddenBed2 B2
ON B1.ReferralCode = B2.ReferralCode
AND B1.BedDate = B2.BedDate
AND B1.BedTime = B2.BedTime
WHERE B1.[BedCode] <> B2.[BedCode]
OR B1.[BedDays] <> B2.[BedDays]
OR B1.[UserCode] <> B2.[UserCode]
OR B1.[ISVirtual] <> B2.[ISVirtual]
OR B1.[FirstName] <> B2.[FirstName]
OR B1.[LastName] <> B2.[LastName]
OR B1.[BeneficiaryName] <> B2.[BeneficiaryName]
OR B1.[ReferralDate] <> B2.[ReferralDate];


Phil Parkin
Phil Parkin
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19001 Visits: 20461
F/-\R//-\Z (8/12/2014)
hi
I want Compare two Table data and insert changed field to the third table
who can help me
thanks a lot:-)Hehe


So all three tables have the same structure?

And table three should contain all of the rows in table 2 which either

a) Do not have a matching row in table one, or
b) Have one or more columns whose value is different.

Is that correct?

In future, please provide sample data as well as DDL. Also you should provide desired results, based on your sample data. This avoids us having to play 'guess the requirement', as we are doing here.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
F/-\R//-\Z
F/-\R//-\Z
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 18
hi
I try your code but this is not work!!!
no detect deferent row!
F/-\R//-\Z
F/-\R//-\Z
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 18
I want do this in picture


Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15450 Visits: 18622
F/-\R//-\Z (8/17/2014)
hi
I try your code but this is not work!!!
no detect deferent row!


In a way that is not too bad, we just have to figure out why;-)
Cool
Now I have few questions:
1. Do you have some sample data we can use?
2. Does the code have to detect missing rows?
3. If 2 = Yes, in which tables?
4. Can you also answer Phil's questions?
F/-\R//-\Z
F/-\R//-\Z
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 18
hi and thanks

answer of Eirikur's questions

1. Do you have some sample data we can use?
I'm sorry no I do not have
2. Does the code have to detect missing rows?
no code is not detected missing rows(table 3 row sending to our hardware for see this information on LED board)
3. If 2 = Yes, in which tables?
4. Can you also answer Phil's questions?

answer of Phil's questions

a) Do not have a matching row in table one, or?
no do not matching row in table 1, [BedCode] is unique
b) Have one or more columns whose value is different.
i need if field of [FirstName] [LastName] [BeneficiaryName] [ReferralDate] and age is changed
i copy row to table 3
avijit2988
avijit2988
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 184
You may try using Merge statement
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search