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
 General SQL Server Forums
 New to SQL Server Programming
 exec() statement

Author  Topic 

dnaes
Starting Member

3 Posts

Posted - 2007-12-16 : 12:08:28
declare @rej_nm as varchar(50)
exec ('select count(*), ' + "'@rej_nm'" + 'from ' + @rej_nm)

trying to return the rowcount AND the name of the object. return set should look like this:

12, tbl_name

i've tried single quotes around @rej_nm, nested single quotes, single/double nested, etc..

instead, i get an error stating that @rej_nm is not a valid column of tbl_name. bottomline...

how do i return a variable 'value' within a select statement...

thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2007-12-16 : 12:19:19
try this:-

exec ('select count(*),''' + @rej_nm + ''' from ' + @rej_nm)
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-12-16 : 16:55:18
select o.name,i.rows
from sysobjects o
join sysindexes i on o.id = i.id
where o.xtype = 'U' and i.indid < 2
order by i.rows desc


elsasoft.org
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-17 : 03:16:14
provided you run DBCC updateusage('dbname','tablename') with row_counts

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -