Solved

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

Posted on 2011-09-08
2
180 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
2 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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