title: How to set value to variable using 'execute' in t-sql?
body: <pre><code>DECLARE @dbName nvarchar(128) = 'myDb'
  DECLARE @siteId int 
  exec ('SELECT TOP 1 @siteId = Id FROM ' + @dbName + '..myTbl')  
  select @siteId
  </code></pre>
  
  <p>When I run the script above I get the following error</p>
  
  <pre><code>Msg 137, Level 15, State 1, Line 1
  Must declare the scalar variable "@siteId".
  
  (1 row(s) affected)
  </code></pre>
  
  <p>Why and how to fix it?</p>
  
  <p>Thank you</p>