i want to create a new table that has dynamic number of columns depending on the row values of an other table. For example i have a table (table1) that has 2 columns named 'VALUE' and 'ISACTIVE' ('ISACTIVE' column takes the value 1 if we need to take into account this value as a column in the new table) and i need to create a new table that has: number of columns (and column name) of new table = the values of table1 where Isactive = 1.
            Asked
            
        
        
            Active
            
        
            Viewed 652 times
        
    0
            
            
        - 
                    What should be the datatype of the columns that we create ? – Aparna Aug 02 '17 at 09:05
- 
                    Have you tried to create it dynamically (dynamic SQL query) based on your conditions? – Cosmin Aug 02 '17 at 09:20
3 Answers
0
            
            
        SELECT [attributeName] INTO [DatabaseName].[dbo].[NewTableName] 
FROM [DatabaseName].[dbo].[FromTableName] WHERE ISACTIVE=1
- 
                    this generates a new table with just 1 column (containing the values of table1 where isactive=1). what i need is to create a table that has: number of columns of new table = number of rows of table1 where is active=1 – Marios88 Aug 02 '17 at 08:32
- 
                    You can't apply where condition on columns but if you want to copy only the structure of the table to a new table with selected columns then this is what you can use: SELECT attributeName1, attributeName2... INTO [DatabaseName].[dbo].[NewTableName] FROM [DatabaseName].[dbo].[FromTableName] WHERE 1=2 – Rahul Aug 02 '17 at 08:51
0
            
            
        My first thought was to create it dynamically in a procedure based on your conditions. Read this question and answers , it will help.
T-SQL How to create tables dynamically in stored procedures?
Raw example
DECLARE @SQLString NVARCHAR(MAX)
SET @SQLString = N'CREATE TABLE <table_name> ('
        -- Conditons here
        SET @SQLString = @SQLString + '<column_name> <type>'
        -- End of conditions
    SET @SQLString = @SQLString + ')'
    EXEC (@SQLString)
 
    
    
        Cosmin
        
- 152
- 3
- 18
0
            Try out the below.This is assuming all the columns to be integer .we can modify accordingly if it is varchar .We need to alter the existing table and add a column called textval which defaults to '0' here
     drop table test
     create table test
    (
      value integer,
      isactive integer
    );
  alter table test add  textval nvarchar(max) default '0'
  insert into test (value,isactive) values (123,5);
   select * from test;
Now update the new columns based on the value of isactive .if it is 5 the new column will have upto col5 all beign integer and use this to create a new table
  begin 
   declare @i integer;
   set @i=1
   declare @isactive integer;
   declare @stmt nvarchar(max);
   declare @stmt2 nvarchar(max);
   declare @testval nvarchar(max);
   set @isactive= (select isactive from test)
   while (@i <=@isactive)
     begin
         declare @textval nvarchar(max);
         set @textval = (select textval from test)
        set @stmt= 'update test set textval = '''+ @textval +'col' +cast(@i 
        as varchar(100)) + ' ' + 'integer,'''
      execute sp_executesql @statement=@stmt
     set @i=@i+1;
    end 
  set @testval=(select left(replace(textval,'0col1','col1'),len(textval)-2) 
               from test)
   set @stmt2 ='create table tab1 ( ' + @testval + ' )';
   execute sp_executesql @statement=@stmt2;
  end
 
    
    
        Aparna
        
- 286
- 1
- 11
- 
                    thx mate this is really helpfull. i also found an alternative way by building the 'update' statement inside a string using a cursor and it worked fine. Your way though is more advanced! thanks! – Marios88 Aug 02 '17 at 10:46
- 
                    Thank you ...if you think the query was useful ,it will be great if you can vote for it since it can be used by others as well – Aparna Aug 02 '17 at 11:30
 
     
     
    