oracle10g中多行的单列更新

发布于 2024-12-06 05:29:48 字数 291 浏览 2 评论 0原文

Update table_1 set col1= 1 where col2 = 'ONE';  
update table_1 set col1= 2 where col2 = 'TWO';  
Update table_1 set col1= 3 where col2 = 'THREE';
...
update table_1 set col1= 100 where col2 = 'HUNDRED';

是否有任何简化的方法可以在单个查询中实现此目的,而不是在 oracle10g 中编写 100 个更新状态网?

Update table_1 set col1= 1 where col2 = 'ONE';  
update table_1 set col1= 2 where col2 = 'TWO';  
Update table_1 set col1= 3 where col2 = 'THREE';
...
update table_1 set col1= 100 where col2 = 'HUNDRED';

Is there any simplified way to achive this in a single query instead of writing 100 update statemnets in oracle10g??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-12-13 05:29:48

我认为可能有一个使用Oracle Case-Statement 或decode-function 的解决方案,尽管这将是一个相当长的语句,而且我不太确定相对于100 个更新语句的优势是什么。另外,我不知道有关参数列表长度等的任何限制。

案例示例:

update table_1
set col1 = CASE col2
  WHEN 'ONE' THEN 1
  WHEN 'TWO' THEN 2
  WHEN 'THREE' THEN 3
  WHEN 'FOUR' THEN 4
  WHEN 'FIVE' THEN 5
  WHEN 'SIX' THEN 6
  WHEN 'SEVEN' THEN 7
  WHEN 'EIGHT' THEN 8
  ...
  WHEN 'HUNDRED' THEN 100
  ELSE col2
END;

解码示例:

update table_1
set col1 = decode(col2,
              'ONE', 1,
              'TWO', 2,
              'THREE', 3,
              'FOUR', 4,
              'FIVE', 5,
              'SIX', 6,
              'SEVEN', 7,
              'EIGHT', 8,
              ...
              'HUNDRED', 100,
              col2);

I think there might be a solution with Oracle Case-Statement or the decode-function, although it will be a quite long statement and I am not quite sure what the advantage over 100 update statements might be. Also I am not aware of any limitations regarding length of parameter-lists, etc.

Example for Case:

update table_1
set col1 = CASE col2
  WHEN 'ONE' THEN 1
  WHEN 'TWO' THEN 2
  WHEN 'THREE' THEN 3
  WHEN 'FOUR' THEN 4
  WHEN 'FIVE' THEN 5
  WHEN 'SIX' THEN 6
  WHEN 'SEVEN' THEN 7
  WHEN 'EIGHT' THEN 8
  ...
  WHEN 'HUNDRED' THEN 100
  ELSE col2
END;

Example for decode:

update table_1
set col1 = decode(col2,
              'ONE', 1,
              'TWO', 2,
              'THREE', 3,
              'FOUR', 4,
              'FIVE', 5,
              'SIX', 6,
              'SEVEN', 7,
              'EIGHT', 8,
              ...
              'HUNDRED', 100,
              col2);
我最亲爱的 2024-12-13 05:29:48

您可以使用 Julian 拼写格式(搜索 Asktom.oracle.com 了解更多详细信息)

这是我的会话的输出: -

create table table_1 (col_1 number, col_2 varchar(20))

insert into table_1 (col_1, col_2) values (null, 'THIRTY-THREE')
insert into table_1 (col_1, col_2) values (null, 'SEVEN')
insert into table_1 (col_1, col_2) values (null, 'EIGHTY-FOUR')

select * from table_1 
COL_1     COL_2     
       THIRTY-THREE     
       SEVEN    
       EIGHTY-FOUR

update /*+bypass_ujvc*/
(select t1.col_1, spelled.n
from
table_1 t1
inner join
(select n, to_char(to_date (n, 'J'),'JSP') spelled_n from
(select level n from dual connect by level <= 100)) spelled
on t1.col_2 = spelled.spelled_n
)
set col_1 = n

select * from table_1

COL_1 COL_2
33     THIRTY-THREE
7      SEVEN
84     EIGHTY-FOUR

令人讨厌的提示(bypass_ujvc)忽略了内联视图未保留密钥的事实 - 实际上您应该请改用合并语句。但这不是现实世界的场景,对吧! (并且您必须将“HUNDRED”视为特殊情况=“ONE HUNDRED”。)

You could make use of the Julian spelling formats (search asktom.oracle.com for more details)

Here's output from my session:-

create table table_1 (col_1 number, col_2 varchar(20))

insert into table_1 (col_1, col_2) values (null, 'THIRTY-THREE')
insert into table_1 (col_1, col_2) values (null, 'SEVEN')
insert into table_1 (col_1, col_2) values (null, 'EIGHTY-FOUR')

select * from table_1 
COL_1     COL_2     
       THIRTY-THREE     
       SEVEN    
       EIGHTY-FOUR

update /*+bypass_ujvc*/
(select t1.col_1, spelled.n
from
table_1 t1
inner join
(select n, to_char(to_date (n, 'J'),'JSP') spelled_n from
(select level n from dual connect by level <= 100)) spelled
on t1.col_2 = spelled.spelled_n
)
set col_1 = n

select * from table_1

COL_1 COL_2
33     THIRTY-THREE
7      SEVEN
84     EIGHTY-FOUR

The nasty hint (bypass_ujvc) ignores the fact that the inline view isn't key preserved - in practice you should use a merge statement instead. But this isn't a real-world scenario, right! (And you'll have to treat your 'HUNDRED' as a special case = 'ONE HUNDRED'.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文