revilord
Programmer
- Oct 2, 2003
- 64
I trying to get a count from a number of tables and use that count as a variable in MS SQL. I have to get the table names from sysobjects so I am using an EXEC. here's what I want but this won't work. I have let out all the loops and other stuff.
DECLARE @query varchar(100)
DECLARE @counter varchar(5)
DECLARE @table_name varchar(20)
SET @table_name = 'sysusers' --
select @query = 'select count(*) from ' + @table_name
EXEC (@query) = @counter -- this fails
If (CAST (@counter) AS int) = 0
BEGIN
Print '0 reocrds in ' + @table name
END
DECLARE @query varchar(100)
DECLARE @counter varchar(5)
DECLARE @table_name varchar(20)
SET @table_name = 'sysusers' --
select @query = 'select count(*) from ' + @table_name
EXEC (@query) = @counter -- this fails
If (CAST (@counter) AS int) = 0
BEGIN
Print '0 reocrds in ' + @table name
END