title: T-SQL table name concatenate with string and @parameter
body: <p>I want to get a count of my @sql before it execute. But I cannot concatenate table name with my parameter value. </p>
  
  <p>SQL:</p>
  
  <pre><code>SET @count = (SELECT count(*) as tblcount 
                FROM RDL_tbl_UserManagement_UserInfo_ + @table_prefix_name 
                WHERE partnerID = CAST(@partnerID AS VARCHAR) 
                  AND userID = CAST(@adminUserID AS VARCHAR) 
                  AND addressType = 'H')
  
  IF @count = 0
  BEGIN
      SET @sql = 'SELECT * FROM RDL_tbl_UserManagement_UserInfo_' + @table_prefix_name +
                 ' WHERE partnerID = ' + CAST(@partnerID AS VARCHAR) + 
                 ' AND userID = ' + CAST(@adminUserID AS VARCHAR) + 
                 ' AND addressType= ''MZ''  ORDER BY company'
  END
  ELSE
  BEGIN           
      SET @sql = 'SELECT * FROM RDL_tbl_UserManagement_UserInfo_' + @table_prefix_name +
                 ' WHERE partnerID = ' + CAST(@partnerID AS VARCHAR) +
                 ' AND userID = ' + CAST(@adminUserID AS VARCHAR) + 
                 ' AND addressType=''H''  ORDER BY company'
  END
  
  Exec (@sql)
  </code></pre>
  
  <p>How to do this ?</p>