【问题标题】:Try to insert a record into a table with select and join尝试使用 select 和 join 将记录插入表中
【发布时间】:2012-07-17 18:19:19
【问题描述】:

我目前正在编写一个插入语句,它给了我“ORA-00923:未在预期的地方找到来自关键字。有什么办法可以做到这一点吗?谢谢。这是语句:

Insert into seda_owner.seda_lookup(table_name,description,sequence,value)
Select 'DEFAULT_CATE_CHG_ITEMS_DOCS','Software Requirements Specification', '1', 
  (select **value** from seda_owner.seda_document a 
   join seda_owner.seda_lookup b 
    on b.value = a.guid and description = 'Software Requirements Specification');

我尝试将 3 个字符串 table_name、description 和 sequence 以及一个变量值传递给 table seda_lookup。

【问题讨论】:

  • 问题似乎出在您的变量值中,请尝试在变量中传递具有静态值的值。这将为您提供更多信息来解决问题。
  • 尝试 Select 'DEFAULT_CATE_CHG_ITEMS_DOCS','Software Requirements Specification', '1', E.value from (select value from seda_owner.seda_document a join seda_owner.seda_lookup b on b .value = a.guid 和 description = '软件需求规范') E;反而?使用子查询作为自己的表

标签: sql oracle


【解决方案1】:

在 Oracle 中,选择语句总是需要from tableName。您查询的外部选择没有这样的 from 子句。

Insert into seda_owner.seda_lookup(table_name,description,sequence,value)
Select 'DEFAULT_CATE_CHG_ITEMS_DOCS','Software Requirements Specification', '1', 
  (select **value** from seda_owner.seda_document a 
   inner join seda_owner.seda_lookup b 
    on b.value = a.guid and description = 'Software Requirements Specification')
from dual;

更好:

Insert into seda_owner.seda_lookup(table_name,description,sequence,value)
Select 'DEFAULT_CATE_CHG_ITEMS_DOCS','Software Requirements Specification', 
    '1', **value** 
from seda_owner.seda_document a 
inner join seda_owner.seda_lookup b 
    on b.value = a.guid and description = 'Software Requirements Specification'

第一种解决方案要求子查询返回一个且只有一个行。

第二种解决方案将插入内连接定义的尽可能多的记录。

【讨论】:

    【解决方案2】:
    1. value 周围的星号是代码的一部分吗?还是您只是想加粗这个词?
    2. 也许您的嵌套选择返回不止一行
    3. value是否只在b中?您没有在 select 中对其进行限定,但在连接条件 (b.value) 中确实对其进行了限定

    【讨论】:

    • 无法知道嵌套选择是否返回多行。给出的错误发生在解析时,尚未执行任何操作。所以可能性2可能是个问题,但只有在语法固定之后。
    • 单独运行嵌套选择以查看结果(即查看它返回的行数。)
    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2011-03-22
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多