SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Query - The table x is ambiguous


Update Query - The table x is ambiguous

Author
Message
Scott Thornton-407727
Scott Thornton-407727
SSC-Addicted
SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)

Group: General Forum Members
Points: 449 Visits: 258
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

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94365 Visits: 38955
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



Cool
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)
Scott Thornton-407727
Scott Thornton-407727
SSC-Addicted
SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)

Group: General Forum Members
Points: 449 Visits: 258
wow, that worked.

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

Thanks!
Shadab Shah
Shadab Shah
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 798
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.
Shadab Shah
Shadab Shah
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 798
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.
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51302 Visits: 21156
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.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search