使用过程输出重复项

问题描述 投票:1回答:1

我正在学习中,目前正在做一些练习。

我正在尝试使程序显示连续多少重复。

CREATE OR REPLACE PROCEDURE anzahl1(
    tabelle VARCHAR2,
    reihe VARCHAR2,
    wieviel OUT NUMBER
    )AS
    test VARCHAR2(4000);
BEGIN

    EXECUTE IMMEDIATE 'SELECT
        reihe,
        (COUNT(*)-1) AS Anzahl
    INTO     
        wieviel
    FROM 
        tabelle
    GROUP BY 
        reihe
    HAVING COUNT(*) > 1' using out wieviel;
    DBMS_OUTPUT.PUT_LINE(wieviel);

END;
/

我尝试了很多事情,但没有任何效果...

甚至我的老学校老师给我的结果也没用:

CREATE OR REPLACE PROCEDURE check_doppelte_Werte_p ( 
   p_tabellenname   IN     USER_TAB_COLUMNS.TABLE_NAME%TYPE, 
   p_spaltenname    IN     USER_TAB_COLUMNS.COLUMN_NAME%TYPE, 
   p_ergebnis       BOOLEAN 
) OUT     
IS 
   v_dummy           NUMBER := 1; 
   v_sql_anweisung   varchar2 (4000); 
BEGIN 
   v_sql_anweisung := 
         'SELECT   MAX(COUNT (' 
      || p_spaltenname 
      || ')) ' 
      || '  FROM   ' 
      || p_tabellenname 
      || '  GROUP BY ' 
      || p_spaltenname; 

   DBMS_OUTPUT.PUT_LINE (v_sql_anweisung); 

   EXECUTE IMMEDIATE v_sql_anweisung INTO   v_dummy; 

   IF v_dummy > 1 THEN  
     DBMS_OUTPUT.PUT_LINE(   'Die Tabelle ' 
                          || 'hat mindestens  ' 
                          || TO_CHAR (V_DUMMY) 
                          || ' doppelte Werte in der Spalte ' 
                          || p_spaltenname); 
     p_ergebnis := TRUE; 
  ELSE  
     p_ergebnis := FALSE; 
  END IF; 
EXCEPTION 
   -- Keine Werte gefunden, da  
   WHEN NO_DATA_FOUND 
   THEN 
      p_ergebnis := FALSE; 
 DBMS_OUTPUT.PUT_LINE ('Tabellenname oder Spaltenname sind nicht vorhan-
den!!!'); 
END;

你会做什么/改变?

sql plsql output procedure plsqldeveloper
1个回答
0
投票

您的代码不会替换动态sql中的变量。绑定变量必须用于替换语句中变量的使用顺序。

请尝试下面的代码,

CREATE OR REPLACE PROCEDURE anzahl1(
tabelle VARCHAR2,
reihe VARCHAR2,
wieviel OUT NUMBER
)AS
test VARCHAR2(4000);
lv_query VARCHAR2(4000);

BEGIN

 lv_query := 'SELECT CNT FROM (SELECT :2,(COUNT(*)-1) cnt FROM :1 GROUP BY :2 HAVING COUNT(*) > 1)';Z

EXECUTE IMMEDIATE lv_query into wieviel using tabelle,reihe;

DBMS_OUTPUT.PUT_LINE(wieviel);

END;
/   
© www.soinside.com 2019 - 2024. All rights reserved.