Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select COUNT from dynamic table name.sql

Author  Topic 

PeterT
Starting Member

2 Posts

Posted - 2004-11-19 : 18:16:54


declare @tablename varchar(32),
@count int
set @tablename = 'authors'
exec
('
DECLARE crsr_Counts CURSOR FOR
SELECT COUNT (*)
FROM [pubs].[dbo].'+@tablename+'
')
open crsr_Counts
fetch next from crsr_Counts into
@count
IF @@FETCH_STATUS = 0 -- fetch success
print @count
CLOSE crsr_Counts
DEALLOCATE crsr_Counts

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-11-19 : 18:43:29
nice cursor.... totaly useless but nice...
what on earth are you trying to do with that?????

edit:
what's wrong with:
declare @tablename varchar(32),
set @tablename = 'authors'
exec
('SELECT COUNT (*)
FROM [pubs].[dbo].'+@tablename
)

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -