Solved

Issue with updating using a join

Posted on 2014-12-04
5
64 Views
Last Modified: 2014-12-07
UPDATE DIST_CUSTOMER_PRODUCT_ADJUSTMENT D
Join Setup_Tissue_Prefix STP On D.TissuePrefixId = STP.TissuePrefixId
SET D.ADJUSTMENT_AMOUNT = 3.0,
D.Adjustment = 0.00
Where STP.TypePrefix In ('0336')
And D.CustomerID IN (SELECT Customer_Code_id from setup_customer_Code where customer_desc LIKE 'HCA %')


Error message:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'D'.
0
Comment
Question by:Sanjeet
[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
  • 3
5 Comments
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 40480951
maybe this

UPDATE DIST_CUSTOMER_PRODUCT_ADJUSTMENT
SET D.ADJUSTMENT_AMOUNT = 3.0, D.Adjustment = 0.00
from DIST_CUSTOMER_PRODUCT_ADJUSTMENT d Join Setup_Tissue_Prefix STP On D.TissuePrefixId = STP.TissuePrefixId
Where STP.TypePrefix In ('0336')
And D.CustomerID IN (SELECT Customer_Code_id from setup_customer_Code where customer_desc LIKE 'HCA %')
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 40480963
or better this

UPDATE DIST_CUSTOMER_PRODUCT_ADJUSTMENT
   SET D.ADJUSTMENT_AMOUNT = 3.0, D.Adjustment = 0.00
  FROM DIST_CUSTOMER_PRODUCT_ADJUSTMENT d 
       inner Join Setup_Tissue_Prefix STP On D.TissuePrefixId = STP.TissuePrefixId and STP.TypePrefix = '0336'
       inner join setup_customer_Code c on D.CustomerID=c.Customer_Code_id and customer_desc LIKE 'HCA %'

Open in new window

0
 

Author Comment

by:Sanjeet
ID: 40480967
Got this error message:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "D.ADJUSTMENT_AMOUNT" could not be bound.
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40480971
You can't JOIN directly to an updated table. Here's how you should do it:
UPDATE D
 SET D.ADJUSTMENT_AMOUNT = 3.0,
      D.Adjustment = 0.00
FROM DIST_CUSTOMER_PRODUCT_ADJUSTMENT D
INNER JOIN Setup_Tissue_Prefix STP On D.TissuePrefixId = STP.TissuePrefixId 
WHERE STP.TypePrefix In ('0336') 
    AND D.CustomerID IN (SELECT Customer_Code_id from setup_customer_Code where customer_desc LIKE 'HCA %')

Open in new window

0
 
LVL 55

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 40480974
try removing alias

UPDATE DIST_CUSTOMER_PRODUCT_ADJUSTMENT
   SET ADJUSTMENT_AMOUNT = 3.0, Adjustment = 0.00
  FROM DIST_CUSTOMER_PRODUCT_ADJUSTMENT d 
       inner Join Setup_Tissue_Prefix STP On D.TissuePrefixId = STP.TissuePrefixId and STP.TypePrefix = '0336'
       inner join setup_customer_Code c on D.CustomerID=c.Customer_Code_id and customer_desc LIKE 'HCA %'

Open in new window


or if qer above gives error. try this:

UPDATE d
   SET d.ADJUSTMENT_AMOUNT = 3.0, d.Adjustment = 0.00
  FROM DIST_CUSTOMER_PRODUCT_ADJUSTMENT d 
       inner Join Setup_Tissue_Prefix STP On D.TissuePrefixId = STP.TissuePrefixId and STP.TypePrefix = '0336'
       inner join setup_customer_Code c on D.CustomerID=c.Customer_Code_id and customer_desc LIKE 'HCA %'

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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