2015-09-21 17 views
0

我有一個非常辛苦的工作,我不知道如何在T-SQL(SQL Server)中實現它。我如何從值列表中創建表格?

我有一個表(TEMP_TABLE)所示:

------------------------------------------------------------- 
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 
. 
.. 
... 
----------------------------------------------------------------- 

然後,從該表,我想創建一個稱作從TEMP_TABLE這樣列TableToBeCreated表:

1)表STAGIN_MEAA:

------------------------------------------ 
D_Product  D_Store  ME_AA ME_AA1 ...... ME_AAx 
------------------------------------------ 

的ME_AA列的數據類型必須是從TEMP_TABLE列「COLUMN_TYPE」相同。

2)表STAGIN_MEBB:

------------------------------------------ 
D_Product  D_Store  D_Store ME_BB  ME_BB1 ...... MEBBx 
------------------------------------------ 

的ME_AA列的數據類型必須是從TEMP_TABLE列 「COLUMN_TYPE」 相同。

如何生成創建此ME_表的代碼?

謝謝!

+0

考慮使用一個支點。 –

+0

對不起關於「2)表STAGIN_MEBB:」,出現錯誤,表格必須是這樣的: ------------------------- ----------------- D_Product D_Store D_Time ME_BB ME_BB1 ...... MEBBx ------------------- ----------------------- –

+0

您可以在問題中添加STAGIN_MEAA的精確預期腳本嗎?我不能得到測量列... –

回答

0

這是腳本。我省略了「測量」欄。我測試過了。由於您使用的是sql server,您可以更改腳本。

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 from #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 is not null 
begin 
    select @name = col_name, @type = col_type, @table = table_name from #x where id = @min_id 

    if @old_table is not null and @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) from #x where id > @min_id 
end 

insert #y select ")" 
insert #y select "go" 

select line from #y 

drop table #x 
drop table #y 

結果:

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