My customer creates yearly a new table with the year included in the name that I have to use in a new view of my SQL Server database. I have solved this problem in a single query:
DECLARE @SQLString nvarchar(500)
SET @SQLString = 'SELECT * FROM [MYDATABASE].[dbo].[MYTABLE_'+cast(YEAR(GETDATE()) as varchar(4))+']'
EXECUTE sp_executesql @SQLString
but I cannot use an execute statement in a view. I've also tryied to move it to a function but the problem is the same. ¿What could I do?
Thanks in advance.
VIEW
. You'll have to use a Stored Procedure for this type of logic. – Philterunion
with enough yearly tables andwhere
clauses to handle the data until you're gone. <Tongue firmly in cheek.> – Avesta