2

如何将此查询转换为也返回相同(字段)表的存储过程:

SELECT   R.REGION_NAME,
           TP.TERRITORY_NAME,
           LSC.LOOKUP_NAME,
           CAST (WM_CONCAT (CM.CUST_NAME) AS VARCHAR2 (500)) AS CUST_NAMES,
           TP.VIEWER_PROFILE,
           TP.CASTING_PREFERENCE,
           TP.PROG_TYPE_PREFERENCE,
           STN_LIST.STN_NAMES,
           STN_LIST.LIC_RIGHTS
    FROM               TERRITORY_PROFILE_MSTR TP
                    INNER JOIN
                       REGION_MSTR R
                    ON TP.REGION_ID = R.REGION_ID
                 LEFT OUTER JOIN
                    LOOKUP_SUB_CATEGORY LSC
                 ON TP.BROADCAST_STD_CD = LSC.LOOKUP_SUB_CAT_ID
              LEFT OUTER JOIN
                 CUST_MSTR CM
              ON CM.TERRITORY_ID = TP.TERRITORY_ID
           LEFT OUTER JOIN
              (SELECT   CAST (WM_CONCAT (SLR.CUST_STN_NAME) AS VARCHAR2 (500))
                             AS STN_NAMES,
                          CAST (WM_CONCAT (LRM.LIC_RIGHTS_ID) AS VARCHAR2 (500))
                             AS LIC_RIGHTS,
                          SLR.CUST_COMPETITOR_ID AS CUST_ID
                   FROM   STN_LIC_RIGHTS SLR, LIC_RIGHTS_MSTR LRM
                  WHERE   SLR.STN_LIC_ID = LRM.LIC_RIGHTS_ID
               GROUP BY   SLR.CUST_COMPETITOR_ID) STN_LIST
           ON STN_LIST.CUST_ID = CM.CUST_ID
   WHERE   TP.TERRITORY_ID <> 0
GROUP BY   TP.TERRITORY_NAME,
           R.REGION_NAME,
           LSC.LOOKUP_NAME,
           STN_LIST.STN_NAMES,
           STN_LIST.LIC_RIGHTS,
           TP.VIEWER_PROFILE,
           TP.CASTING_PREFERENCE,
           TP.PROG_TYPE_PREFERENCE
ORDER BY   R.REGION_NAME, TP.TERRITORY_NAME

我想我不需要指定表结构。请注意,查询功能齐全,并且值以我期望的方式正确返回。

谢谢你。

更新:

存储过程必须通过 SQL Server Reporting Services 或 SSRS 在 ASP.NET(C#) 中兼容和可执行。

4

3 回答 3

3

有几种方法可以满足您的要求:

  1. 通过使用返回 sys_refcursor 的函数/过程,如此处所述
  2. 通过使用此处描述的“参数化视图”
  3. 通过使用此处描述的表函数

可能还有更多……

于 2013-05-27T08:37:25.670 回答
1

您可以创建一个返回引用游标的 PL SQL 过程。

CREATE OR REPLACE
PROCEDURE foo(p_terr_id IN TERRITORY_PROFILE_MSTR.TERRITORY_ID%TYPE, 
  p_recordset out SYS_REFCURSOR) 
AS 
BEGIN 
    OPEN p_recordset FOR
    /* Your SQL here */
END foo;
/

更多细节和例子在这里

于 2013-05-27T08:21:48.813 回答
1

没有人提到我的问题的任何替代方案,例如使用 Oracle VIEWS

VIEWS尽管它们没有任何参数,但我可以使用它来回答我的问题,而不是为我的 SSRS 报告创建存储过程。

为了将来参考这里的代码:

CREATE VIEW RV_TERRITORY_PROFILE
AS
SELECT   R.REGION_NAME,
           TP.TERRITORY_NAME,
           LSC.LOOKUP_NAME,
           CAST (WM_CONCAT (CM.CUST_NAME) AS VARCHAR2 (500)) AS CUST_NAMES,
           TP.VIEWER_PROFILE,
           TP.CASTING_PREFERENCE,
           TP.PROG_TYPE_PREFERENCE,
           STN_LIST.STN_NAMES,
           STN_LIST.LIC_RIGHTS
    FROM               TERRITORY_PROFILE_MSTR TP
                    INNER JOIN
                       REGION_MSTR R
                    ON TP.REGION_ID = R.REGION_ID
                 LEFT OUTER JOIN
                    LOOKUP_SUB_CATEGORY LSC
                 ON TP.BROADCAST_STD_CD = LSC.LOOKUP_SUB_CAT_ID
              LEFT OUTER JOIN
                 CUST_MSTR CM
              ON CM.TERRITORY_ID = TP.TERRITORY_ID
           LEFT OUTER JOIN
              (SELECT   CAST (WM_CONCAT (SLR.CUST_STN_NAME) AS VARCHAR2 (500))
                             AS STN_NAMES,
                          CAST (WM_CONCAT (LRM.LIC_RIGHTS_ID) AS VARCHAR2 (500))
                             AS LIC_RIGHTS,
                          SLR.CUST_COMPETITOR_ID AS CUST_ID
                   FROM   STN_LIC_RIGHTS SLR, LIC_RIGHTS_MSTR LRM
                  WHERE   SLR.STN_LIC_ID = LRM.LIC_RIGHTS_ID
               GROUP BY   SLR.CUST_COMPETITOR_ID) STN_LIST
           ON STN_LIST.CUST_ID = CM.CUST_ID
   WHERE   TP.TERRITORY_ID <> 0
GROUP BY   TP.TERRITORY_NAME,
           R.REGION_NAME,
           LSC.LOOKUP_NAME,
           STN_LIST.STN_NAMES,
           STN_LIST.LIC_RIGHTS,
           TP.VIEWER_PROFILE,
           TP.CASTING_PREFERENCE,
           TP.PROG_TYPE_PREFERENCE
ORDER BY   R.REGION_NAME, TP.TERRITORY_NAME

然后你可以像运行它

SELECT * FROM RV_TERRITORY_PROFILE
于 2013-05-28T06:31:13.863 回答