2012-06-15 88 views
1

我有一個下面的存儲過程:如何從存儲過程獲取數據表?

create or replace 
PROCEDURE "SP_UTILITIES_LOG" 
(
p_utility_name IN varchar2, 
p_r_object_id IN varchar2, 
p_platform_name IN varchar2, 
p_exported_file_path IN varchar2, 
p_Is_binary IN number, 
p_extraction_status IN varchar2, 
p_extraction_error IN varchar2, 
p_extraction_datetime IN VARCHAR2, 
p_schema_name IN varchar, 
p_publication_path IN varchar, 
p_schema_tcm_id IN varchar, 
p_component_tcm_id IN varchar, 
p_component_name IN varchar, 
p_loading_status IN varchar, 
p_transformed_file_path IN varchar, 
p_transformed_status IN varchar, 
p_import_status IN varchar, 
p_loading_error IN varchar, 
p_transform_error IN varchar, 
p_import_error IN varchar, 
p_loading_datetime IN TIMESTAMP, 
p_transform_datetime IN TIMESTAMP, 
p_import_datetime IN TIMESTAMP 

) 
    IS 
    BEGIN 

    IF(p_utility_name ='EXTRACTION') 
    THEN 
    BEGIN 
    INSERT INTO utilities_log(R_OBJECT_ID, 
PLATFORM_NAME, 
EXPORTED_FILE_PATH, 
IS_BINARY, 
EXTRACTION_STATUS, 
EXTRACTION_ERROR, 
EXTRACTION_DATETIME 
) 
    VALUES(p_r_object_id, p_platform_name,p_exported_file_path, p_is_binary, p_extraction_status, p_extraction_error, p_extraction_datetime); 

    END; 
    END IF; 


    IF(p_utility_name ='LOADING') 
    THEN 
    BEGIN 

    UPDATE UTILITIES_LOG SET schema_name=p_schema_name, 
    publication_path= p_publication_path, 
    schema_tcm_id= p_schema_tcm_id, 
    component_tcm_id= p_component_tcm_id, 
    component_name= p_component_name, 
    loading_status= p_loading_status, 
    loading_error= p_loading_error, 
    loading_datetime= current_timestamp 
    WHERE 
    r_object_id= p_r_object_id; 

    END; 
    END IF; 


    IF(p_utility_name ='PRE-TRANSFORMATION') 
    THEN 
    BEGIN 
    SELECT exported_file_path,component_tcm_id FROM utilities_log 
    WHERE 
    platform_name= p_platform_name and loading_status=p_loading_status 
    and extraction_status=p_extraction_status; 


    END; 
    END IF; 


    IF(p_utility_name ='TRANSFORMATION') 
    THEN 
    BEGIN 
    UPDATE UTILITIES_LOG SET 
    transformed_file_path= p_transformed_file_path, 
    transformed_status= p_transformed_status, 
    transform_error= p_transform_error, 
    transform_datetime= current_timestamp 
    WHERE 
    exported_file_path= p_exported_file_path; 

    END; 
    END IF; 

    IF(p_utility_name ='RETRIEVE') 
    THEN 
    BEGIN 
    execute immediate 'create global temporary table temp_import as SELECT transformed_file_path,publication_path 
    FROM utilities_log 
    WHERE 
    platform_name= p_platform_name'; 

    END; 
    END IF; 

    IF(p_utility_name ='IMPORTER') 
    THEN 
    BEGIN 
    UPDATE UTILITIES_LOG SET 
    import_status=p_import_status, 
    import_error=p_import_error, 
    import_datetime=current_timestamp 
    WHERE 
    publication_path= p_publication_path; 
    END; 
    END IF; 


    END; 

我收到錯誤「變成」條款中缺少select語句... 任何幫助嗎? 感謝

回答

1

要做到這一點(返回從SP到C#中的查詢結果集),正確的方法,是通過使用REF CURSOR類型,Oracle提供。 你可以看到它是如何實現的herehere

編輯:
它應該是這樣的(我沒有在這裏有甲骨文,但你可以很容易地檢查):

create or replace PROCEDURE "SP_UTILITIES_LOG" ( 
p_utility_name IN varchar2, 
p_r_object_id IN varchar2, 
/* your other parameters */ 
p_refcur out sys_refcursor /* notice the out parameter */ 
) 
AS 
BEGIN 
/* one sample from your select*/ 
IF(p_utility_name ='PRE-TRANSFORMATION') 
THEN  
BEGIN  
     OPERN p_refcur FOR 
     SELECT exported_file_path,component_tcm_id FROM utilities_log  
     WHERE  platform_name= p_platform_name 
    AND  loading_status=p_loading_status  
    AND extraction_status=p_extraction_status;  
END;  
END IF; 
END; 
+0

感謝Yavgeny的建議....我解決了這個問題......我從來沒有實現過,所以它的一點點難以理解......如果你可以修改代碼,它會有很大的幫助...... 。謝謝 – Aquarius24

+0

非常感謝YavgenyP – Aquarius24

1
IF(p_utility_name ='RETRIEVE') THEN 
BEGIN 
    execute immediate 'create global temporary table temp_import 
    as SELECT transformed_file_path,publication_path  
    INTO temp_import 
    FROM utilities_log  
    WHERE platform_name= p_platform_name' 
END  
END IF 

我覺得

+1

否; CTAS('create table as select')不需要'into'。臨時表不應該在Oracle中隨時創建,但這是OP的錯誤,不是你的* 8-) –

+1

該死的沒有看到orackle標籤。 –

1

錯誤信息涉及到:

IF(p_utility_name ='PRE-TRANSFORMATION') 
THEN 
BEGIN 
SELECT exported_file_path,component_tcm_id FROM utilities_log 
WHERE 
platform_name= p_platform_name and loading_status=p_loading_status 
and extraction_status=p_extraction_status; 
END; 
END IF; 

在一個PL/SQL塊中,數據必須被選入某個東西,所以你需要聲明一些變量,然後執行:

SELECT exported_file_path,component_tcm_id 
INTO <local_exported_file_path_var>, <local_component_tcm_id_var> 
FROM utilities_log 
... 

然後,您可以在SP的其他地方使用這些變量,這似乎不有需要的時刻,所以不知道爲什麼你會選擇在所有。或者您可以選擇OUT參數,以便調用者可以訪問它們,但是您也沒有這樣做。

臨時表不應該在Oracle中隨時創建,因此整個概念看起來很混亂。如果你試圖將一個數據表返回給調用者,那麼就像@ YavgenyP所說的那樣,看看ref遊標。不確定你想要做什麼。

相關問題