Avatar of Satish B
Satish B
 asked on

Related to SQL Query

I have 2 tables e.g. a and b having account_no as common field  i want to update table b if same account_no exist in table a then i need to update the status column in b table as Not paid else Paid also want yesterdays date  in table b under Payment_date column if status is Paid
Microsoft SQL Server

Avatar of undefined
Last Comment
Steven Vella

8/22/2022 - Mon
Mike Eghtebas

Please verify the following 3 items are what you need:

- update table b if same account_no exist in table a

- update the status column in b table as Not paid else Paid
Question: it is not clear when Not paid and when Paid.  Provide criteria

- set b.Payment_date to yesterdays date.   <-- verify
Satish B

ASKER
When matching account in both table then i need as Not_Paid other wise Paid
ASKER CERTIFIED SOLUTION
Vitor Montalvão

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Satish B

ASKER
Thank you so much Mr.Vitor for solution provided its giving me exact result.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Steven Vella

Had to make a few assumptions, but here is a draft for your consideration.

Note: Consider using select statements to test the statement before changing live data i.e. SELECT b.status, getdate()-1 FROM b WHERE EXISTS (...).

update
  b
set
  b.status='Paid',
  b.payment_date=GETDATE()-1
where
  b.status is null and
  exists (select a.account_no
    from a
    where a.account_no=b.account_no);

update
  b
set
  b.status='Not Paid'
where
  b.status is null and
  not exists (select a.account_no
    from a
    where a.account_no=b.account_no);