使用CTE更新语句(Firebird 2.1)

时间:2016-01-13 06:57:48

标签: sql firebird flamerobin

我有以下声明,我希望根据它的一些列条目更新表。我使用Firebird 2.1并且文档显示更新可以与CTE一起使用,但我的flamerobin固执地坚持认为更新语句不被识别。你能解释一下吗?

with cte as (select gen_id(gen_new,1) as num , N.elm_prof, N.elm_mat From 
(select distinct elm_mat, elm_prof from elements ) N )
update elements E set E.PROP_TYPE = cte.num where cte.elm_prof = E.ELM_PROF and cte.elm_mat = E.ELM_MAT

Engine Message :
Dynamic SQL Error
SQL error code = -104
Token unknown - line 3, column 1
update


merge into elements E1
using (with CTE as (select distinct e2.ELM_MAT mat1, e2.ELM_PROF mat2 from elements e2)
select gen_id(gen_new,1) num, mat1, mat2 from cte)
on E1.elm_mat = mat1 and e1.elm_prof = mat2
when matched then update set e1.prop_type = num

1 个答案:

答案 0 :(得分:1)

文档中的哪些内容表明更新可以与CTE一起使用?当我查看Common Table Expressions时,它会说

import java.util.LinkedList;
import java.util.Scanner;

class GenQueue<E> {
   private LinkedList<E> list = new LinkedList<E>();
   public void enqueue(E item) {
      list.addLast(item);
   }
   public E dequeue() {
      return list.poll();
   }
   public boolean hasItems() {
      return !list.isEmpty();
   }
   public int size() {
      return list.size();
   }
   public void addItems(GenQueue<? extends E> q) {
      while (q.hasItems())
         list.addLast(q.dequeue());
   }
}

public class myQueue {
   public static void main(String[] args) {
      GenQueue<Employee> empList;
      empList = new GenQueue<Employee>();
      GenQueue<HourlyEmployee> hList;
      hList = new GenQueue<HourlyEmployee>();

      Scanner reader = new Scanner(System.in);  // Reading from System.in
      System.out.println("Enter a number: ");
      int n = reader.nextInt(); // Scans the next token of the input as an int.
      for(int i =1;i<=n ;i++){
          System.out.println("Enter a name: ");
          String name = reader.next();

          hList.enqueue(new HourlyEmployee(name, name));
          empList.addItems(hList);
      }
      System.out.println("The employees' names are:");
      while (empList.hasItems()) {
         Employee emp = empList.dequeue();
         //System.out.println(emp.firstName + " "  + emp.lastName);
         char firstletter = emp.firstName.charAt(1); //error: value is null 
         System.out.println(firstletter); 
      }
   }
}

class Employee {
   public String lastName;
   public String firstName;
   public Employee() {
   }
   public Employee(String last, String first) {
      this.lastName = last;
      this.firstName = first;
   }
   public String toString() {
      return firstName + " " + lastName;
   }
}

class HourlyEmployee extends Employee {
   public double hourlyRate;
   public HourlyEmployee(String last, String first) {
      super(last, first);
   }
}

即只允许<cte-construct> ::= <cte-defs> <main-query> <main-query> ::= the main SELECT statement, which can refer to the CTEs defined in the preamble select。我认为您对语句

感到困惑
  

当括在括号中时,CTE结构可以用作SELECT语句中的子查询,也可以用作UPDATE,MERGE等。

稍后在文档中。据我所知,这意味着你可以使用像

这样的语句
main-query

请注意,在这种情况下,CTE必须是singelton选择。