Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:44:33+00:00 2026-05-25T13:44:33+00:00

I have a tableA: ID value 1 100 2 101 2 444 3 501

  • 0

I have a tableA:

ID value
 1  100
 2  101
 2  444
 3  501

Also TableB

ID Code
1
2

Now I want to populate col = code of table B if there exists ID = 2 in tableA. for multiple values , get max value.
else populate it with ‘123’. Now here is what I used:

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

I am sure there is some problem in BEGIN;END or in IF EXIST;ELSE.
Basically I want to by-pass the else part if select statement in IF-part exist and vice- versa. For example if select statement of IF=part is:

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:44:33+00:00Added an answer on May 25, 2026 at 1:44 pm

    EDIT

    I want to add the reason that your IF statement seems to not work. When you do an EXISTS on an aggregate, it’s always going to be true. It returns a value even if the ID doesn’t exist. Sure, it’s NULL, but its returning it. Instead, do this:

    if exists(select 1 from table where id = 4)
    

    and you’ll get to the ELSE portion of your IF statement.


    Now, here’s a better, set-based solution:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have three tables : TableA (key, value) TableB (key, value) TableC (key,
I have a table id_user, hour, medition 1 0 100 1 1 101 1
I have this table : Trans_ID Name Value Total_Item 100 I1 0.33333333 3 100
I have this table : TABLE Transaction Trans_ID Name Value Total_Item 100 I1 0.33333333
I have a table that contains the following: DataDate Value 2010-03-01 08:31:32.000 100 2010-03-01
I have a table similar to the following: value unit ----- ----- 100 cm
We have a table value function that returns a list of people you may
I have table with some fields that the value will be 1 0. This
I have the following query in VBA: DoCmd.RunSQL INSERT INTO table (value) VALUES ('example')
I have a number of code value tables that contain a code and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.