Question

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.

Was it helpful?

Solution

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

Index Basics

Types of Indexes

OTHER TIPS

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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top