【问题标题】:How i create a table from a list of values?我如何从值列表中创建一个表?
【发布时间】:2015-12-17 21:13:40
【问题描述】:

我的工作真的很辛苦,但我不知道如何在 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_ 表的代码?

谢谢!

【问题讨论】:

  • 研究使用 Pivot。
  • Sorry about the "2) Table STAGIN_MEBB:",有错误,表应该是这样的:-------- ---------------------- D_Product D_Store D_Time ME_BB ME_BB1 ...... MEBBx ---------------- --------------------------
  • 您能否将所需的 STAGIN_MEAA 脚本添加到问题中?我无法获取 Measure 列...
  • 我想要一个像这样的脚本:CREATE TABLE STAGIN_MEAA (D_PRODUCT BIGINT, D_STORE BIGINT, ME_AA [decimal](19,6), ME_AA1 [decimal](19,6), .. ... ME_AAX [十进制](19,6) )
  • 如果 ME_AA 也是要创建的列,为什么不将这些作为“D_Product”之类的行提供?因为这种结构很难处理

标签: sql sql-server performance tsql stored-procedures


【解决方案1】:

这是脚本。我省略了“测量”列。我已经测试过了。您可以根据您使用的 sql 服务器更改脚本。

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                                                                                                   

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 2017-03-30
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多