sqlserver 存储过程 返回结果集的 例子
本人不会在sqlserver存储过程返回结果集,请大家指教如何写这样的存储过程。能给出代码的例子吗?我明白,主要是不知道怎么写...
本人不会在 sqlserver 存储过程 返回结果集 ,请大家指教如何写这样的存储过程。
能给出代码的例子吗?
我明白,主要是不知道怎么写 展开
能给出代码的例子吗?
我明白,主要是不知道怎么写 展开
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询