postgresql里面有dual没

postgresql里面有dual没,第1张

PostgreSQL中不需要dual虚拟表。在我的理解中,dual是Oracle中为了保证每个select语句都有from而设置的一个虚拟表,而没有其他任何意义。

在PostgreSQL中(SQL Server也是一样),select语句可以没有from,例如:

select 'Hello Jack' as txt

而在Oracle中则必须:

select 'Hello Jack' as txt from dual

需要存储过程实现。

1、创建输出路径,比如你要在d盘test目录下输出,你就先在d盘根目录下建立一个test的目录。

2、sqlplus下以sysdba登录,执行以下语句

12345

create or replace directory TMP as 'd:\test'  grant read,write on directory TMP to scott --比如我用的scott用户  alter system set utl_file_dir='d:\test' scope=spfile

3、以上步骤执行完,需要重启数据库。

4、创建一个存储过程,代码如下(基本是不用改动,原封复制即可):

CREATE OR REPLACE PROCEDURE SP_OUTPUT_PROCEDURE is  file_handle     utl_file.file_type  Write_content   VARCHAR2(1024)  Write_file_name VARCHAR2(50)  v_name          varchar2(50)  v_text          varchar2(2000)  cursor cur_procedure_name is    select distinct name from user_source where type = 'PROCEDURE'  cursor cur_sp_out is    select t.text      from (select 0 line, 'CREATE OR REPLACE ' text              from dual            union            select line, text              from user_source             where type = 'PROCEDURE'               and name = v_name) t     order by linebegin  open cur_procedure_name  loop    fetch cur_procedure_name      into v_name    exit when cur_procedure_name%notfound    write_file_name := v_name || '.txt'    open cur_sp_out    loop      fetch cur_sp_out        into v_text      exit when cur_sp_out%notfound      file_handle   := utl_file.fopen('TMP', write_file_name, 'a')      write_content := v_text      --write file      IF utl_file.is_open(file_handle) THEN        utl_file.put_line(file_handle, write_content)      END IF      --close file      utl_file.fclose(file_handle)    end loop    close cur_sp_out  end loop  close cur_procedure_nameend

5、创建完毕执行存储过程,这个就不赘述了,执行完毕后,你会发现d盘test目录下的文件名就是以存储过程名命名的txt文件,如图:

6、里边内容(就是存储过程创建时的代码,可能排版看着难看点,但是不影响使用):


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6714192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存