我想更新一张表中的一个字段,想指定值只能在10,37,43,1,55等这些没有规律的固定数字之间,sql该如何写呢?oracle sql

解决方案 »

  1.   

    scott@ORA11GR2> create table abc (id int);Table created.scott@ORA11GR2> alter table abc add check (id in (10,37,43,1,55));Table altered.scott@ORA11GR2> insert into abc values(10);1 row created.scott@ORA11GR2> insert into abc values(11);
    insert into abc values(11)
    *
    ERROR at line 1:
    ORA-02290: check constraint (SCOTT.SYS_C0013039) violated
    scott@ORA11GR2>
      

  2.   

    楼主为什么要这样做啊 是程序需要还是只想造一些数据?
    如果是程序需要,建议用数组去实现 先定义一个数组 记录这些数据 然后随机一个数作为下标
    如果想自己造数据 建议用动态sql 原理同上
    因为纯sql实现起来太麻烦 下面是我的一个想法 但是好像有点问题 具体原因没查出来
    with tab as (select 1 as id,10 as value from dual union 
    select 2 as id,37 as value from dual union 
    select 3 as id,43 as value from dual union 
    select 4 as id,55 as value from dual  )
    select tab.value from tab where tab.id = round(dbms_random.value(1, 4), 0)
    本来应该是没问题的,但不知道为什么,结果有时候不对
    能力有限,只能做到这一步了
      

  3.   

    没有规律的数据是多还是少?如果只有几个,1楼的枚举+约束可以满足
    如果是有穷多,在PL/SQL用变量来传递