UPDATE dbo.INVOICELINES
SET dbo.INVOICELINES.NetPrice = ITEM.Price
FROM dbo.INVOICELINES LNS
INNER JOIN dbo.Storage ITEM ON LNS.Product = ITEM.Product



UPDATE dbo.INVOICELINES
SET dbo.INVOICELINES.GrossPrice = dbo.INVOICELINES.NetPrice/100*(100+(select TaxRate  from dbo.INVOICELINES LNS,dbo.TaxRates txr where LNS.TAX=txr.code))

In this query I updated the prices in the invoices taking the new price from the storage.

The problem came in the last UPDATE and SET where I wanted to update the gross price with the tax code present in the invoice line, referring to the table of tax codes.

The problem came where there were multiple invoices. My database is in Italian (it)

Here is an example:

invoicelines

LINE    INVOI   ARTICLE                     QTT     NET     GROSS   TAX
1       1000    ARTICLE1                    11,00   30,00   36,60   022
2       1000    ARTICLE2                    1,00    0,00    794,22  022
2       2000    ARTICLE1                    1,00    30,00   110,00  010
3       1000    ARTICLE2                    1,00    0,00    55,00   010 

taxcodes

CODE DESCRIPT       PERCENTAGE
004 IVA 4%          4
007 ESCL. ART.7     0
010 IVA 10%         10
017 NS ART 17 EDIL. 0
020 IVA 20%         20
021 IVA 21%         21
022 IVA 22%         22
026 ESCL. ART.26    0
041 NI ART.41 INTRA 0   

The problem is here:

(742 row(s) affected) Msg 512, Level 16, State 1, Line 14 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

1

There are 1 answers

0
Matej Hlavaj On BEST ANSWER

Second update returns more values in subquery, Try this:

UPDATE LNS
SET LNS.GrossPrice = dbo.INVOICELINES.NetPrice/100*(100+(txr.TaxRate)
from dbo.INVOICELINES LNS
JOIN dbo.TaxRates txr ON LNS.TAX=txr.code