Solved

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

Posted on 2011-09-08
2
178 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach 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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now