473,372 Members | 848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Update using a join in the where clause

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 1451
code green
1,726 Expert 1GB
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
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 Expert 1GB
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
got it - thanks. I will give that a try. I will need the sub-q.
Feb 2 '10 #5
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)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.