Check it like this:
DROP TABLE #tmp1
DROP TABLE #tmp2
GO
CREATE TABLE #tmp1 ( test_id INT PRIMARY KEY )
CREATE TABLE #tmp2 ( test_id INT NOT NULL )
GO
SET NOCOUNT ON
INSERT INTO #tmp1 VALUES( 1 )
INSERT INTO #tmp1 VALUES( 2 )
INSERT INTO #tmp2 VALUES( 2 )
INSERT INTO #tmp2 VALUES( 2 )
GO
SET NOCOUNT OFF
-- Reproduce the problem
SELECT *
FROM #tmp1 t1
INNER JOIN #tmp2 t2 ON t1.test_id = t2.test_id
GO
UPDATE t1
SET t1.test_id = t2.test_id
FROM #tmp1 t1
INNER JOIN #tmp2 t2 ON t1.test_id = t2.test_id
GO
-- Update with cardinality check
DECLARE @actual INT,
@expected INT
-- Get the number of rows expected
SELECT @expected = COUNT(*)
FROM #tmp1 t1
INNER JOIN #tmp2 t2 ON t1.test_id = t2.test_id
UPDATE t1
SET t1.test_id = t2.test_id
FROM #tmp1 t1
INNER JOIN #tmp2 t2 ON t1.test_id = t2.test_id
-- Capture no. of rows affected
SET @actual = @@rowcount
IF @actual <> @expected
RAISERROR( 'Wrong number of rows affected. Actual(%i) Expected(%i).', 16,
1, @actual, @expected )
wBob
"simonZ" wrote:
> Thanx.
>
> I know I can use agregate but
> if select join statement returns 3 rows, than also the same update from the
> same join should affect all 3 rows.
>
> This is very tricky and can cause a lot of mistakes over sql users, because
> a lot of them doesn't know that and it's not so logical.
> I think that this should be repaired with some service pack :)
>
> regards,Simon
>
>
> "xyb" <xiangyuanbo@gmail.com> wrote in message
> news:1175246779.324079.169690@e65g2000hsc.googlegroups.com...
> On 3Ã?Ã?30Ã?Ã?, ÃÃ?Ã?ç4Ã?±56·Ã?, "simonZ" <simon.zu...@studio-moderna.com> wrote:
> > Here is DDL:
> >
> > if exists (select * from dbo.sysobjects where id =
> > object_id(N'[dbo].[table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
> > drop table [dbo].[table1]
> >
> > if exists (select * from dbo.sysobjects where id =
> > object_id(N'[dbo].[Table2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
> > drop table [dbo].[table2]
> >
> > CREATE TABLE [dbo].[table1] (
> > [rowID] [int] IDENTITY (1, 1) NOT NULL ,
> > [warehouseID] [int] NOT NULL ,
> > [productID] [char] (1) COLLATE SQL_Slovenian_CP1250_CI_AS NOT NULL ,
> > [quantity] [int] NULL
> > ) ON [PRIMARY]
> >
> > CREATE TABLE [dbo].[table2] (
> > [warehouseID] [int] NOT NULL ,
> > [productID] [char] (1) COLLATE SQL_Slovenian_CP1250_CI_AS NOT NULL ,
> > [quantity] [int] NOT NULL
> > ) ON [PRIMARY]
> >
> > INSERT INTO table1(warehouseID,productID,quantity) VALUES (1,'A',10)
> > INSERT INTO table1(warehouseID,productID,quantity) VALUES (1,'A',20)
> > INSERT INTO table1(warehouseID,productID,quantity) VALUES (1,'B',30)
> >
> > INSERT INTO table2(warehouseID,productID,quantity) VALUES (1,'A',5)
> > INSERT INTO table2(warehouseID,productID,quantity) VALUES (1,'B',100)
> >
> > begin tran
> > select * from table2
> >
> > select c.* from table1 c INNER JOIN table2 i
> > ON c.warehouseID=i.warehouseID AND c.productID=i.productID
> >
> > UPDATE i SET i.quantity=i.quantity+c.quantity
> > from table1 c INNER JOIN table2 i
> > ON c.warehouseID=i.warehouseID AND c.productID=i.productID
> >
> > select * from table2
> > rollback tran
> >
> > Quantity in table2 should be 35, but it's not.
> >
> > Regards,S
> >
> > "simonZ" <simon.zu...@studio-moderna.com> wrote in message
> >
> > news:O6j5wbqcHHA.1240@TK2MSFTNGP04.phx.gbl...
> >
> >
> >
> > >I have very simple example.
> >
> > > If I write select statement:
> >
> > > select c.* from table1 c INNER JOIN table2 i
> > > ON c.warehouseID=i.warehouseID AND c.productID=i.productID
> >
> > > I get 3 rows:
> >
> > > warehouseID productID quantity
> > > -----------------------------------------
> > > 1 'A' 10
> > > 1 'A' 20
> > > 2 'B' 30
> >
> > > I I write UPDATE statement:
> >
> > > UPDATE i SET i.quantity=i.quantity+c.quantity
> > > from table1 c INNER JOIN table2 i
> > > ON c.warehouseID=i.warehouseID AND c.productID=i.productID
> >
> > > only 2 rows are updated.
> > > Product A is updated only for quantity=10, not for quantity=20.
> >
> > > Why update statement doesn't effect as many rows as select statement
> > > returns?
> >
> > > Regards,Simon- �þ²�±»�ý������ -
> >
> > - ÃÃ?Ã?¾Ã?ýÃ?Ã?µÃ?Ã?Ã?Ã?Ã? -
>
> thank you for post DDL
> you see that table2 have only two row,and UPDATE clause based on this
> 2 rows to update,
> so if i have not misunderstood your meaning,you will want data as:
> 1 A 35
> 1 B 130
> so
> you can not update one row from other two row i think.
> if you want, you shoud use aggerate function here :)
>
>
>