Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about select count(*) from "@table"

Status
Not open for further replies.

bloombergCR

Programmer
Sep 17, 2007
8
US
declare @table varchar(20)
set @table = 'Persons'
select count(*) from "@table"

Hi,

Would you please tell me why the above script gave me an error meassage of Must declare the variable '@table'?
How can I improve it?

Thanks alot.

Bloomberg
 
because @table is not a table variable.

Is this what you are trying to do?

Code:
declare @table table (id int)

insert into @table values(1)
insert into @table values(2)
insert into @table values(3)

select count(*) from @table

- Paul
- If at first you don't succeed, find out if the loser gets anything.
 
Or, maybe it's dynamic SQL you are looking for.

Code:
declare @table varchar(20)
set @table = 'Persons'

Declare @SQL VarChar(8000)
Set @SQL = 'select count(*) from [' + @table + ']'

exec (@SQL)

If you decide to use this code, do a google search on Dynamic SQL first.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks for helping me. It works.

One more thing,

declare @table varchar(20),
@stmt varchar(200),
@count int

set @table = 'Persons'
set @stmt = 'select @count = count(*) from [' + @table + ']'

execute (@stmt)

I want the @count holding the number of records.

Thanks again!

Bloomberg
 
declare @table varchar(20),
@stmt varchar(200),
@count int

set @table = 'Persons'
set @stmt = N'select ' + QUOTENAME(@count) + ' = count(*) from [' + @table + ']'
execute (@stmt)

Can you tell why this script outputs no error, no result?

Bloomberg
 
Dynamic SQL use sp_executesql worked me out.

Thank you very much!


Bloomberg
 
I still have problems.

DECLARE @SQLString nvarchar(2000),
@paramList nvarchar(1000),
@RtnCount int

SET @SQLString = N'SELECT @RtnCount = COUNT(t1.PersonsID) FROM @thisTable t1
INNER JOIN Students
ON Students.PersonID = t1.PersonID
WHERE Students.StudentsID = CAST(@thisID as varchar(10))'

SELECT @ParamList = N'@thisTable varchar(1000),
@thisID int,
@RtnCount int OUTPUT'

EXEC SP_EXECUTESQL @SQLString,
@ParamList,
@thisTable = 'Persons',
@thisID = 124356,
@RtnCount = @RtnCount OUTPUT


I got an error of Must declare the variable '@thisTable'. I am confused.

Because the table will be changed, TABLENAME should be dynamic. I wonder if TABLENAME can be a parameter?

Thank you!

Bloomberg
 
How about this?

Code:
DECLARE @SQLString  nvarchar(2000),
        @paramList  nvarchar(1000),
        @RtnCount   int 

DECLARE @thisTable varchar(50)
SET @thisTable = 'Persons'

SET @SQLString = N'SELECT @RtnCount = COUNT(t1.PersonsID) FROM ' + @thisTable            
+ 't1 INNER JOIN Students  
ON Students.PersonID = t1.PersonID  
WHERE Students.StudentsID = CAST(@thisID as varchar(10))'          
         
SELECT @ParamList = N'@thisID int,   
                      @RtnCount int OUTPUT'

EXEC SP_EXECUTESQL @SQLString, 
                   @ParamList, 
                   @thisID    = 124356,
                   @RtnCount  = @RtnCount OUTPUT

SELECT @RtnCount

Sunil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top