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
Satish BSr. MIS executive Database ManagementAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Mike EghtebasDatabase and Application DeveloperCommented:
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
0
Satish BSr. MIS executive Database ManagementAuthor Commented:
When matching account in both table then i need as Not_Paid other wise Paid
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
If I understood correctly your requirement then you'll need two updates. One for NOT PAID (matching records with table A) and second one for PAID (not matching records):
UPDATE b
SET b.status = 'Not paid'
FROM TableB b
	INNER JOIN TableA a ON b.account_no = a.account_no

UPDATE b
SET b.status = 'Paid',
	b.Payment_date = GETDATE()-1
FROM TableB b
WHERE NOT EXISTS (SELECT 1
	FROM TableA a 
	WHERE b.account_no = a.account_no)

Open in new window

1

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
Satish BSr. MIS executive Database ManagementAuthor Commented:
Thank you so much Mr.Vitor for solution provided its giving me exact result.
0
Steven VellaIT AdviserCommented:
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);
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
Microsoft SQL Server

From novice to tech pro — start learning today.