How do I return a table from a function?

时间:2017-08-30 20:54:32

标签: oracle function oracle12c

I'm using Oracle 12c and Oracle SQL Developer to create a package. My intent is to pass a value to the package from a SQL statement, like public class A { private int[] values; public int[] getValues() { return values; } } . I want to return a table like executing a SQL SELECT statement in the IDE.

This is my package. When I try to compile I get the following error:

my_clf = DecisionTreeClassifier(random_state=0,class_weight='balanced') param_grid = dict( classifier__min_samples_split=[5,7,9,11], classifier__max_leaf_nodes =[50,60,70,80], classifier__max_depth = [1,3,5,7,9] )

How do I fix this?

Here is the package:

#Create a classifier 
clf = LogisticRegression(random_state = 0)

#Cross-validate the dataset
cv=StratifiedKFold(n_splits=n_splits).split(features,labels)

#Declare the hyper-parameter grid
param_grid = dict(
            classifier__tol=[1.0,0.1,0.01,0.001],
              classifier__C = np.power([10.0]*5,list(xrange(-3,2))).tolist(),
              classifier__solver =['newton-cg', 'lbfgs', 'liblinear', 'sag'],

             )

#Perform grid search using the classifier,parameter grid, scoring function and the cross-validated dataset
grid_search = GridSearchCV(clf, param_grid=param_grid, verbose=10,scoring=make_scorer(f1_score),cv=list(cv))

grid_search.fit(features.values,labels.values)

#To get the best score using the specified scoring function use the following
print grid_search.best_score_

#Similarly to get the best estimator
best_clf = grid_logistic.best_estimator_
print best_clf

2 个答案:

答案 0 :(得分:0)

我认为你要么将CURSOR移动到DECLARE部分,要么像这样使用匿名游标:

FOR cur_rec IN (SELECT x1, x2, count(*) FROM t1 WHERE x1 = x)
LOOP

答案 1 :(得分:0)

存在一些问题:函数定义需要BEGIN。此函数必须返回变量的实例而不是游标,查询需要GROUP BY

以下每项更改都有评论。

create table t1(x1 number, x2 number);

CREATE OR REPLACE PACKAGE test2 AS

    TYPE measure_record IS RECORD(
       x_start VARCHAR2(50), 
       x_end VARCHAR2(50), 
       trip_count NUMBER);

    TYPE measure_table IS TABLE OF measure_record;

    FUNCTION get_ups(x number)
        RETURN measure_table
        PIPELINED;
END;
/
CREATE OR REPLACE PACKAGE BODY test2 AS

    FUNCTION get_ups(x number) RETURN measure_table
        PIPELINED as

        temp measure_record; --Add temporary variable to hold the cursor values.

        cursor temp_cur is 
          SELECT x1, x2, count(*) FROM t1 WHERE x1 = x GROUP BY x1, x2;  --Add "GROUP BY".
    BEGIN --Add "BEGIN".
            for cur_rec in temp_cur
            loop
              --Assign the values to the temporary variable.
              temp.x_start := cur_rec.x1;
              temp.x_end := cur_rec.x2;
              temp.trip_count := cur_rec."COUNT(*)";
              pipe row(temp); --Pipe the variable, not the cursor.
            end loop;

        RETURN;
    END get_ups;
END;
/

select * from table(test2.get_ups(1));