简体   繁体   English

如何创建存储过程?

[英]How do I create the stored procedure?

I have the following records generated through some sql joins. 我通过一些sql联接生成了以下记录。

ID SYSTEM   CODE
1  W         A
2  W         NULL
3  W         NULL
4  W         B
5  U         X

In the above generated records I need to update all the records CODE as 'X' since the SYSTEM U has X in it. 在上面生成的记录中,由于SYSTEM U中包含X,因此我需要将所有记录CODE更新为“ X”。 Please note this CODE which is I'm updating is from some table say CODES_TABLE. 请注意,我正在更新的此CODE来自某些表,例如CODES_TABLE。 How do I do that through stored procedure? 如何通过存储过程做到这一点? DO I have to create some temp table to hold these vales somewhere? 我是否必须创建一些临时表以将这些值保存在某个地方? Thanks in advance 提前致谢

Is this what you want? 这是你想要的吗?

update t
    set code = (select t2.code from t t2 where t2.system = 'U')
    where t2.system <> 'U';

EDIT: 编辑:

Or, for a query, just use analytic functions: 或者,对于查询,只需使用解析函数:

select t.*, max(case when system = 'U' then code end) over () as u_code
from t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM