I have compiled a list of columns which I hope to build a table with. Is it possible to use a procedure to create a table based on this list? Ie
List
A
B
C
New_Table
Column A Column B Column C
I have compiled a list of columns which I hope to build a table with. Is it possible to use a procedure to create a table based on this list? Ie
List
A
B
C
New_Table
Column A Column B Column C
Customer is atable consist of column List with rows:- A, B, C
What I've done is added a RowNum column on run time into the customer table which is dynamic. Due to this another column of row number got attached with List column. Using this:-
Here is the procedure as per your requirement. Please verify:-
create procedure newtable
@tablename NVARCHAR(1000)
as
Declare @col1 varchar(50),@col2 varchar(50),@col3 varchar(50), @cmd NVARCHAR(1000)
select @col1=List from (select List,ROW_NUMBER() Over(Order by List) as RowNum from customer)x where x.RowNum=1
select @col2=List from (select List,ROW_NUMBER() Over(Order by List) as RowNum from customer)x where x.RowNum=2
select @col3=List from (select List,ROW_NUMBER() Over(Order by List) as RowNum from customer)x where x.RowNum=3
set @cmd=N'Create Table '+@tablename+ N' (' + @col1+N' Varchar(50),'+@col2 + N' varchar(50),'+@col3+N' varchar(50))' ;
print @cmd
Exec(@cmd)
After this run your command:-
exec newtable 'New_table'