从 PL/SQL 中的随机列中选择

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

我正在尝试调试 ChatGPT 建议的一段代码,但我不确定它是否可能。运行此命令时,我收到“未找到数据”错误:

SELECT CASE
    WHEN v_column_no = 1 THEN LASTNAMES1
    WHEN v_column_no = 2 THEN LASTNAMES2
    WHEN v_column_no = 3 THEN LASTNAMES3
    WHEN v_column_no = 4 THEN LASTNAMES4
    WHEN v_column_no = 5 THEN LASTNAMES5
    WHEN v_column_no = 6 THEN LASTNAMES6
    END
INTO v_last_name
FROM EXTERNAL_LAST_NAMES_ALL
WHERE LASTNAME_ID = TRUNC(DBMS_RANDOM.VALUE(1,5171));

我期望从一个随机列中返回一个随机值(该表有 5170 行)。但我收到错误。

是否有一个优雅的解决方案可以在 PL/SQL 中随机选择列?

sql oracle plsql
1个回答
0
投票

是否有一个优雅的解决方案可以在 PL/SQL 中随机选择列?

您可以使用动态sql命令:

--      S a m p l e    D a t a :
Create Table EXTERNAL_LAST_NAMES_ALL 
    ( LASTNAMES1 VarChar2(24), LASTNAMES2 VarChar2(24), LASTNAMES3 VarChar2(24), 
      LASTNAMES4 VarChar2(24), LASTNAMES5 VarChar2(24), LASTNAMES6 VarChar2(24), 
      LASTNAME_ID Number(6)
    );
Insert Into EXTERNAL_LAST_NAMES_ALL
  Select 'Smith',  'Jones', 'Robertson', 'Jordan',    'Mandel',    'Doe',     1 From Dual Union All 
  Select 'Doe',    'Smith', 'Jones',     'Robertson', 'Jordan',    'Mandel',  2 From Dual Union All 
  Select 'Mandel', 'Doe',   'Smith',     'Jones',     'Robertson', 'Jordan',  3 From Dual;

...从 3 个随机 ID 行中返回 3 个随机选择的列的代码:

SET SERVEROUTPUT ON;
Declare 
    v_column_no    EXTERNAL_LAST_NAMES_ALL.LASTNAMES1%TYPE;
    v_last_name    EXTERNAL_LAST_NAMES_ALL.LASTNAMES1%TYPE;
    v_last_name_id EXTERNAL_LAST_NAMES_ALL.LASTNAME_ID%TYPE;
    v_column_name  VarChar2(32);
    v_sql          VarChar2(512);
Begin
    For i In 1..3 Loop
        v_column_no := TRUNC( DBMS_RANDOM.VALUE( 1, 6 ) );
        v_last_name_id := TRUNC( DBMS_RANDOM.VALUE( 1, 3 ) );
        --
        Select Case   When v_column_no = 1 Then 'LASTNAMES1'
                      When v_column_no = 2 Then 'LASTNAMES2'
                      When v_column_no = 3 Then 'LASTNAMES3'
                      When v_column_no = 4 Then 'LASTNAMES4'
                      When v_column_no = 5 Then 'LASTNAMES5'
                      When v_column_no = 6 Then 'LASTNAMES6'
                End "RANDOM_COL_NAME"
            INTO  v_column_name
        From  Dual;
        --
        v_sql := 'Select ' || v_column_name || ' From EXTERNAL_LAST_NAMES_ALL Where LASTNAME_ID = ' || v_last_name_id;
        Execute Immediate v_sql INTO v_last_name; 
        DBMS_OUTPUT.Put_Line( 'Pass No.: ' || i || Chr(10) || 
                              'Random column no:   ' || v_column_no || Chr(10) ||
                              'Random lastname id: ' || v_last_name_id || Chr(10) ||
                              'SQL Statement:      ' || v_sql || Chr(10) ||
                              'Selected lastname:  ' || v_last_name || Chr(10) || 
                              '___________________________________________________');
    End Loop;
End;
/
/*      R e s u l t : 
Pass No.: 1
Random column no:   1
Random lastname id: 1
SQL Statement:      Select LASTNAMES1 From EXTERNAL_LAST_NAMES_ALL Where LASTNAME_ID = 1
Selected lastname:  Smith
___________________________________________________
Pass No.: 2
Random column no:   5
Random lastname id: 2
SQL Statement:      Select LASTNAMES5 From EXTERNAL_LAST_NAMES_ALL Where LASTNAME_ID = 2
Selected lastname:  Jordan
___________________________________________________
Pass No.: 3
Random column no:   3
Random lastname id: 2
SQL Statement:      Select LASTNAMES3 From EXTERNAL_LAST_NAMES_ALL Where LASTNAME_ID = 2
Selected lastname:  Jones
___________________________________________________

PL/SQL procedure successfully completed.                     */
© www.soinside.com 2019 - 2024. All rights reserved.