Reputation: 105
Is it possible to create a table in SQL dynamically. In my web application I need to store the data in a table named as "INFO_2013" for this year (i.e) 2013. When the system date changes to 2014, then a new table has to be created as "INFO_2014" and the values have to be added in that table.
NOTE : Also the Table has to be in the same DB.
Upvotes: 0
Views: 146
Reputation: 7723
Try This :
DECLARE @sql NVARCHAR(2000)
DECLARE @table NVARCHAR(100)
SET @table = 'INFO_2013'
SET @sql = N'CREATE TABLE '+@table+'
(
RollNumber INT
,Name VARCHAR(50)
)'
EXECUTE SP_EXECUTESQL @sql
Upvotes: 0
Reputation: 166606
I would avoid using this approach.
Why not rather create a single table, with a YEAR
column, which will identify which year it is from?
Further to that, if you are woried about the table getting to large, you might want to take a look at Creating Partitioned Tables and Indexes
May have a look at these too
Upvotes: 6