Gayashan
Gayashan

Reputation: 351

SQL Query Update Error

update invd.InvoiceDetail set invd.costprice=883.75
from InvoiceDetail invd
where saleDespatchDetailID = 5

I'm getting an error in this query:

Msg 208, Level 16, State 1, Line 1
Invalid object name 'invd.InvoiceDetail'.

Upvotes: 1

Views: 5171

Answers (2)

John Woo
John Woo

Reputation: 263893

you are not joining in your update so you could directly execute the basic UPDATE statement.

update InvoiceDetail 
set    costprice = 883.75
where  saleDespatchDetailID = 5

In my own way of writing UPDATE statement, I only use FROM if I'm joining a table on the update statement, like this below

UPDATE a
SET    a.ColumnName = 'a'
FROM   table1 a
       INNER JOIN table2 b
            ON a.PK = b.FK
WHERE  a.Column = 'xxx'

Upvotes: 1

Taryn
Taryn

Reputation: 247860

Since you provided an alias you will reference the alias, so the code should be:

update invd
set invd.costprice=883.75
from InvoiceDetail invd
where invd.saleDespatchDetailID = 5

But since you are not joining the table with another table, you do not actually need the alias.

update InvoiceDetail
set costprice=883.75
where saleDespatchDetailID = 5

Upvotes: 4

Related Questions