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
 SQL Server Development (2000)
 Question of 'set @variable'

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-07-16 : 07:27:05
Ambika writes "I am using SQL Server 2000 T-Sql
I am not able to code: get the number of rows in a table into a variable where the tablename is a variable

This is my code:

declare @counter int
declare @tablename varchar(100)
exec('select @counter=count(*) from ' + @tablename)

The above does not work because @counter has not been declared inside the exec() session.
The problem is because I cannot hard code @tablename
Please tell me a way around it

Thanks
-Ambika"

smccreadie
Aged Yak Warrior

505 Posts

Posted - 2003-07-16 : 07:29:53
This question gets asked a lot - see http://www.sqlteam.com/FAQ.asp and click on the dynamic SQL links to learn how to build a query dynamically.

Go to Top of Page
   

- Advertisement -