SQL Server IF EXISTS ELSE

0 votes

I have a tableA:

ID value
 1  100
 2  101
 2  444
 3  501

Also TableB

ID Code
1
2

Now, if ID = 2 is present in tableA, I want to fill in col = code of table B. Get the maximum value for all values. otherwise, fill it with "123." Here is what I employed:

if exists (select MAX(value) from #A where id = 2)
 BEGIN
 update #B
 set code = (select MAX(value) from #A where id = 2)
 from #A
 END

 ELSE 

 update #B
 set code = 123
 from #B

There must be some issue with BEGIN;END or IF EXIST;ELSE. In essence, I want to skip the else section of the if choose statement and vice versa. Assuming that the choose statement of the IF=part is:

(select MAX(value) from #A where id = 4)

It should just populate 123, coz ID = 4 do not exist ! 

Can someone please help me with this?

Sep 18, 2022 in Database by Kithuzzz
• 38,010 points
3,057 views

1 answer to this question.

0 votes

Try this:

update b
  set code = isnull(a.value, 123)
from #b b
left join (select id, max(value) from #a group by id) a
  on b.id = a.id
where
  b.id = yourid

This has the benefit of being able to run on the entire table rather than individual ids.

answered Sep 19, 2022 by narikkadan
• 63,420 points

Related Questions In Database

0 votes
0 answers

SQL Server 2008 - IF NOT EXISTS INSERT ELSE UPDATE

I'm trying to create a time clock ...READ MORE

Aug 28, 2022 in Database by Kithuzzz
• 38,010 points
3,673 views
0 votes
0 answers

Check if table exists in SQL Server

I want this to be the last ...READ MORE

Aug 21, 2022 in Database by Kithuzzz
• 38,010 points
33,458 views
0 votes
0 answers

If else in stored procedure sql server

I have created a stored procedure as ...READ MORE

Aug 22, 2022 in Database by Kithuzzz
• 38,010 points
725 views
0 votes
0 answers

SQL Server Insert if not exists

I want to add data to my ...READ MORE

Aug 25, 2022 in Database by Kithuzzz
• 38,010 points
1,782 views
0 votes
1 answer

How do I perform an IF THEN statement in an SQL SELECT?

The CASE statement is the closest to IF in ...READ MORE

answered Feb 16, 2022 in Database by Vaani
• 7,020 points
1,248 views
0 votes
1 answer

Calculate Time Intersection to Correlate Sequences of Independent Events

I think this solution requires a CROSS JOIN implementation. ...READ MORE

answered Oct 26, 2018 in Power BI by Upasana
• 8,620 points
581 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,020 points
591 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4, 2022 in Database by Vaani
• 7,020 points
328 views
0 votes
1 answer

Get list of databases from SQL Server

Execute: SELECT name FROM master.sys.databases I hope this helps ...READ MORE

answered Sep 10, 2022 in Database by narikkadan
• 63,420 points
363 views
0 votes
1 answer

Use of contains() in sql server

The straightforward method is shown here. You ...READ MORE

answered Sep 10, 2022 in Database by narikkadan
• 63,420 points
590 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP