Elango_Thanumalayan
Elango_Thanumalayan

Reputation: 105

Idea's to Create a table dynamically

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

Answers (2)

Anvesh
Anvesh

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

Adriaan Stander
Adriaan Stander

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

Index Basics

Types of Indexes

Upvotes: 6

Related Questions