如何将ORACLE存储过程中SQL的结果集打印出来,并且按照一定的格式封装到一个字符串中呢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将ORACLE存储过程中SQL的结果集打印出来,并且按照一定的格式封装到一个字符串中呢相关的知识,希望对你有一定的参考价值。

CREATE OR REPLACE PROCEDURE KH.WAPCRM_CJ(
V_OP_ID IN NUMBER,
V_MONTH IN NUMBER,
V_INFO OUT VARCHAR2) IS

V_SQL VARCHAR2(3000);
BEGIN
V_SQL='SELECT OBJECT_ID || ''|''||
OBJECT_NAME || ''|''||
PHASE_ID || ''|''||
BUSI_TYPE || ''|''||
BUSI_NAME || ''|''||
BUSI_SUBCLASS_CODE || ''|''||
BUSI_SUBCLASS_NAME || ''|''||
BUSI_CLASSIFI_CODE || ''|''||
BUSI_CLASSIFI_NAME || ''|''||
BRAND_TYPE || ''|''||
CODE_NAME || ''|''||
RULER_OPTI || ''|''||
RULER_CENT || ''|''||
DONE_DATE || ''|''||'';''
FROM KH.KF_IVR_BUSI_CALCULATE S WHERE S.PHASE_ID=' || V_MONTH ||
' AND S.OBJECT_ID=' || V_OP_ID || '';
V_INFO := INFO;
END WAPCRM_CJ;
简单的数就是讲查询结果集,返回,加个分隔符,最后组装个一个字符串

参考技术A 这就用上了递归的思想,思路如下,没有你的数据如果有问题你调试下
V_SQL='select * from (
select REPLACE(SYS_CONNECT_BY_PATH(a,','),',') , LEVEL from (
SELECT OBJECT_ID || ''|''||
OBJECT_NAME || ''|''||
PHASE_ID || ''|''||
BUSI_TYPE || ''|''||
BUSI_NAME || ''|''||
BUSI_SUBCLASS_CODE || ''|''||
BUSI_SUBCLASS_NAME || ''|''||
BUSI_CLASSIFI_CODE || ''|''||
BUSI_CLASSIFI_NAME || ''|''||
BRAND_TYPE || ''|''||
CODE_NAME || ''|''||
RULER_OPTI || ''|''||
RULER_CENT || ''|''||
DONE_DATE || ''|''||'';'' a
FROM KH.KF_IVR_BUSI_CALCULATE S WHERE S.PHASE_ID=' || V_MONTH ||
' AND S.OBJECT_ID=' || V_OP_ID || ')
START WITH ROWNUM =1
CONNECT BY PRIOR rn +1= rn ORDER BY LEVEL DESC )
WHERE ROWNUM < 2 ';本回答被提问者和网友采纳
参考技术B 利用dbms_output.put_line(),wm.concat()函数,应该可以实现你的需求 参考技术C DBMS_OUTPUT.PUT_LINE (你要的内容);

sqlserver 存储过程 返回结果集的 例子

本人不会在 sqlserver 存储过程 返回结果集 ,请大家指教如何写这样的存储过程。
能给出代码的例子吗?
我明白,主要是不知道怎么写

返回结果集不用Output,直接Select出来的结果集就能返回
在应用程序或网页程序中用你调用存储过程的这个对象像普通记录集一个调用就好了
如果是在查询分析器中执行,可以在下面的“网格”中直接看到返回的这个结果集
参考技术A SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE AIR_RPT_Report5
( @Year varchar(10)= '2008',
@Month Varchar(10)= '03' ,
@SQLWhere VARCHAR(2000)
)
AS
BEGIN

EXEC('SELECT IB_ID,''AIR'' AS SYSTEMID,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_R,
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_P,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' then BLAMT else 0 end ) as AMT_R_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' then BLAMT else 0 end ) as AMT_P_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_USD_AG,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_USD_AG,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_EUR,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_EUR,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_GBP,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_GBP,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''R'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_R_CNY,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''P'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_P_CNY,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end )-
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_Profit
FROM
( SELECT SUM(AMOUNT) AS BLAMT,SUM(BASE_CUR_AMOUNT) AS AMT,A.CUR_ID,ARP_TYPE,RP_INDICATOR,IB_ID
FROM RP_FRT A
JOIN AIR_BL_PLAN B ON A.BL_ID=B.BL_ID
WHERE BL_TYPE<>''总单'' AND Year(A.INPUT_DATE)='+@Year+' AND month(A.INPUT_DATE)='+@Month +@SQLWhere+
'GROUP BY IB_ID,A.CUR_ID,RP_INDICATOR,ARP_TYPE
) AS B
GROUP BY IB_ID
UNION
SELECT IB_ID,''OCEAN'' AS SYSTEMID,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_R,
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_P,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' then BLAMT else 0 end ) as AMT_R_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' then BLAMT else 0 end ) as AMT_P_USD,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_USD_AG,
sum(case when CUR_ID=''USD'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_USD_AG,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_EUR,
sum(case when CUR_ID=''EUR'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_EUR,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''R'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_R_GBP,
sum(case when CUR_ID=''GBP'' AND RP_INDICATOR=''P'' AND ARP_TYPE=''AG'' then BLAMT else 0 end ) as AMT_P_GBP,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''R'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_R_CNY,
sum(case when CUR_ID=''CNY'' AND RP_INDICATOR=''P'' AND ARP_TYPE<>''AG'' then AMT else 0 end ) as AMT_P_CNY,
sum(case when RP_INDICATOR=''R'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end )-
sum(case when RP_INDICATOR=''P'' AND CUR_ID IN(''USD'',''CNY'',''EUR'',''GBP'') then ISNULL(AMT,0) else 0 end ) as AMT_Profit
FROM
( SELECT SUM(AMOUNT) AS BLAMT,SUM(BASE_CUR_AMOUNT) AS AMT,A.CUR_ID,ARP_TYPE,RP_INDICATOR,IB_ID
FROM RP_FRT A
JOIN OC_BL_PLAN B ON A.BL_ID=B.BL_ID
WHERE BL_TYPE<>''MBL'' AND Year(A.INPUT_DATE)='+@Year+' AND month(A.INPUT_DATE)='+@Month +@SQLWhere+
'GROUP BY IB_ID,A.CUR_ID,RP_INDICATOR,ARP_TYPE
) AS B
GROUP BY IB_ID')

END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

满意了吗
其实很简单,就是SELECT

以上是关于如何将ORACLE存储过程中SQL的结果集打印出来,并且按照一定的格式封装到一个字符串中呢的主要内容,如果未能解决你的问题,请参考以下文章

oracle 如何返回多条记录

PL/SQL 编程游标存储过程函数

java调用oracle存储过程 关于sql里面in函数参数的问题

Oracle 存储过程返回结果集

oracle 存储过程执行动态SQL 返回结果给游标,外部程序获得dataset结果集。

如何将结果集作为输入从java传递到oracle存储过程