Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update Query - The table x is ambiguous Expand / Collapse
Author
Message
Posted Wednesday, November 9, 2011 8:24 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, August 4, 2014 8:34 PM
Points: 81, Visits: 218
Hello,

I am attempting to write a query to update the cost of an item, based on the cost of an item on the same invoice in one statement.

However I receive the error:
Server: Msg 8154, Level 16, State 1, Line 1
The table 'dbo.INVOICE_ITEM' is ambiguous.

UPDATE dbo.INVOICE_ITEM 
SET
INVOICE_COST = CONSULT_MBS_COST.MBS_ITEM_FEE * 0.50,
ITEM_STATUS_CODE = CONSULT.ITEM_STATUS_CODE
FROM
dbo.INVOICE INVOICE
INNER JOIN
dbo.INVOICE_ITEM TELEHEALTH ON
INVOICE.INVOICE_ID = TELEHEALTH.INVOICE_ID
INNER JOIN
dbo.MBS_ITEM MBS ON
TELEHEALTH.MBS_ITEM_NUMBER = MBS.MBS_ITEM_NUMBER AND
MBS.ITEM_TYPE_CODE = 'M' AND
MBS.TELEHEALTH_ITEM = 'Y'
INNER JOIN
dbo.INVOICE_ITEM CONSULT ON
INVOICE.INVOICE_ID = CONSULT.INVOICE_ID
INNER JOIN
dbo.MBS_ITEM CONSULT_MBS ON
CONSULT.MBS_ITEM_NUMBER = CONSULT_MBS.MBS_ITEM_NUMBER AND
CONSULT_MBS.ITEM_TYPE_CODE = 'M' AND
ISNULL(CONSULT_MBS.TELEHEALTH_ITEM,'N') = 'N'
INNER JOIN
dbo.MBS_ITEM_COST CONSULT_MBS_COST ON
CONSULT_MBS.MBS_ITEM_NUMBER = CONSULT_MBS_COST.MBS_ITEM_NUMBER AND
CONSULT.SERVICE_DATE BETWEEN CONSULT_MBS_COST.MBS_COST_START AND ISNULL(CONSULT_MBS_COST.MBS_COST_END, GETDATE() ) AND
CONSULT_MBS_COST.COST_TYPE_ID = 10

WHERE
INVOICE.INVOICE_DATE IS NULL


The table dbo.INVOICE_ITEM is referenced twice in the FROM clause.

Is this at all possible?

I was hopng to avoid writing something like:

UPDATE dbo.INVOICE_ITEM 
SET
INVOICE_COST = ( <select a new price here in a subquery> )
FROM
ETC ETC

Post #1203234
Posted Wednesday, November 9, 2011 9:25 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:12 PM
Points: 23,397, Visits: 32,241
Just a shot in the dark, but did you try this:

UPDATE TELEHEALTH SET
INVOICE_COST = CONSULT_MBS_COST.MBS_ITEM_FEE * 0.50,
ITEM_STATUS_CODE = CONSULT.ITEM_STATUS_CODE
FROM
dbo.INVOICE INVOICE
INNER JOIN dbo.INVOICE_ITEM TELEHEALTH
ON INVOICE.INVOICE_ID = TELEHEALTH.INVOICE_ID
INNER JOIN dbo.MBS_ITEM MBS
ON TELEHEALTH.MBS_ITEM_NUMBER = MBS.MBS_ITEM_NUMBER AND
MBS.ITEM_TYPE_CODE = 'M' AND
MBS.TELEHEALTH_ITEM = 'Y'
INNER JOIN dbo.INVOICE_ITEM CONSULT
ON INVOICE.INVOICE_ID = CONSULT.INVOICE_ID
INNER JOIN dbo.MBS_ITEM CONSULT_MBS
ON CONSULT.MBS_ITEM_NUMBER = CONSULT_MBS.MBS_ITEM_NUMBER AND
CONSULT_MBS.ITEM_TYPE_CODE = 'M' AND
ISNULL(CONSULT_MBS.TELEHEALTH_ITEM,'N') = 'N'
INNER JOIN dbo.MBS_ITEM_COST CONSULT_MBS_COST
ON CONSULT_MBS.MBS_ITEM_NUMBER = CONSULT_MBS_COST.MBS_ITEM_NUMBER AND
CONSULT.SERVICE_DATE BETWEEN CONSULT_MBS_COST.MBS_COST_START AND
ISNULL(CONSULT_MBS_COST.MBS_COST_END, GETDATE() ) AND
CONSULT_MBS_COST.COST_TYPE_ID = 10
WHERE
INVOICE.INVOICE_DATE IS NULL




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1203239
Posted Wednesday, November 9, 2011 9:28 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, August 4, 2014 8:34 PM
Points: 81, Visits: 218
wow, that worked.

I had no idea you could use Alias's in the UPDATE clause.

Thanks!
Post #1203241
Posted Monday, April 1, 2013 5:24 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, September 15, 2014 5:08 AM
Points: 263, Visits: 651
Lynn Pettis (11/9/2011)
Just a shot in the dark, but did you try this:

UPDATE TELEHEALTH SET
INVOICE_COST = CONSULT_MBS_COST.MBS_ITEM_FEE * 0.50,
ITEM_STATUS_CODE = CONSULT.ITEM_STATUS_CODE
FROM
dbo.INVOICE INVOICE
INNER JOIN dbo.INVOICE_ITEM TELEHEALTH
ON INVOICE.INVOICE_ID = TELEHEALTH.INVOICE_ID
INNER JOIN dbo.MBS_ITEM MBS
ON TELEHEALTH.MBS_ITEM_NUMBER = MBS.MBS_ITEM_NUMBER AND
MBS.ITEM_TYPE_CODE = 'M' AND
MBS.TELEHEALTH_ITEM = 'Y'
INNER JOIN dbo.INVOICE_ITEM CONSULT
ON INVOICE.INVOICE_ID = CONSULT.INVOICE_ID
INNER JOIN dbo.MBS_ITEM CONSULT_MBS
ON CONSULT.MBS_ITEM_NUMBER = CONSULT_MBS.MBS_ITEM_NUMBER AND
CONSULT_MBS.ITEM_TYPE_CODE = 'M' AND
ISNULL(CONSULT_MBS.TELEHEALTH_ITEM,'N') = 'N'
INNER JOIN dbo.MBS_ITEM_COST CONSULT_MBS_COST
ON CONSULT_MBS.MBS_ITEM_NUMBER = CONSULT_MBS_COST.MBS_ITEM_NUMBER AND
CONSULT.SERVICE_DATE BETWEEN CONSULT_MBS_COST.MBS_COST_START AND
ISNULL(CONSULT_MBS_COST.MBS_COST_END, GETDATE() ) AND
CONSULT_MBS_COST.COST_TYPE_ID = 10
WHERE
INVOICE.INVOICE_DATE IS NULL



The solution given by Lynn Pettis worked for me too. But i don't see anything wrong in OP Questions(I too had made the query in same manner). Would like to know from the expert over here why does the OP Query not worked.
Post #1437403
Posted Sunday, April 7, 2013 11:58 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, September 15, 2014 5:08 AM
Points: 263, Visits: 651
Scott Thornton-407727 (11/9/2011)
wow, that worked.

I had no idea you could use Alias's in the UPDATE clause.

Thanks!


Hi experts,
This solution is working for me as a butter on a hot pan. But I would like to know from you guys , when the alias works with an update why not the actual table name. I mean that if we use alias it works why not it works by specifying the table name.
Post #1439690
Posted Monday, April 8, 2013 1:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, September 15, 2014 7:23 AM
Points: 5,074, Visits: 11,852
Shadab Shah (4/7/2013)
Scott Thornton-407727 (11/9/2011)
wow, that worked.

I had no idea you could use Alias's in the UPDATE clause.

Thanks!


Hi experts,
This solution is working for me as a butter on a hot pan. But I would like to know from you guys , when the alias works with an update why not the actual table name. I mean that if we use alias it works why not it works by specifying the table name.


Using the table name works as long as YOU DO NOT alias it in the FROM part of the query - if you do, you need to reference the table using only this alias name.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1439698
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse