【问题标题】:SQL Server: Dynamic columns, insert into SQL tableSQL Server:动态列,插入 SQL 表
【发布时间】:2014-04-02 21:20:46
【问题描述】:

我有一个名为 SQL_Standard 的表:

ID name          sql_name      sourceTable
1  member_id     mem_id        tableA
2  member_dob    mem_dob       tableA
3  member_email  mem_email     tableA
4  member_phone                tableA
5  member_id     mbr_id        tableB
6  member_dob    mbr_dob       tableB
7  member_email                tableB
8  member_phone                tableB
9  member_id     sub_id        tableC
10 member_id     emp_id        tableC
11 member_dob    sub_dob       tableC
12 member_email                tableC
13 member_phone sub_phn        tableC

我也有tableA、tableB、tableC。 sql_name 空白表示该列在该表中不存在。但我们的标准名称为 member_id,member_dob, , member_email, member_phone,因为这些是所有源表的标准列。

tableA
mem_id    mem_dob      mem_email
1011      1986-05-05  bsi@yahoo.com
1012      1987-09-09  bw@gmail.com

tableB
mbr_id   mbr_dob
5555     1965-02-09

tableC
sub_id  emp_id  sub_dob      sub_phn 
15       56     19474-02-05  808-888-8888

我们最终希望将所有源表中的所有记录加载到组合表中。 sourcetable 中不存在的列将不会被加载。

我想在这里实现两个目标:

  1. 将源表中的所有记录加载到组合表中
  2. 如果源表有两个名称(ID 9 和 10),请连接 两列合二为一。

组合表:

Member_id   member_dob  member_email member_phone  sourcetable

1011       1986-05-05   bsi@yahoo.com              tableA
1012       1987-09-09   bw@gmail.com               tableA
5555       1965-02-09                              tableB
1556       19474-02-05  808-888-8888               tableC

我的努力:

DECLARE @memID VARCHAR(20)
DECLARE @DOB   VARCHAR(20)
DECLARE @email VARCHAR( 20)
DECLARE @phone VARCHAR(20)

SET @source_table = (SELECT top 1 sourcetable from SQL_Standard )


SELECT @memID = sql_name 
from SQL_Standard 
where (name = 'member_id' and sourcetable = @source_table) 

SELECT @DOB = sql_name 
from SQL_Standard 
where (name = 'member_dob' and sourcetable = @source_table) 

SELECT @email = sql_name 
from SQL_Standard 
where (name = 'member_email' and sourcetable = @source_table) 

SELECT @phone = sql_name 
from SQL_Standard 
where (name = 'member_phone' and sourcetable = @source_table)    

SELECT @sqlStr = ' ; 
WITH Tableinfo1 AS (SELECT [' + @memID +  '] 
,[' + @DOB +  ']
,[' + @email +  ']
,[' + @phone +  ']
FROM [' + @source_table + '] ) 

SELECT * FROM Tableinfo1'

EXEC (@sqlstr)

【问题讨论】:

  • 你在问什么?当你做你做的事情时有任何错误吗?
  • 为什么不对 3 个表进行连接?其中哪一部分是真正动态的?
  • 是的,首先,我的脚本只有在 sourcetables 中有所有 4 列时才有效。所以它不会处理丢失的列。其次,它不处理串联。谢谢
  • 你是如何生成SQL_Standard的?
  • 名称和列会改变吗?

标签: sql-server concatenation dynamic-programming


【解决方案1】:
IF @memID is not null or @DOB is not null or @email  is not null or @phone is not null  -- make sure at least 1 is there
BEGIN
    SET @sqlStr = ' ; WITH Tableinfo1 AS (SELECT '

    if  @memID is not null
      SET  @sqlStr= @sqlStr + ' [' + @memID +  '],' 
    if  @DOB is not null
      SET  @sqlStr= @sqlStr + ' [' + @DOB +  '],' 
    if   @email  is not null
      SET  @sqlStr= @sqlStr + ' [' +  @email +  '],' 
    if  @phone is not null
      SET  @sqlStr= @sqlStr + ' [' + @phone +  '],' 

    SET @sqlStr =@sqlStr + ' FROM [' + @source_table + '] )'

    --sheer laziness below to remove last comma
    SET @sqlStr =REPLACE(@sqlStr, ', FROM',' FROM')
END

根据需要根据需要添加子句。这并不理想,但肯定有你正在运行的代码你没有发布。

顺便说一下,除了 TableA 之外,你是怎么得到其他东西的

SET @source_table = (SELECT top 1 sourcetable from SQL_Standard )

【讨论】:

  • 好的,我知道你在那里做了什么。但其座右铭是将 tableA、tableB、tableC 中的数据插入到组合表中。您的查询将绕过eiter mem_id、dob、电话或电子邮件为空或“”的记录。这不是我们想要做的。我们想插入或选择(因为我们只写选择)到一个表中,并为不存在的列留空。
  • 你为什么要参数化而不是做子选择内连接其他子选择?
  • 实际数据在哪里,这些只是列名对吗?您需要为每个源表设置类似的IF 语句集,然后将结果视为常规连接。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多