Help with T SQL statement

I have very limited Microsoft SQL experience but need an efficient way to do this.

Table1
field1 field2 field3

Table2
field1 field2 field4

I want to work through table1 - first to last
check table2 for match on table1.field1 = table2.field1 and table2.field4 = 'Y'
if no match
      create new record in table2
if match and table1.field2 <> table2.field2
      update table2.field4
      create new record in table2

      
      Any help appreciated
simonsjdIT ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, not sure about your exact logic, but take a look at MERGE, e.g.

MERGE Table2 Dst
USING (   SELECT T.Column1 ,
                 T.Column2 ,
                 T.Column3
          FROM   Table1 T ) Src
ON (   Dst.Column1 = Src.Column1
       AND Dst.Column2 != SrcColumn2
       AND Dst.Column4 = 'Y' )
WHEN MATCHED THEN UPDATE SET Dst.Column4 = '?'
WHEN NOT MATCHED THEN INSERT ( Column1 ,
                               Column2 ,
                               Column4 )
                      VALUES ( Src.Column1, Column2, '?' )
OUTPUT DELETED.* ,
       $ACTION ,
       INSERTED.*;

Open in new window

0
MlandaTCommented:
      update table2.field4
      create new record in table2

Open in new window

This bit is not very clear on your question. Are you doing BOTH? An update and creating a new record on Table2? What is the update going to be? Using MERGE, you will not be able to execute both. However, you could use the MERGE to perform the inserts, OUTPUT these results into a temporary table, and then execute the updates based on the temporary table. Something like:
-- Create a temporary table variable to hold the output actions.  
DECLARE @changes TABLE(Change VARCHAR(20), T2RecordID, T2Field1, T2Field2, T2Field4, T2OldField4  );

MERGE Table2 as target
USING Table1 as source
ON ( target.field1 = source.field1 AND target.Column4 = 'Y' )
WHEN NOT MATCHED THEN INSERT ( fiel1, field2, field4 )
                      VALUES ( source.field1, source.field2, 'Y' )
WHEN MATCHED AND target.field2 <> source.field2 THEN UPDATE SET target.Column4 = '?'
OUTPUT $action, inserted.id /*assuming an autonumber field here*/, inserted.field1, inserted.field2, inserted.field4, deleted.field4 INTO @changes;

--@changes will now contain a record of all the changes that have been made. Use this data to do the "create new record in table2" as necessary.

Open in new window

0
simonsjdIT ManagerAuthor Commented:
Sorry if I haven't explained the logic very well.

If I get a match on "check table2 for match on table1.field1 = table2.field1 and table2.field4 = 'Y'"

and also table1.field2 <> table2.field2

I need to update table2.field2 in the matched record and then create a new record in  table 2



If I get a match on "check table2 for match on table1.field1 = table2.field1 and table2.field4 = 'Y'"

and also table1.field2 =  table2.field2 I do nothing
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Scott PletcherSenior DBACommented:
I don't know what you want to put in "field4" in the new row, so you'll have to fill that in below.  I'm also assuming field2 can't be NULL -- if it can be, we'll have to adjust the comparison on field2.


INSERT INTO Table2 ( field1, field2, field4 )
SELECT t1.field1, t1.field2, ?.?
FROM Table1 t1
INNER JOIN Table2 t2 ON t2.field1 = t1.field1 AND t2.field4 = 'Y' AND t2.field2 <> t1.field2

UPDATE t2
SET field2 = t1.field2
FROM Table2 t2
INNER JOIN Table2 t2 ON t2.field1 = t1.field1 AND t2.field4 = 'Y' AND t2.field2 <> t1.field2
0
simonsjdIT ManagerAuthor Commented:
Thanks Scott. I think the only bit missing is create a new record in table2 if there is no match with table1?
Thanks
0
Scott PletcherSenior DBACommented:
So you also want to INSERT if there's no match at all on table2??

--first:
INSERT INTO Table2 ( field1, field2, field4 )
SELECT t1.field1, t1.field2, ?.?
FROM Table1 t1
LEFT OUTER JOIN Table2 t2 ON t2.field1 = t1.field1 AND t2.field4 = 'Y' AND t2.field2 <> t1.field2
WHERE t2.field1 IS NULL
--then:
UPDATE t2
SET field2 = t1.field2
FROM Table2 t2
INNER JOIN Table2 t2 ON t2.field1 = t1.field1 AND t2.field4 = 'Y' AND t2.field2 <> t1.field2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simonsjdIT ManagerAuthor Commented:
Many thanks - sorted
0
Scott PletcherSenior DBACommented:
Great, glad I helped you arrive at a solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
T-SQL

From novice to tech pro — start learning today.