From: Alejandro Carnero on
I have one table named estoque with this fields
Produto int
ICM int


and the second table produtov1 with this fields
Produto int
ICM int

I want to update all the column ICM of the second table produtov1 where the
field
produto is = , I have write this but not work

UPDATE produtov1
SET icm = estoque.icm
WHERE produtov1.produto = estoque.produto

Thanks by any help or ideia






From: Philipp Post on
You need to use a subquery in the SET clause. Alternatively you could
use UPDATE FROM, but be carefull as you get random results if more
than one row in the source table is matching.

Some reading here:
http://sqlblog.com/blogs/hugo_kornelis/archive/2008/03/10/lets-deprecate-update-from.aspx

brgds

Philipp Post

From: Scott Morris on

"Alejandro Carnero" <alecarnero(a)uol.com.br> wrote in message
news:Oc$MW1rOLHA.2276(a)TK2MSFTNGP06.phx.gbl...
>I have one table named estoque with this fields
> Produto int
> ICM int
>
>
> and the second table produtov1 with this fields
> Produto int
> ICM int
>
> I want to update all the column ICM of the second table produtov1 where
> the field
> produto is = , I have write this but not work
>
> UPDATE produtov1
> SET icm = estoque.icm
> WHERE produtov1.produto = estoque.produto
>

UPDATE produtov1
SET icm = estoque.icm
FROM estoque
WHERE produtov1.produto = estoque.produto


From: Dan Guzman on
Below are example of the ANSI-standard subquery method along with the SQL
Server proprietary UPDATE...FROM syntax. In addition to the multi-row
consideration Phillip mentioned, be aware that you will get different
results for non-matching rows. The subquery method will set the target
column to NULL when no rows match (unless further limited via a WHERE
clause) whereas the UPDATE...FROM will update only those rows that match.
Consider the following:

CREATE TABLE dbo.produtov1(
produto int NOT NULL CONSTRAINT PK_produtov1 PRIMARY KEY,
ICM int NULL
);

CREATE TABLE dbo.estoque(
produto int NOT NULL CONSTRAINT PK_estoque PRIMARY KEY,
ICM int NULL
);

INSERT INTO dbo.produtov1
VALUES
(1,0)
,(3,0)
,(5,0)
,(7,0);

INSERT INTO dbo.estoque
VALUES
(1,1)
,(2,2)
,(3,3)
,(4,4) ,(7,7);
GO

UPDATE dbo.produtov1
SET icm = (
SELECT estoque.icm FROM dbo.estoque
WHERE produtov1.produto = estoque.produto);

Results:

produto ICM
1 1
3 3
5 NULL
7 7

Repeating the test with the query below:

UPDATE produtov1
SET icm = estoque.icm
FROM estoque
WHERE produtov1.produto = estoque.produto;

Results:

produto ICM
1 1
3 3
5 0
7 7

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Alejandro Carnero" <alecarnero(a)uol.com.br> wrote in message
news:Oc$MW1rOLHA.2276(a)TK2MSFTNGP06.phx.gbl...
> I have one table named estoque with this fields
> Produto int
> ICM int
>
>
> and the second table produtov1 with this fields
> Produto int
> ICM int
>
> I want to update all the column ICM of the second table produtov1 where
> the field
> produto is = , I have write this but not work
>
> UPDATE produtov1
> SET icm = estoque.icm
> WHERE produtov1.produto = estoque.produto
>
> Thanks by any help or ideia
>
>
>
>
>
>