条件判断decode函数的使用
SELECT P.SYS_CODE,
P.ORDER_ID,
DECODE(P.ORDER_STATUS,
'20100',
'办理中',
'20101',
'办理成功',
'20102',
'办理失败') AS 订单状态
FROM PS_ORDER P
将查询出的多个值赋值给多个变量
DECLARE
XM1 VARCHAR(10);
XM2 VARCHAR(10);
XM3 VARCHAR(10);
BEGIN
SELECT XIANGM1,XIANGM2,XIANGM3 INTO XM1,XM2,XM3 FROM MENZFP WHERE FAPH='001500001017' AND MENZH='00003158';
END;
更新表防止空等情况出现的安全方法
-- 获取扩展表里的第三方订单号插入到base_order里
MERGE INTO BASE_ORDER_TEST F
USING (SELECT C.EXTEND_VALUE, D.ORDER_ID
FROM PS_ORDER_EXTEND_PROPERTY C, PS_ORDER D
WHERE D.ORDER_ID = C.ORDER_ID
AND C.EXTEND_CODE = '800022'
AND D.ORDER_ID = '83620033009900015146') E
ON (F.ORDER_ID = E.ORDER_ID)
WHEN MATCHED THEN
UPDATE SET F.PARTNER_ID = E.EXTEND_VALUE;
COMMIT;
将一个数据关联的多条数据某个值合并成一条数据
/*仅需更新表名:PS_ORDER_DETAIL,需要分组的列名:ORDER_ID,需要合并的列名:PROD_OFFER_NBR*/
SELECT ORDER_ID,
OPERATION_TYPE,
TRANSLATE(LTRIM(TEXT, '/'), '*/', '*,') PROD_OFFER_NBR_xxx
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY ORDER_ID ORDER BY ORDER_ID, LVL DESC) RN,
ORDER_ID,
OPERATION_TYPE,
TEXT
FROM (SELECT ORDER_ID,
OPERATION_TYPE,
LEVEL LVL,
SYS_CONNECT_BY_PATH(PROD_OFFER_NBR, '/') TEXT
FROM (SELECT ORDER_ID,
OPERATION_TYPE,
PROD_OFFER_NBR AS PROD_OFFER_NBR,
ROW_NUMBER() OVER(PARTITION BY ORDER_ID ORDER BY ORDER_ID, PROD_OFFER_NBR) X
FROM PS_ORDER_DETAIL
-- 加入测试数据
WHERE ORDER_ID = '83620033009900015146'
ORDER BY ORDER_ID, PROD_OFFER_NBR) A
CONNECT BY ORDER_ID = PRIOR ORDER_ID
AND X - 1 = PRIOR X))
WHERE RN = 1
ORDER BY ORDER_ID