【问题标题】:Insert records in SQL if records does not exists如果记录不存在,则在 SQL 中插入记录
【发布时间】:2014-04-04 03:05:44
【问题描述】:

我想在 SQL 中插入记录,这样如果条目组合存在,则脚本不应继续插入语句。这是我到目前为止所拥有的:

insert into TABLE_TESTING(R_COMPONENT_ID,OPRID) 
select 1942,'Test' 
from  TABLE_TESTING 
where not exists 
    (select * 
     from TABLE_TESTING 
     where R_COMPONENT_ID='1942' 
       and oprid ='Test');

我有一个表名:TABLE_TESTING 它有两列:R_COMPONENT_ID 和 OPRID

如果数据库中已经存在“1942”和“测试”的记录组合,则我的脚本不应执行插入操作,如果不存在,则应将记录作为 R_COMPONENT_ID 和 OPRID 的组合插入。

请提出建议。 使用上面指定的查询,我在数据库中添加了多个插入。请提出一些解决方案。

【问题讨论】:

  • 您使用的是哪个 DBMS?后格雷斯?甲骨文?
  • @a_horse_with_no_name 我正在使用 Oracle。

标签: sql oracle insert duplicates


【解决方案1】:

由于您不想更新现有行,因此您的方法基本上是正确的。您唯一需要做的更改是替换插入语句源中的from table_testing

insert into TABLE_TESTING (R_COMPONENT_ID,OPRID) 
select 1942,'Test' 
from  dual -- <<< this is the change
where not exists 
    (select * 
     from TABLE_TESTING 
     where R_COMPONENT_ID = 1942
       and oprid = 'Test');

当您使用from table_testing 时,这意味着插入尝试在TABLE_TESTING 中插入一行为每一行。但是您只想插入一个 single 行。从DUAL 中选择将实现这一点。

正如其他人所指出的,您也可以为此使用 MERGE 语句,如果您需要插入的不仅仅是一行,这可能会更好一些。

merge into table_testing target
using 
(
    select 1942 as R_COMPONENT_ID, 'Test' as OPRID from dual
    union all 
    select 1943, 'Test2' from dual
) src
ON (src.r_component_id = target.r_component_id and src.oprid = target.oprid)
when not matched
then insert (r_component_id, oprid)
     values (src.r_component_id, src.oprid);

【讨论】:

    【解决方案2】:

    试试这个

    if not exists(Select * From TABLE_TESTING where R_COMPONENT_ID='1942' and OPRID='Test' )
    begin
        insert into TABLE_TESTING(R_COMPONENT_ID,OPRID) values('1942','Test')
    end
    

    【讨论】:

    • 它在命令开始时说错误。这是我正在执行的查询:如果不存在(Select * From TABLE_TESTING where R_COMPONENT_ID='1942' and OPRID='Test')开始插入 TABLE_TESTING(R_COMPONENT_ID,OPRID)值('1942','Test')结束;
    • 我在我的 Oracle SQL Developer 中执行了这个
    • 在SQL SERVER中使用,不知道Oracle SQL Developer。
    • 我的意思是只有 SQL Server。请提出建议。
    • 是的,我的 ans 在 sql server 中工作,不知道 OSD。对于 oracle sql 开发人员,请参考此 [链接] (stackoverflow.com/questions/10824764/…)
    【解决方案3】:

    这是一个使用 MERGE 的骨架。我运行它,它工作正常。您可以根据需要进一步调整它。希望这会有所帮助!

    DECLARE
    BEGIN
       FOR CURTESTING IN (SELECT R_COMPONENT_ID, OPRID FROM TABLE_TESTING)
       LOOP
          MERGE INTO TABLE_TESTING
               USING DUAL
                  ON (R_COMPONENT_ID = '1942' AND OPRID = 'Test')
          WHEN NOT MATCHED
          THEN
             INSERT     (PK, R_COMPONENT_ID, OPRID)
                 VALUES (TEST_TABLE.NEXTVAL, '1942', 'Test');
       END LOOP;
    
       COMMIT;
    END;
    

    【讨论】:

    • 在循环中进行合并并不是一个好主意。这可以在不使用(非常慢的)光标方法的情况下完成
    • 我同意。想法是给 MERGE 的骨架,因为 @user3373799 提到不知道 MERGE。
    【解决方案4】:

    你可以使用MERGE操作。

    【讨论】:

    • 因为我是 SQL 新手,所以没有太多知识。
    • 获得一些知识比复制粘贴要好。只是一个建议。酷。
    【解决方案5】:
    insert into TABLE_TESTING  
    select 1942,'Test' where 0<(
    select count(1) from TABLE_TESTING 
    where not exists(select 1 from TABLE_TESTING  where R_COMPONENT_ID=1942 and oprid ='Test'))
    

    试试上面的代码。

    【讨论】:

    • 得到以下错误代码:SQL 错误:ORA-00923:FROM 关键字未在预期的位置找到 00923。00000 -“FROM 关键字未在预期的位置找到”
    猜你喜欢
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 2021-11-14
    • 2018-12-26
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多