Solved

Issue with updating using a join

Posted on 2014-12-04
5
59 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
  • 3
5 Comments
 
LVL 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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 46

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 51

Accepted Solution

by:
HainKurt 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse field in SQL View 15 96
Microsoft SQL ADO Conn Issue 6 37
SQL Exceptions 3 36
SQL - How to list all tables participating in a query 7 42
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

914 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

19 Experts available now in Live!

Get 1:1 Help Now