I’m playing around with t-sql and would like to be able to select into some variable.
Currently I just want to get a count(*) for every table in the database and print this.
When I try the following:
EXEC(‘SELECT COUNT(*) FROM ‘+ @table_name)
This gives the counts for all rows, now, I’d like to store the COUNT(*) into a variable. To achieve this i’ve declared. I’ve tried a few different approaches, amongst others this one:
EXEC(‘SELECT ‘ + @row_count +’ = COUNT(*) FROM ‘+ @table_name)
Which yields an error (Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ‘=’.).
Any idea how to correctly express this?
There is a more efficient way to get the number of rows in a table, by looking at the database metadata. Here is an alternative solution: