sql - How i create a table from a list of values? -
i have hard work , don't know how implement in t-sql (sql server).
i have table (temp_table) this:
------------------------------------------------------------- measure column_name column_type tabletobecreated ------------------------------------------------------------- me_aa d_product [decimal](19,6) stagin_meaa me_aa d_store [decimal](19,6) stagin_meaa me_aa1 d_product [decimal](19,6) stagin_meaa me_aa1 d_store [decimal](19,6) stagin_meaa me_bb d_product [decimal](19,6) stagin_mebb me_bb d_store [decimal](19,6) stagin_mebb me_bb d_time [decimal](19,6) stagin_mebb me_bb1 d_product [decimal](19,6) stagin_mebb me_bb1 d_store [decimal](19,6) stagin_mebb me_bb1 d_time [decimal](19,6) stagin_mebb . .. ... -----------------------------------------------------------------
then, table, want create table called column tabletobecreated temp_table this:
1) table stagin_meaa:
------------------------------------------ d_product d_store me_aa me_aa1 ...... me_aax ------------------------------------------
the data type of me_aa columns must same column "column_type" temp_table.
2) table stagin_mebb:
------------------------------------------ d_product d_store d_store me_bb me_bb1 ...... mebbx ------------------------------------------
the data type of me_aa columns must same column "column_type" temp_table.
how can generate code creation of me_ tables?
thanks!
here script. omitted "measure" column. i've tested it. can alter script due sql server use.
create table #x( id numeric(10) identity, col_name varchar(64), col_type varchar(64), table_name varchar(64) ) delete #x insert #x select 'd_product','[decimal](19,6)','stagin_meaa' insert #x select 'd_store ','[decimal](19,6)','stagin_meaa' insert #x select 'd_product','[decimal](19,6)','stagin_meaa' insert #x select 'd_store ','[decimal](19,6)','stagin_meaa' insert #x select 'd_product','[decimal](19,6)','stagin_mebb' insert #x select 'd_store ','[decimal](19,6)','stagin_mebb' insert #x select 'd_time ','[decimal](19,6)','stagin_mebb' insert #x select 'd_product','[decimal](19,6)','stagin_mebb' insert #x select 'd_store ','[decimal](19,6)','stagin_mebb' insert #x select 'd_time ','[decimal](19,6)','stagin_mebb' create table #y( line varchar(100) ) delete #y declare @min_id numeric(10), @name varchar(64), @type varchar(64), @table varchar(64), @old_table varchar(64), @script varchar(1000) select @min_id = 1 while @min_id not null begin select @name = col_name, @type = col_type, @table = table_name #x id = @min_id if @old_table not null , @old_table != @table insert #y select ") " + char(13) + char(10) + "go" if @old_table != @table insert #y select "create table " + @table + "(" if @old_table = @table insert #y select " ," + @name + " " + @type else insert #y select " " + @name + " " + @type select @old_table = @table select @min_id = min(id) #x id > @min_id end insert #y select ")" insert #y select "go" select line #y drop table #x drop table #y
result:
line ---- create table stagin_meaa( d_product [decimal](19,6) ,d_store [decimal](19,6) ,d_product [decimal](19,6) ,d_store [decimal](19,6) ) go create table stagin_mebb( d_product [decimal](19,6) ,d_store [decimal](19,6) ,d_time [decimal](19,6) ,d_product [decimal](19,6) ,d_store [decimal](19,6) ,d_time [decimal](19,6) ) go
Comments
Post a Comment