We help IT Professionals succeed at work.

SQL update statement help -- update 1 table if another statement returns null

194 Views
Last Modified: 2012-05-08
I need to update a table in 1 database if a select statement on another table returns NULL.   I'm not sure how the syntax should be setup, but what I have below is not working.
UPDATE ARCUS SET BITFLAG = 4 WHERE 
ISNULL(SELECT CRMDEV.dbo.Users.user_userid FROM CRMDEV.dbo.Users WHERE RTRIM(LTRIM(user_firstname)) + ' ' + RTRIM(LTRIM(user_lastname)) = '{fullname}') 
AND IDCUST = '{compid}'

Open in new window

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
this should do:
UPDATE a
  SET BITFLAG = 4 
FROM ARCUS a
LEFT JOIN CRMDEV.dbo.Users u
  ON u.IDCUST = '{compid}'
 AND RTRIM(LTRIM(u.user_firstname)) + ' ' + RTRIM(LTRIM(u.user_lastname)) = '{fullname}') 
WHERE i.user_userid IS NULL

Open in new window

AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
assuming that those two tables are connected via fullname and ID

UPDATE a
SET BITFLAG = 4
FROM ARCUS a
JOIN CRMDEV.dbo.Users u on RTRIM(LTRIM(user_firstname)) + ' ' + RTRIM(LTRIM(user_lastname)) = a.fullname
and u.IDCUST  = U.compid
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
i think Angel is right

Author

Commented:
They are only connected through the fullname.   There is no compid on the Users table.   The following query obtained exactly OPPOSITE of what I want--if it found a user it was updating the BITFLAG.  I want it to only update the BITFLAG if there is no matching user.   How do I reverse the following?

UPDATE a
SET CBSBITFLAG = 4
FROM ARCUS a
JOIN CRMDEV.dbo.Users u on
RTRIM(LTRIM(user_firstname)) + ' ' + RTRIM(LTRIM(user_lastname)) = '{fullname}'
and a.IDCUST  = '{IDCUST}'
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
as I posted initially, you need a left join + additional condition in the where:
UPDATE a
SET CBSBITFLAG = 4
FROM ARCUS a
LEFT JOIN CRMDEV.dbo.Users u on
RTRIM(LTRIM(user_firstname)) + ' ' + RTRIM(LTRIM(user_lastname)) = '{fullname}'
and a.IDCUST  = '{IDCUST}'
AND u.user_firstname IS NULL

Open in new window

Author

Commented:
Closer, but I need some condition other than that because if the firstname or lastname have values but the full value doesn't match it won't update correctly...Just ran it in a test and it updated 0 rows when there was someone's name that didn't match.    
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
can you please show the column names in ARCUS table that should "match" the fullname?

Author

Commented:
It's just one column: FULLNAME.    ARCUS.FULLNAME should match
RTRIM(LTRIM(user_firstname)) + ' ' + RTRIM(LTRIM(user_lastname)) from the USERS table.
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
It worked....Thanks!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.