Capture the result from dynamic sql statement

I have the following part of a script

@String is my table name
What im trying to do is capture the count value so that i can then update another table
What it does is retruns on the screen the value 91896 but the print @retVal remains empty.

      SELECT @SQL = N'SELECT COUNT(*) FROM '
      SELECT @SQL = @SQL + @STRING
      set @retType = N'@cnt varchar(20) OUTPUT'

      exec sp_executesql @sql, @retType, @retVal OUTPUT

      print 'ret val ' + convert(nvarchar(100),@retVal)
      

Any suggestions.
LVL 7
TRACEYMARYAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

raopsnCommented:
raopsnCommented:
Try this;

     SELECT @SQL = N'SELECT @cnt = COUNT(*) FROM '
     SELECT @SQL = @SQL + @STRING
     set @retType = N'@cnt varchar(20) OUTPUT'

     exec sp_executesql @sql, @retType, @cnt = @retVal OUTPUT

     print 'ret val ' + convert(nvarchar(100),@retVal)
 

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TRACEYMARYAuthor Commented:
Your a star....................thanks very much been on this all day long i could not work it out....

Thanks very much really appreciated have a great day.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Anthony PerkinsCommented:
>>Thanks very much really appreciated have a great day.<<
Great!  Now please close the question.
Anthony PerkinsCommented:
Thank you for the points, but I believe you have mistaken me for raopsn, please see here from the EE Help:

I accepted the wrong answer. Now what?
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi17
TRACEYMARYAuthor Commented:
so i did sorry .............i get it changed
TRACEYMARYAuthor Commented:
Thanks acperkins...........for be honest i did click wrong answer.
raopsnCommented:
Thanks Guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.