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


Validate rows in 2 tables and write log


Validate rows in 2 tables and write log

Author
Message
User7766
User7766
Old Hand
Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)

Group: General Forum Members
Points: 384 Visits: 226
Hi,

I'm having 2 tables with old and new project numbers, If project number in Table A matches with Project Number in Table B, Then i need to update the Table A project number with its equivalent New project number located in Table B.

For that i;ve written the below query which worked fine, But now my requirement has been changed to:

I need to iterate through all the rows in Table A and Table B, If match found i need to INSERT a record in a new table which should store Project number(Varchar), IsProjNumConverted (Bit (0,1))

How can i do this using CASE statement>?

UPDATE
dbo.Incident_Info
SET
dbo.Incident_Info .Proj_ID = x.INVC_PROJ_ID
FROM
dbo.Incident_Info i
left outer join Repository.dbo.PROJ_ID_XREF x on x.PROJ_ID=i.Proj_ID



Thanks,
sandeep rawat
sandeep rawat
Right there with Babe
Right there with Babe (797 reputation)Right there with Babe (797 reputation)Right there with Babe (797 reputation)Right there with Babe (797 reputation)Right there with Babe (797 reputation)Right there with Babe (797 reputation)Right there with Babe (797 reputation)Right there with Babe (797 reputation)

Group: General Forum Members
Points: 797 Visits: 376
use merge statement to achieve the required .

For more detail


http://technet.microsoft.com/en-us/library/bb510625.aspx


or
http://blog.sqlauthority.com/2010/06/08/sql-server-merge-operations-insert-update-delete-in-single-execution/
rVadim
rVadim
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1421 Visits: 2308
So, you need to compare TableA and TableB and only if match is found insert into TableC ? Match on what? Old and Old? New and New? Both?
How do you determine value of the IsProjNumConverted flag?
thermanson
thermanson
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 287
I haven't tested this since there was no test data supplied, but I think this should work.


Insert into MergeTable (Project number, IsProjNumConverted)
Select Coalesce(x.proj_id,i.proj_id) as projID,
Case
When x.proj_id is not null then 1
else 0
End Converted
FROM dbo.Incident_Info i
left outer join Repository.dbo.PROJ_ID_XREF x on x.PROJ_ID=i.Proj_ID


User7766
User7766
Old Hand
Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)

Group: General Forum Members
Points: 384 Visits: 226
thermanson (8/27/2012)
I haven't tested this since there was no test data supplied, but I think this should work.


Insert into MergeTable (Project number, IsProjNumConverted)
Select Coalesce(x.proj_id,i.proj_id) as projID,
Case
When x.proj_id is not null then 1
else 0
End Converted
FROM dbo.Incident_Info i
left outer join Repository.dbo.PROJ_ID_XREF x on x.PROJ_ID=i.Proj_ID



Hi,

The query is working fine, But i need to display the project number even though there is no match found with project id in table b. The result set is coming like below when i execute your query,

01779.124.034 Yes
12682.004.006 Yes
NULL No
12682.004.006 Yes
NULL No
NULL No
NULL No
NULL No

Only for matched records i can see the project number, But i also need to see Project Number for NO case also. How can i acheive this?

Thanks
User7766
User7766
Old Hand
Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)

Group: General Forum Members
Points: 384 Visits: 226
I need to write both the cases,

If match found in table A and table B, Then i need to write the output like

Table A ProjNumber Table B ProjNumber IsMatchFound

1234 1234 Y
1234 0000 N

I need to trace for which project numbers, there was no match found..

Please help me out..

Thanks
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34453 Visits: 16649
If you can't or won't correct your design as suggested by Joe, use the MERGE statement as suggested by Sandeep.

John
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