views:

42

answers:

4
declare @t1 Table
(
a1 int
)

insert into @t1 
select top 10 AnimalID from Animal 

--select * from @t1 

declare @t2 table
(
dogs int null
)

update @t2
set dogs = (Select COUNT(*) from @t1)

---------> The out put it gives me is just 0

+1  A: 

Try using an insert statement instead of an update ("update" is to modify an already existing record; "insert" is to enter a new one) -

INSERT INTO @t2 SELECT COUNT(*) FROM @t1

And why should there be any output at all from this code? I don't see any select or print statements.

(What exactly are you trying to accomplish?)

froadie
A: 

It's because you never insert into @t2, so your update doesn't have any records to update.

jaltiere
Actually i have various other columns in temp table @t2. I want to update all those columns from the 1st table which @t1, which is also having various other columns.This i was just summarizing to small so i can work around if i get some solution.I think Francisco solution may help me a bit.Thanks
desi
+3  A: 

well first you can't update any records in @t2 becasue you don't have any records to update. You need to do another insert. Assuming you had records, you need a way to relate the subquery to the record you need to update which you don't have.

HLGEM
A: 

It is because you are creating your table, and updating it, but its empty.

Change

update @t2 set dogs = (Select COUNT(*) from @t1)

For

declare @c int

select @c = COUNT(*) from @t1

insert into @t2 (dogs) values (@c)
Francisco Soto
Thanks Francisco. Your solution helps me a bit. best Regards
desi