且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

识别PL/SQL迭代中的电话号码

更新时间:2023-11-29 15:25:46

我会说很难找到比oracle更丰富的另一个sql引擎. 我建议您设置 sqlfiddle 这样的问题.

I'd say its quite hard to find another sql-engine more feature rich then oracle. I advice you to setup sqlfiddle for question like this.

UPDATE t_numbers tn SET
  tn.country = (
    SELECT ic.country 
    FROM int_codes ic 
    WHERE ic.int_code = substr(tn.phone_number, 1, 4)
  )
WHERE tn.country IS NULL;

如果将其作为pl/sql脚本运行,则可以使用循环将其包装起来,以避免复制/粘贴.

if you running it as pl/sql script you can wrap it with loop, to avoid copy/paste.

BEGIN
  FOR i IN REVERSE 1..4
  LOOP
    UPDATE t_numbers tn SET
      tn.country = (
        SELECT ic.country 
        FROM int_codes ic 
        WHERE ic.int_code = substr(tn.phone_number, 1, i)
      )
    WHERE tn.country IS NULL;
  END LOOP;
END;

此代码依赖于int_codes表中的int_code列为UK的事实.

This code relies on a fact that column int_code in the int_codes table is UK.