473,466 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Update using a join in the where clause

4 New Member
I am trying to run this update qry in SQL Server:

UPDATE [mfgtest803].[dbo].[partplant]
SET [reschedoutdelta] = '1'

WHERE ([partplant].[partnum] = [partWhse].[partnum])AND
(([partplant].[safetyqty]>'0')OR([partplant].[minimumqty]>'0'))AND
([partWhse].[WarehouseCode]='120')


I get the following errors:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "partWhse.partnum" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "partWhse.WarehouseCode" could not be bound.

But this select statement works fine:

SELECT [partplant].[plant]
,[partplant].[partnum]
,[partplant].[primwhse]
,[partplant].[minimumqty]
,[partplant].[maximumqty]
,[partplant].[safetyqty]

,[partplant].[reschedoutdelta]
,[partplant].[reschedindelta]
,[partplant].[nonstock]
,[partWhse].[WarehouseCode]

FROM [mfgtest803].[dbo].[partplant], [mfgtest803].[dbo].[partWhse]

WHERE ([partplant].[partnum] = [partWhse].[partnum])AND
(([partplant].[safetyqty]>'0')OR([partplant].[minimumqty]>'0'))AND
([partWhse].[WarehouseCode]='120')


Can someone help me understand what is wrong and how to fix?

thanks!
Feb 1 '10 #1

✓ answered by code green

This is a different problem altogether.
if a partplant record has corosponding partwhse records equal to 120 but not 320,
By this you mean UPDATE if partwhse has a corresponding value of 120 ONLY.
If this is so then (excuse me while I remove all the brackets, are you using Access?)
Expand|Select|Wrap|Line Numbers
  1. WHERE (partplant.safetyqty > 0 
  2.     OR partplant.minimumqty > 0) AND
  3. (partWhse.WarehouseCode = 120 
  4.     AND COUNT(partWhse.WarehouseCode) = 1)
This is assuming your JOIN is correct.
If more than two entries are allowed (multiple 120 or other values) then a sub-query will probably be needed
Expand|Select|Wrap|Line Numbers
  1. WHERE ..id.. NOT IN 
  2. (SELECT ..id.. FROM partWhse 
  3.     WHERE partWhse.WarehouseCode = 320)
..or similar

5 1454
code green
1,726 Recognized Expert Top Contributor
Please remove the Bold My eyes are hurting.
You haven't done a JOIN on partWhse that is why the error states 'cannot be found'
Feb 2 '10 #2
NightZEN
4 New Member
thanks CG! That was it. (sorry about the bolding)

I now have this q:

UPDATE [mfgtest803].[dbo].[partplant]
SET [reschedindelta] = '15'
FROM [mfgtest803].[dbo].[partplant] JOIN [mfgtest803].[dbo].[partWhse] ON ([partplant].[partnum]=[partWhse].[partnum])
WHERE (([partplant].[safetyqty]>'0')OR([partplant].[minimumqty]>'0'))AND
(([partWhse].[WarehouseCode]='120') AND([partWhse].[WarehouseCode]<>'320'))

But there is a problem --> a partplant record often has 2 corosponding partwhse records, whose values could be 120 and 320. IF this is the case, I do not want to update (i.e., if a partplant record has corosponding partwhse records equal to 120 but not 320, then do the update). The above query updates any partplant record that has a corosponding partwhse record = 120. Am I making sense? How can I stop this?

thanks for your help CG
Feb 2 '10 #3
code green
1,726 Recognized Expert Top Contributor
This is a different problem altogether.
if a partplant record has corosponding partwhse records equal to 120 but not 320,
By this you mean UPDATE if partwhse has a corresponding value of 120 ONLY.
If this is so then (excuse me while I remove all the brackets, are you using Access?)
Expand|Select|Wrap|Line Numbers
  1. WHERE (partplant.safetyqty > 0 
  2.     OR partplant.minimumqty > 0) AND
  3. (partWhse.WarehouseCode = 120 
  4.     AND COUNT(partWhse.WarehouseCode) = 1)
This is assuming your JOIN is correct.
If more than two entries are allowed (multiple 120 or other values) then a sub-query will probably be needed
Expand|Select|Wrap|Line Numbers
  1. WHERE ..id.. NOT IN 
  2. (SELECT ..id.. FROM partWhse 
  3.     WHERE partWhse.WarehouseCode = 320)
..or similar
Feb 2 '10 #4
NightZEN
4 New Member
got it - thanks. I will give that a try. I will need the sub-q.
Feb 2 '10 #5
NightZEN
4 New Member
Thanks for your help! this did the trick:

UPDATE [mfgtest803].[dbo].[partplant]
SET [reschedindelta] = '15'
FROM [mfgtest803].[dbo].[partplant] JOIN [mfgtest803].[dbo].[partWhse] ON ([partplant].[partnum]=[partWhse].[partnum])
WHERE (([partplant].[partnum]
NOT IN
(SELECT [partWhse].[partnum] FROM [mfgtest803].[dbo].[partWhse] WHERE [partWhse].[WarehouseCode]<>'120'))
AND
(([partplant].[safetyqty]>'0')OR([partplant].[minimumqty]>'0')))

BTW, I am running this via SQL Management Studio Express - it does not run without the brackets...

Thanks Again!
Feb 2 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
14
by: bolidev | last post by:
I'm new to SQL and can't figure out how to update my table (StoreItemStatus) that contains the current status for items in each store (STORE_KEY, ITEM_KEY, STATUS,...). I get updated status info...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
2
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
4
by: 001 | last post by:
Hello, The select statement needs only 1 second to complete the query. But the update statement spends 30 minutes. Why? SELECT STATEMENT: declare @IDate smalldatetime select @IDate=col001...
5
by: Harry Broomhall | last post by:
I wonder if anybody could give me a few pointers on a problem I face. I need to do an UPDATE on table A, from an effective left outer join on A and another table B. (This is trying to perform a...
6
by: Jeff Kowalczyk | last post by:
I need to adapt this an update statement to a general form that will iterate over multiple orderids for a given customerinvoiceid. My first concern is a form that will work for a given orderid,...
2
by: Sim Zacks | last post by:
The following query updated all the rows in the AssembliesBatch table, not just where batchID=5. There are 2 rows in the AssembliesBatch table with batch ID of 5 and I wanted to update both of...
1
by: Mark Dexter | last post by:
In Microsoft SQL Server, I can write an UPDATE query as follows: update orders set RequiredDate = (case when c.City IN ('Seattle','Portland') then o.OrderDate + 2 else o.OrderDate + 1 end)...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.