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 in Database by Kithuzzz
• 20,660 points
197 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 by narikkadan
• 37,660 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 in Database by Kithuzzz
• 20,660 points
374 views
0 votes
0 answers

Check if table exists in SQL Server

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

Aug 21 in Database by Kithuzzz
• 20,660 points
789 views
0 votes
0 answers

If else in stored procedure sql server

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

Aug 22 in Database by Kithuzzz
• 20,660 points
104 views
0 votes
0 answers

SQL Server Insert if not exists

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

Aug 25 in Database by Kithuzzz
• 20,660 points
50 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 in Database by Vaani
• 7,020 points
210 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
346 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 in Database by Vaani
• 7,020 points
167 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 in Database by Vaani
• 7,020 points
70 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 in Database by narikkadan
• 37,660 points
52 views
0 votes
1 answer

Use of contains() in sql server

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

answered Sep 10 in Database by narikkadan
• 37,660 points
61 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