Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-如何在where子句中使用逗号分隔的列值_Sql_Sql Server_Tsql_Stored Procedures_Sql Server 2012 - Fatal编程技术网

SQL-如何在where子句中使用逗号分隔的列值

SQL-如何在where子句中使用逗号分隔的列值,sql,sql-server,tsql,stored-procedures,sql-server-2012,Sql,Sql Server,Tsql,Stored Procedures,Sql Server 2012,我有一个名为Configuration的表。它包含如下值: Id SourceColumns TargetColumns SourceTable TargetTable 1 Name, Age CName, CAge STable TTable 2 EId EmplId EmpTable TTable 在存储过程中,我必须从上表中获取列名,并且必须比较源表和目标表 对于第二条记录,我可以很容

我有一个名为Configuration的表。它包含如下值:

Id  SourceColumns   TargetColumns   SourceTable TargetTable
1   Name, Age       CName, CAge     STable       TTable 
2   EId             EmplId          EmpTable     TTable 
在存储过程中,我必须从上表中获取列名,并且必须比较源表和目标表

对于第二条记录,我可以很容易地做到这一点,因为它只有一个列名,所以在where子句中,我可以像

SELECT 
       EId
    ,  EmplId
FROM 
      EmpTable E
      JOIN TTable T ON E.Eid = T.EmplId
表中的第一条记录有两列,以逗号(,)分隔

我要这样比较,

SELECT 
       Name
    ,  Age 
FROM 
      STable S 
      JOIN TTable T ON S.Name = T.CName AND S.Age = T.CAge
在某些情况下,源列和目标列可能有更多以逗号(,)分隔的列名


请在这方面帮助我。

因为我不知道您是否完全理解我在请求评论中建议的数据模型,为了正确回答问题:

由于列
SourceColumns
TargetColumns
中的数据不是原子数据,因此表未规范化。甚至还需要解释数据(分隔符是逗号,一列中的第n个元素与另一列中的第n个元素相关)

这是表的外观(create语句是伪代码):

然后,您的示例数据将成为

配置表
演示:

我想说,更改您的数据模型。关系数据库中不应有逗号分隔的数据。我们有这张桌子。添加一个表
configuration\u columns(id,id\u configuration,source\u column,target\u column)
就完成了设置。看看:除了@ThorstenKettner所说的之外,您似乎还在表中存储对象名,因此,我假设使用它来生成动态语句。这种“配置”表通常表示一个很大的潜在设计缺陷或严重缺乏对数据库设计的理解。要么不需要表,您应该正确地编写语句,要么您需要学习数据库的设计。“因此我必须将表名和列名存储在表中。”它们已经存储在表中;这是
sys.tables
sys.columns
对象。@SteveC非常感谢您。
create table configuration_tables
(
 id_configuration_tables int,
 source_table text,
 target_table text,
 primary key (id_configuration_tables),
 unique key (source_table),
 unique key (target_table) -- or not? in your sample two souce table map to the same target table
);

create table configuration_columns
(
 id_configuration_columns int,
 id_configuration_tables int,
 source_column text,
 target_column text,
 primary key (id_configuration_columns),
 foreign key (id_configuration_tables) references configuration_tables (id_configuration_tables)
);
id_configuration_tables | source_table | target_table ------------------------+--------------+------------- 1 | STable | TTable 2 | EmpTable | TTable id_configuration_columns | id_configuration_tables | source_column | target_column -------------------------+-------------------------+---------------+-------------- 1 | 1 | Name | CName 2 | 1 | Age | CAge 3 | 2 | EId | EmplId
select 
  'select s.' + string_agg (c.source_column + ', t.' + c.target_column, ', ') +
  ' from ' + t.source_table + ' s' +
  ' join ' + t.target_table + ' t' +
  ' on ' + string_agg('t.' + c.target_column + ' = s.' + c.source_column, ' and ') +
  ';' as query
from configuration_tables t
join configuration_columns c on c.id_configuration_tables = t.id_configuration_tables
group by t.source_table, t.target_table
order by t.source_table, t.target_table;