Tsql can't update the column with count statement
declare @t1 Table
(
a1 int
)
insert into @t1
select top 10 AnimalID fro开发者_StackOverflow社区m 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
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.
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?)
It's because you never insert into @t2, so your update doesn't have any records to update.
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)
精彩评论