【问题标题】:Which table contains column哪个表包含列
【发布时间】:2018-11-13 22:48:19
【问题描述】:

有一个程序可以构建一个select语句,例如

public void GetLog(DataSet dataSet, string tableName, string userCase){
  OracleCommand cmd = new OracleCommand();
  cmd.Connection = conn;
  tmpSql = @"select * from table1 t1
  join table2 on t1.key=t2.key 
  where 1=1 and "+userCase
  cmd.CommandType = CommandType.Text;
  adapter = new OracleDataAdapter(cmd);
  adapter.SelectCommand = cmd;
  adapter.Fill(dataSet, tableName);
}

两个表都包含一个列,例如“NAME”,当 userCase 类似于“name='BLABLA'”时,我有异常“ora-00918 列定义不明确”。我想将表名添加到 userCase 中。如何获取包含此列的表的信息,以便将其添加到 var userCase "table1.name='BLABLA'" 中。 userCase 可以包含 table1 或 table2 中的任何列。我需要更改过程,以便它通过 sql 或表名获取元数据

【问题讨论】:

  • 如果两个表都有一个名为NAME的列,那么只有知道您数据的人才能为您回答这个问题。
  • 这只是示例,sql和字段可能是其他的。我想为构建 sql 制定通用方法。
  • 那么任何使用userCase调用这个函数的进程都必须知道必要列所属的表
  • 正如我之前所说的,这只是一个例子,我需要通用的方法来构建 sql。我尝试使用DataTableMapping,DataTableMapping mapping = adapter.TableMappings.Add("table1", "table1");但映射不包含表描述
  • 我不确定你想做什么是可能的。您可能需要考虑为不同的查询使用不同的方法。

标签: c# oracle dataset metadata


【解决方案1】:

似乎您已经将 tableName 作为 Method 参数传递了。如果这是包含 userCase 列的表,那么您可以编写如下内容:

tmpSql = @"select * from table1 t1

在 t1.key=t2.key 上加入 tablet2 其中 1=1 和 "+tableName+"."+userCase

如果您在多个表中有同名的列,那么计算机将无法知道您指的是哪一个,除非您指定它(通过将其作为 userCase 的一部分传递,或将其连接起来,如上所示)。如果列在单个表中,那么您无需担心。

问候,

【讨论】:

    【解决方案2】:

    需要改sql之类的

    tmpSql = @"select * from (select t1.key as t1_key,t2.key as t2_key, t1.name as t1_name,t2.name  as t2_name..... from table1 t1
      join table2 on t1.key=t2.key )
      where t2_name='...'
    

    并且条件userCase应该基于别名

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      相关资源
      最近更新 更多