Q:

Select from table with dynamic compute name

I want to write query in which name table will dynamicaly compute.  I have code like this below.  What should I put in 'magic code' region? 
DECLARE @myTableName nvarchar(100) = 'sch' 
         +  CAST(@nowYear as VARCHAR(5)) 
         +  'Q' 
         +  CAST(@nowQuarter as VARCHAR(3)) 
         +  '.[tVisits]'

-- magic code --
myTable = DoSomething(@aktTableName)   
-- magic code --

SELECT * FROM myTable

I use MS SQL Server 2012

A:

You need use the dynamic SQL -
DECLARE 
      @nowYear INT = 2013
    , @nowQuarter INT = 1

DECLARE @myTableName NVARCHAR(100) = '[sch' 
         +  CAST(@nowYear AS VARCHAR(5)) 
         +  'Q' 
         +  CAST(@nowQuarter AS VARCHAR(3)) 
         +  '].[tVisits]'

DECLARE @SQL NVARCHAR(MAX) = N'SELECT * FROM ' + @myTableName

EXEC sys.sp_executesql @SQL

