plsql

SQL Query to return N rows from dual

跟風遠走 提交于 2021-02-06 15:23:15
问题 I want to write a SQL query which accepts a bind variable (say :NUM) and its output consists of one column & :NUM number of rows, each row having its row number. i.e. if we pass :NUM as 7, the output should be: VAL ==== 1 2 3 4 5 6 7 There shouldn't be any actual DB tables in query and no PL/SQL code should be used. i.e. only dual should be used in the query Is there any way to achieve this? 回答1: You could use: WHERE ROWNUM <= :NUM ...but the table has to contain row equal or greater to the

SQL Query to return N rows from dual

眉间皱痕 提交于 2021-02-06 15:23:05
问题 I want to write a SQL query which accepts a bind variable (say :NUM) and its output consists of one column & :NUM number of rows, each row having its row number. i.e. if we pass :NUM as 7, the output should be: VAL ==== 1 2 3 4 5 6 7 There shouldn't be any actual DB tables in query and no PL/SQL code should be used. i.e. only dual should be used in the query Is there any way to achieve this? 回答1: You could use: WHERE ROWNUM <= :NUM ...but the table has to contain row equal or greater to the

dynamically pass table name in oracle for loop

独自空忆成欢 提交于 2021-02-05 11:51:41
问题 Is it possible to dynamically pass table name in oracle for loop ? e.g :- for nm in ('select * from '|| table_name)) loop dbms_output.put_line('chetan') ; end loop; 回答1: You can use a REF CURSOR type cursor. From Oracle Doc: (contains also binds - you don't asked for them...) CREATE OR REPLACE PROCEDURE query_invoice( month VARCHAR2, year VARCHAR2) IS TYPE cur_typ IS REF CURSOR; c cur_typ; query_str VARCHAR2(200); inv_num NUMBER; inv_cust VARCHAR2(20); inv_amt NUMBER; BEGIN query_str :=

Retrieving the data from the table using the PL/SQL

孤者浪人 提交于 2021-02-05 11:37:49
问题 I want to retrieve all the information about each department from the DEPARTMENT table and display the information on the screen. Column name Data type Constraints DEPARTMENT_ID NUMBER(5) PK DEPARTMENT_NAME VARCHAR2(25) NOT NULL LOCATION_ID VARCHAR2(15) Sample Output: Department Details are : 1000, ADMIN, HQ-101 1010, DEVELOPMENT, CBE-103 1020, TESTING, CHN-102 I have a code which is as follows- set serveroutput on; declare v_dno department.department_id%type; v_dname department.department

Retrieving the data from the table using the PL/SQL

孤者浪人 提交于 2021-02-05 11:37:19
问题 I want to retrieve all the information about each department from the DEPARTMENT table and display the information on the screen. Column name Data type Constraints DEPARTMENT_ID NUMBER(5) PK DEPARTMENT_NAME VARCHAR2(25) NOT NULL LOCATION_ID VARCHAR2(15) Sample Output: Department Details are : 1000, ADMIN, HQ-101 1010, DEVELOPMENT, CBE-103 1020, TESTING, CHN-102 I have a code which is as follows- set serveroutput on; declare v_dno department.department_id%type; v_dname department.department

Oracle RESET_PACKAGE does not reset value of a variable in the session

邮差的信 提交于 2021-02-05 09:42:42
问题 I have an app where JDBC connections are pooled. This is related to my question. For simplicity let's assume I have 1 connection and I need to set a variable then reset session / context state. However the idea is not reverse / reset the 'app1_ctx' variable particularly as in the actual case users can enter many procedures that set many variables so what I need is one procedure that clears all session related variables or even restart session. (please check this question too to understand the

Creating a table if it doesn't exist already

牧云@^-^@ 提交于 2021-02-04 21:39:02
问题 I'm trying to create a table if it doesn't exist already. I'm currently checking to see if it exists in DBA_TABLES first and if that query returns nothing then insert. Is there a way to just check in the same statement so I don't have to break it up into separate queries? This is what I have currently. BEGIN SELECT COUNT(*) INTO lvnTableExists FROM DBA_TABLES WHERE Table_Name = 'SOME_TABLE'; IF lvnTableExists = 0 THEN EXECUTE IMMEDIATE 'CREATE TABLE SOME_TABLE AS (SELECT * FR0M OTHER_TABLE)';

Creating a table if it doesn't exist already

╄→尐↘猪︶ㄣ 提交于 2021-02-04 21:35:47
问题 I'm trying to create a table if it doesn't exist already. I'm currently checking to see if it exists in DBA_TABLES first and if that query returns nothing then insert. Is there a way to just check in the same statement so I don't have to break it up into separate queries? This is what I have currently. BEGIN SELECT COUNT(*) INTO lvnTableExists FROM DBA_TABLES WHERE Table_Name = 'SOME_TABLE'; IF lvnTableExists = 0 THEN EXECUTE IMMEDIATE 'CREATE TABLE SOME_TABLE AS (SELECT * FR0M OTHER_TABLE)';

Oracle UTL_FILE read CSV file lines

霸气de小男生 提交于 2021-02-04 14:18:14
问题 i'm using UTL_FILE package to read a csv file , then insert values in table, but my issue is how to read values separated by Commas.. , this is my code : declare file1 UTL_FILE.FILE_TYPE; str varchar2(200 CHAR); begin file1 := UTL_FILE.FOPEN('DRCT1','test_file.csv','R'); loop UTL_FILE.GET_LINE(file1,str); -- here i want to read each value before the Commas then insert them in my table -- insert statement.. dbms_output.put_line(str); end loop; exception when no_data_found then UTL_FILE.FCLOSE

Why is static ddl not allowed in PL/SQL?

感情迁移 提交于 2021-02-04 11:38:06
问题 In an Oracle PL/SQL block, why is dynamic sql allowed begin execute immediate 'drop table table_name'; end; but static is not? begin drop table table_name; end; I hope the answer is more insightful than "because that's how the language works". 回答1: The answer is PL/SQL does not support dynamic polymorphism. it only supports static polymorphism because All PL/SQL generats a "DIANA" -> Descriptive Intermediate Attributed Notation for Ada , a tree-structured intermediate language. DIANA is used