Creating table in runtime fetching col frm cursor | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Creating table in runtime fetching col frm cursor

I have a cursur which consists of field name. .These field names
will be going to be the column names for the table which I want to create in run time
i.e I want to create a table in runtime which will take field name from cursor
as column names .
Can some one help me in writing the create table for the same,
I am making the query as given below. DECLARE @sql nvarchar(1000)
Declare @fieldId int
declare @field varchar(500)
declare @CategoryId int declare Field_Cursor cursor for
select a.FieldId,a.FieldName from tblfield
a join TBLCatField as b
on a.FieldId=b.FieldId
where [email protected] open Field_Cursor
fetch next from Field_Cursor into @FieldId,@Field
while @@FETCH_STATUS = 0
BEGIN
select @sql= ‘create table Business_’ + @CategoryId + ‘ (‘ What next ??????????????????? Thanks

Exec(@sql) Well Why do you need to create table runtime, to make the life so complicated? Madhivanan Failing to plan is Planning to fail
A situation arised like this in which we have to do reverse migration,
where the coloumns names need to be taken from a table,which consist of many records.
Thanks Madhi for your response ,But I want to create a table in runtime which will take field name from cursor
as column names
As I specified, you need Exec(@sql) at the end Madhivanan Failing to plan is Planning to fail
Fyi,http://www.sommarskog.se/dynamic_sql.html on using dynamic SQL. Satya SKJ
Microsoft SQL Server MVP
Writer, Contributing Editor & Moderator
http://www.SQL-Server-Performance.Com
This posting is provided AS IS with no rights for the sake of knowledge sharing. Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it.
]]>