Reputation: 41
create procedure qa_cc
@tablename varchar(500)
AS
BEGIN
-- Create two integer values
DECLARE @tableOneCount varchar(50), @tableTwoCount varchar(50)
-- Get the number of rows from the first table
SELECT @tableOneCount = 'SELECT COUNT(*) FROM' + @tablename;
exec (@tableOneCount);
select @tableOneCount ;
END
exec qa_cc @tablename=table1
Upvotes: 4
Views: 11336
Reputation: 878
CREATE PROCEDURE [dbo].[TableRowCount]
@tableName text
AS
EXEC ('SELECT COUNT(*) FROM ' + @tableName)
RETURN @@ROWCOUNT
I hope that helps. Thank you.
Upvotes: -1
Reputation: 51494
SELECT SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
and ta.name=@tablename
GROUP BY ta.name
Upvotes: 3
Reputation: 70718
Looks like you were missing a space after the FROM keyword which was reading FROMTABLE
rather than FROM Table
DECLARE @TABLE NVARCHAR(100)
SET @TABLE = 'CLIENT' -- YOUR TABLE NAME
EXEC('SELECT COUNT(*) FROM ' + @TABLE)
Upvotes: 2
Reputation: 238078
You could use sp_executesql
, like:
declare @sql nvarchar(max);
set @sql = N'select @cnt = count(*) from ' + @tablename;
declare @cnt int;
exec sp_executesql @sql, N'@cnt int output', @cnt = @cnt output;
select @cnt;
Upvotes: 3