declare @tableName varchar(30)
declare @sql varchar(255)
select @sql = "select count(*) from " + @tableName
exec (@sql)
Hope this help.
Ricardo.-
Michael Brule escribió en mensaje <352394...@phl.com>...
Table names must be determined before the query (or proc) is run, because
SQL Server takes a parse, optimize, compile execute approach. During the
Parse phase, the parser evaluates whether or not the object names exist.
Since a variable table name is not known at parse time, it can't be
checked, and it can't be optimized.
Now, having said that, you may spawn a separate process with the EXEC
command, like in this example:
declare @tblname sysname, --sysname is varchar(30) not null
@querystring varchar(255)
select @tblname = 'sysobjects'
select @querystring = 'select count(*) from ' + @tblname
exec(@querystring)
... but this doesn't really answer your question, because what you REALLY
wanted to do was get the count of rows in the table. You can't pass the
values of variables out of exec() (at least, AFAIK), so the only way to
do this would be to do all the work (the counts, the action based on the
count, etc.) inside the exec().
--
Ben McEwan, President, Geist, LLC
bmcewan@global2000. -ANTISPAM- .net
Well, I figured it out!!! My thanks to Ricardo, Ben and Ken. See the
attachment to see how it was worked out. Basically, I had to create a
temp table, insert the result of the count into it, then select it back
out. Geez! You'd think there was an easier way!
You'll need to do the select count(*) in an EXEC statement that also
populates a fixed table based on the result - use @@spid to give you a
unique row. Then you can check the table back in the sp and do
something based on that.
Neil Pike MVP/MCSE
Protech Computing Ltd