Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How Do I Do an SQL Update From a Field on a Table Join?

Posted on 2011-09-08
2
Medium Priority
?
187 Views
Last Modified: 2012-08-13
I need to update tblDatabase status field to 'P' where tblnstance.Server_Name does not exist in my subquery.  I am getting an error on my table joins.

Code Below:

UPDATE tblDatabase SET tblDatabase.status = 'P'
FROM tblApplication INNER JOIN  
tblDBAppRel ON tblApplication.Application_Id = tblDBAppRel.Application_Id INNER JOIN
tblDatabase ON tblDatabase.Database_Id = tblDBAppRel.Database_Id LEFT OUTER JOIN
tblInstance ON tblDatabase.Instance_id = tblInstance.Instance_id
WHERE tblInstance.Server_Name NOT IN
               (SELECT Server_Name
                     FROM #DBWDAIInventory
                     WHERE Server_Name IN (SELECT Server_Name FROM tblMSSQLExtract)
               )
               
How do you write this query correctly?

Thanks,

Dan
0
Comment
Question by:danielolorenz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 36507200
Your statement seems a little contradictory

LEFT OUTER JOIN tblInstance ON tblDatabase.Instance_id = tblInstance.Instance_id
WHERE tblInstance.Server_Name NOT IN

try
;with cte as
(
  select tblDatabase.Instance_id, tblDatabase.status
  from tblDatabase 
  INNER JOIN tblDBAppRel ON tblDatabase.Database_Id = tblDBAppRel.Database_Id 
  inner join tblApplication ON tblApplication.Application_Id = tblDBAppRel.Application_Id 
  inner JOIN tblInstance ON tblDatabase.Instance_id = tblInstance.Instance_id
  WHERE tblInstance.Server_Name NOT IN
               (SELECT Server_Name
                     FROM #DBWDAIInventory
                     WHERE Server_Name IN (SELECT Server_Name FROM tblMSSQLExtract)
               )
)

UPDATE cte 
SET [status] = 'P'

Open in new window

0
 

Author Closing Comment

by:danielolorenz
ID: 36511555
Your solution this time worked great.  With the table join all of the tblDatabase status rows updated successfully and the way I wanted them to.

Thanks for your help.

Dan
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question