How to print a message of number of rows from a table variable name
18:07 15 Mar 2022

I have to print out the number of rows when given only a variable named after the table and not the actual table name itself. Below is my attempt to make this work. It should print out to the message box of SQL Server "X" number of rows.

DECLARE @tableName AS VARCHAR(100)
SET @tableName = 'Tb_Offers' --Name of table i need to find the number of rows therin

DECLARE @rowCount AS INT --variable to store count of rows
SELECT @rowCount = COUNT(*) FROM @tableName

PRINT @rowCount --statement to print count to MS SQL message box. 

sql sql-server t-sql