ORA-00928:缺少SELECT关键字:使用更新

时间:2019-03-26 18:24:21

标签: sql oracle

我收到ORA-00928:将“ update”与“ with”一起使用时,缺少SELECT关键字错误。

这给出了错误。

public static void main (String[] args) throws java.lang.Exception
{
    // gather the number of inputs, here we will use 3, but should
    //  be collected from the user
    int sizeofArray = 3;

    // create the array
    int[] arr = new int[sizeofArray * 2];
    fillValues(arr, 3, false);
    Arrays.sort(arr);
    // this should produce [1, 2, 2, 3, 4, 6], so can check the basic
    //  algo
    System.out.println("Sorted array: " + Arrays.toString(arr));

    // here it is with random values
    arr = new int[sizeofArray * 2];
    fillValues(arr, 3, true);
    Arrays.sort(arr);
    System.out.println("Sorted array: " + Arrays.toString(arr));
}

public static void fillValues(int[] arr, int numRand, boolean useRand) 
{
    if (arr.length < numRand) {
        throw new IllegalArgumentException("not enough elements");
    }
   // first three are random, if desired; hard to debug when so
   Random rand = new Random();

   if (useRand) {
    for (int i = 0; i < numRand; ++i) {
      arr[i] = rand.nextInt();
    }
   }
   else {
    for (int i = 0; i < numRand; ++i) {
        arr[i] = (i +  1);
    }
   }

   // now multiple the rest of the array by 2
   for (int j = 0; j < arr.length - numRand; ++j) {
     arr[j + numRand] = arr[j] * 2;
   }
}

但这很好用

with wr_double as
(select...)

update work_request r 
set r.name = r.name || '_old'
where exists 
(select 1 from wr_double wd
where wd.name = r.name and wd.wr_id = r.id)

此外,如果我将with的子查询放在更新正文中,则可以正常工作。

with wr_double as
(select...)

select * from work_request r 
where exists 
(select 1 from wr_double wd
where wd.name = r.name and wd.wr_id = r.id)

我不能将“ with”与“ update”一起使用吗?

1 个答案:

答案 0 :(得分:5)

您必须按照以下方式编写代码,因为CTE是SELECT的一部分,而不是UPDATE

update work_request 
set name = name || '_old'

   where exists (
      with wr_double as
         (select...)
      select 1 from wr_double wd  wd.name = work_request.name and wd.wr_id = work_request.id
     );