简体   繁体   English

在PL / SQL Oracle中,需要与此T-Sql函数等效的函数。 如果还有其他解决方案,则流水线化

[英]Need an equivalent function for this T-Sql function in PL/SQL Oracle. If there is other solution then Pipelining

 ALTER  FUNCTION [dbo].[MyFun]()
 RETURNS  TABLE 
 AS
 RETURN 
 (
 WITH addd(CITY_NAME,CITY_ID)
 AS
 (
    select CITY_NAME,CITY_ID from city
 )
  select * from addd
 )

Pipelining is not required because I want to call it direct as called in T-SQL for example 不需要流水线,因为我想像在T-SQL中那样直接调用它

Select * from myfun();

You can use the Oracle pipelined function functionality: 您可以使用Oracle流水线函数功能:

Step 1: Create an object the represents one row of the results: 第1步:创建一个对象,代表一行结果:

CREATE OR REPLACE TYPE city_type 
AS OBJECT
(
    city_id VARCHAR(6),
    city_name VARCHAR(60)    
);

Step 2: Create a collection (table type) of the object type created in step 1 步骤2:创建在步骤1中创建的对象类型的集合(表类型)

CREATE OR REPLACE TYPE city_table_type 

AS TABLE OF city_type;

Step 3: Create a function that returns the table type from step 3 步骤3:创建一个函数,该函数返回步骤3中的表类型

CREATE OR REPLACE FUNCTION fnc_Get_Cities_Pipelined
  RETURN city_table_type
  PIPELINED
AS
BEGIN

    FOR v_Rec IN (SELECT * FROM city) LOOP

        PIPE ROW (city_type(v_Rec.City_Id, v_Rec.City_Name));

    END LOOP;

    RETURN;
END;

Step 4: Consume the function as if it was a “table” or “view” 步骤4:使用该函数,就好像它是“表”或“视图”一样

SELECT city_id, city_name
FROM TABLE(fnc_Get_Cities_Pipelined());

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

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