当前位置: 首页 > news >正文

SPOOL

-----How to Pass UNIX Variable to SPOOL Command (Doc ID 1029440.6)
setenv只有csh才有不行啊PROBLEM DESCRIPTION:
====================You would like to put a file name in Unix and have SQL*Plus read that file name, 
instead of hardcoding it, because it will change.You want to pass a Unix variable on the command line like:SQL*Plus -s <user>/<password>@test.sql $SPOOLand have the $SPOOL value be passed into existing SQL.SOLUTION DESCRIPTION:
=====================This syntax will not work because everything after the word 'SQL*Plus' is taken 
as arguments passed in to SQL*Plus, and SQL*Plus does not expand 
(or even recognize) the shell variable.  One way around this is to set the SPOOL variable just prior to running the 
script.  For example:set the SPOOL variable with this line:setenv SPOOL /u02/usrname/spoolfile.Then you create a file called testfile.sql that contained the lines:spool $SPOOL;select user from dual;select count(*) from dba_tables;And you then type 'SQL*Plus -s <user>/<password> @testfile.sql'.
The $SPOOL variable will be expanded inside the file to generate the file 
/u02/usrname/spoolfile.lst for the session.  Once exported, shell variables can 
be referenced one level down from their defining shell.

-------generate a grant script:

spool generate_sql.sql
set long 9999999
set header off

SQL> select 'GRANT READ, SELECT on ' || owner || '.' || table_name || ' to <user_name>;' from dba_tables;   -- Replace <user_name> with the user you want to give permissions to.

spool off

---------------set 这些如果直接执行不行------------

SQL> set NEWPAGE 0
SQL> set SPACE 0
SQL> set LINESIZE 80
SQL> set PAGESIZE 0
SQL> set ECHO OFF
SQL> set FEEDBACK OFF
SQL> set HEADING OFF
SQL> spool report.txt
SQL> select sysdate from dual; 
10-JUN-24
SQL> spool off
SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.20.0.0.0
[oracle@rac1 ~]$ cat report.txt
SQL> select sysdate from dual;
10-JUN-24                                                                       
SQL> spool off

 

goal: How To Create a flat file Without Showing Statement Or ''Spool Off''
fact: SQL*Plus

fix:

Create a script, e.g. report.sql, with the following contents:
set NEWPAGE 0
set SPACE 0
set LINESIZE 80
set PAGESIZE 0
set ECHO OFF
set FEEDBACK OFF
set HEADING OFF
spool report.txt
select sysdate from dual;     <--- your SQL statement here
spool off

Run the script from SQL*Plus:
SQL> @report.sql

-----spool 两个..

Case 1
------
In SQL*Plus, how do you spool data to a file with a
.TXT file extension?When you issue the following line of code:SPOOL myfilethis automatically spools data to MYFILE.LST.
How do you spool to MYFILE.TXT?Case 2
------
You are spooling to a filename that the user passes in as
a substitution variable.  How do you append a .TXT file
extension, as opposed to the default .LST, to the 
filename?Sample script:SPOOL &&filenameSELECT * FROM dept/SPOOL OFFIf you enter MYFILE for filename, MYFILE.LST is the default
spooled filename.  How do you spool to MYFILE.TXT?Solution Description:
=====================Case 1
------
Specify the .TXT extension after the filename:SPOOL myfile.txtCase 2
------
If you are spooling to a substitution variable,
specify "..txt" after the substitution variable.SPOOL &&filename..txtSELECT * FROM dept/SPOOL OFFIf you enter MYFILE for filename, this stores data into the
MYFILE.TXT file.Make sure to append 2 periods ("..") to the substitution variable.
If you only include 1 period and enter MYFILE for filename,
this stores data into the MYFILETXT.LST file.

-----------------------加上Oracle SID

How is "@" interpretted when used in spooled file name?
========================================================
When you spool output to file with "@" included in the file name, the "@" is 
replaced with ORACLE_SID value.  

-rw-r--r--. 1 oracle oinstall      500 Jun 10 13:30 TESTcdb1..txt
[oracle@rac1 ~]$ cat testfile1.sql
   spool  &&filename@..txt            ---------------这里不能两个.
   select user from dual;
   select count(*) from dba_tables;
[oracle@rac1 ~]$ cat TESTcdb1..txt

USER                                                                            
--------------------------------------------------------------------------------
SYS                                                                             


  COUNT(*)                                                                      
----------                                                                      
      2202                                                                      

SQL> exit

-rw-r--r--. 1 oracle oinstall       88 Jun 10 13:31 testfile1.sql
-rw-r--r--. 1 oracle oinstall      496 Jun 10 13:31 T11cdb1.txt
[oracle@rac1 ~]$ cat T11cdb1.txt

USER                                                                            
--------------------------------------------------------------------------------
SYS                                                                             


  COUNT(*)                                                                      
----------                                                                      
      2202                                                                      

SQL> -----这里为什么没有exit 因为我用ctrl D退出的
[oracle@rac1 ~]$ cat  testfile1.sql
   spool  &&filename@.txt
   select user from dual;
   select count(*) from dba_tables;
 

----How can you generate a spool file format c:\temp\Overnight_13FEB06.log?

SOLUTION

Sample script:

COLUMN SPOOL_DATE NEW_VALUE FILE_DATE
COLUMN SPOOL_PREFIX NEW_VALUE FILE_PREFIX
COLUMN SPOOL_SUFFIX NEW_VALUE FILE_SUFFIX
SELECT
TO_CHAR(SYSDATE,'YYYYMMDD') SPOOL_DATE,
'c:\temp\Overnight_' SPOOL_PREFIX,
'.log' SPOOL_SUFFIX
FROM
DUAL
/
SPOOL &FILE_PREFIX.&FILE_DATE.&FILE_SUFFIX
/
report
/
SPOOL OFF

[oracle@rac1 ~]$ cat  spooldate.sql
COLUMN SPOOL_DATE NEW_VALUE FILE_DATE
COLUMN SPOOL_PREFIX NEW_VALUE FILE_PREFIX
COLUMN SPOOL_SUFFIX NEW_VALUE FILE_SUFFIX
SELECT
TO_CHAR(SYSDATE,'YYYYMMDD') SPOOL_DATE,
'/tmp/Overnight_' SPOOL_PREFIX,
'.log' SPOOL_SUFFIX
FROM
DUAL
/
SPOOL &FILE_PREFIX.&FILE_DATE.&FILE_SUFFIX
/
report
/
SPOOL OFF
[oracle@rac1 ~]$ 

------How to change spool file name dynamically ?


Each time query executes, the same file should not be overwritten.
It would be helpful in situations where an output of a table is 
monitored at periodic intervals to find the difference.Solution Description
--------------------
COLUMN command with NEW_VALUE can be used to accomplish this.
Have the following three lines before the query -column col1 new_value filename; 
select to_char(sysdate,'DDMONYYHH24MI') col1 from dual; 
spool &&filename..txt select .... ; -- original query 
spool off; Spool file name will contain the date and time so that they are not
overwritten.(NOTE: In the example above, user details / company name / address / email / telephone number represent a fictitious sample (based upon made up data used in the Oracle Demos).  Any similarity to actual persons, living or dead, is purely coincidental and not intended in any manner.)

------------------  script to automatically label each spool filename with a timestamp of the date and time of the script execution.

 

 

Background 

If you must execute a single SQL*Plus script several times (for example because you run a report every week or when running a script in batch mode regularly), and the scriptspools the output to a file, you may want to give a unique name to the spool file for each execution.

Script

The following SQL*Plus script illustrates the timestamp method:
 

column timecol new_value timestamp
select to_char(sysdate,'.MMDDYY_HHMISS') timecol
from sys.dual
/
spool output_&&timestamp
select sysdate from sys.dual
/
spool off

If this script is executed at 12:34:12 AM on October 17, 1999, the output would be spooled to an output file with the name 'output.101799_123412'. You can modify the date format mask ('.MMDDYY_HHMISS') and/or the constant filename portion ('output_') of the example to create different spool filename formats.

Depending on the operating system the spool file will have the suffix .LIS or .LST appended to it's name by default. To replace the default ending with your own, for example '.txt', change the script like suggested below.
 

column timecol new_value timestamp
column spool_extension new_value suffix
select to_char(sysdate,'_MMDDYY_HHMISS') timecol,
'.txt' spool_extension
from sys.dual
/
spool output_&&timestamp&&suffix
select sysdate from sys.dual
/
spool off

You can take this even a step further and give the user of your script control over the file extension of the spool file. For example on UNIX make files can process files of a designated extension. On Microsoft Windows on the other hand most file extensions are associated with certain defined actions when you for example 'Open' or 'Edit' a file.

Replace the literal '.txt' with a variable to let the user of your script decide on an file extension that is useful for them.
 

select to_char(sysdate,'_MMDDYY_HHMISS') timecol,
'.'||&file_ending spool_extension

Please note, this last option is not useful for batch files.

------------------

How to generate sql/select statement from a select statement?
Solution Description
--------------------
This sample will show how to generate a select statement from
a select statement. For example, how to dynamically create a
sql script which contains a set of select statements based on 
column values from the EMP table. Specifically, how to create
a set of select statements based on each deptno value in EMP 
and job='CLERK'?How to create this script?
==========================
[Sample script based on demo table EMP]1. The following is an example of how you can dynamically create selectstatements based on column values in table. For example, how to generateselect statements based on each deptno value and job='CLERK' in EMP table.   Use the following code to create a SQL script (This includes comments 
for better documentation):*****************************************-- Given demo table EMP.
-- The following select statement will generate a sql file withselect statements based on each department number (deptno) andjob = 'CLERK'set heading off
set feedback off
set echo off
spool temp.sqlselect distinct 'select empno, ename, job from emp where deptno='||
deptno
||' and job = '||''''||
JOB
||''''||';' query_statement
from emp
where job = 'CLERK'
/
spool off*****************************************Result
------
Executing the above script will generate temp.sql file.The temp.sql file contains: 
select empno, ename, job from emp where deptno=10 and job = 'CLERK';
select empno, ename, job from emp where deptno=20 and job = 'CLERK';
select empno, ename, job from emp where deptno=30 and job = 'CLERK';You can now execute temp.sql to run the dynamically generated select statements.

相关文章:

SPOOL

-----How to Pass UNIX Variable to SPOOL Command (Doc ID 1029440.6) setenv只有csh才有不行啊PROBLEM DESCRIPTION: You would like to put a file name in Unix and have SQL*Plus read that file name, instead of hardcoding it, because it will change.You want to pa…...

挑战绝对不可能:再证有长度不同的射线

黄小宁 一空间坐标系中有公共汽车A&#xff0c;A中各座位到司机处的距离h是随着座位的不同而不同的变数&#xff0c;例如5号座位到司机处的距离是h3&#xff0c;…h5&#xff0c;…。A移动了一段距离变为汽车B≌A&#xff0c;B中5号座位到司机处的距离h’h3&#xff0c;…h’h5…...

【机器学习】Python与深度学习的完美结合——深度学习在医学影像诊断中的惊人表现

&#x1f525; 个人主页&#xff1a;空白诗 文章目录 一、引言二、深度学习在医学影像诊断中的突破1. 技术原理2. 实际应用3. 性能表现 三、深度学习在医学影像诊断中的惊人表现1. 提高疾病诊断准确率2. 辅助制定治疗方案 四、深度学习对医疗行业的影响和推动作用 一、引言 随着…...

MapStruct的用法总结及示例

MapStruct是一个代码生成器&#xff0c;它基于约定优于配置的原则&#xff0c;使用Java注解来简化从源对象到目标对象的映射过程。它主要用于减少样板代码&#xff0c;提高开发效率&#xff0c;并且通过编译时代码生成来保证性能。 我的个人实践方面是在2021年前那时候在项目中…...

redis 05 复制 ,哨兵

01.redis的复制功能&#xff0c;使用命令slaveof 2. 2.1 2.2 3. 3.1 3.1.1 3.1.2 3.1.3 4 4.1 4.2 例子 5.1 这里是从客户端发出的指令 5.2 套接字就是socket 这里是和redis事件相关的知识 5.3 ping一下...

强大的.NET的word模版引擎NVeloDocx

在Javer的世界里&#xff0c;存在了一些看起来还不错的模版引擎&#xff0c;比如poi-tl看起来就很不错&#xff0c;但是那是人家Javer们专属的&#xff0c;与我们.Neter关系不大。.NET的世界里Word模版引擎完全是一个空白。 很多人不得不采用使用Word XML结合其他的模版引擎来…...

MySQL中所有常见知识点汇总

存储引擎 这一张是关于整个存储引擎的汇总知识了。 MySQL体系结构 这里是MySQL的体系结构图&#xff1a; 一般将MySQL分为server层和存储引擎两个部分。 其实MySQL体系结构主要分为下面这几个部分&#xff1a; 连接器&#xff1a;负责跟客户端建立连 接、获取权限、维持和管理…...

Flink 基于 TDMQ Apache Pulsar 的离线场景使用实践

背景 Apache Flink 是一个开源的流处理和批处理框架&#xff0c;具有高吞吐量、低延迟的流式引擎&#xff0c;支持事件时间处理和状态管理&#xff0c;以及确保在机器故障时的容错性和一次性语义。Flink 的核心是一个分布式流数据处理引擎&#xff0c;支持 Java、Scala、Pytho…...

远程访问及控制

SSH协议 是一种安全通道协议 对通信数据进行了加密处理&#xff0c;用于远程管理 OpenSSH(SSH由OpenSSH提供) 服务名称&#xff1a;sshd 服务端控制程序&#xff1a; /usr/sbin/sshd 服务端配置文件&#xff1a; /etc/ssh/sshd_config ssh存放的客户端的配置文件 ssh是服务端额…...

【代码随想录训练营】【Day 44】【动态规划-4】| 卡码 46, Leetcode 416

【代码随想录训练营】【Day 44】【动态规划-4】| 卡码 46&#xff0c; Leetcode 416 需强化知识点 背包理论知识 题目 卡码 46. 携带研究材料 01 背包理论基础01 背包理论基础&#xff08;滚动数组&#xff09;01 背包 二维版本&#xff1a;dp[i][j] 表示从下标为[0-i]的物…...

html5实现个人网站源码

文章目录 1.设计来源1.1 网站首页页面1.2 个人工具页面1.3 个人日志页面1.4 个人相册页面1.5 给我留言页面 2.效果和源码2.1 动态效果2.2 目录结构 源码下载 作者&#xff1a;xcLeigh 文章地址&#xff1a;https://blog.csdn.net/weixin_43151418/article/details/139564407 ht…...

【内存管理】内存布局

ARM32位系统的内存布局图 32位操作系统的内存布局很经典&#xff0c;很多书籍都是以32位系统为例子去讲解的。32位的系统可访问的地址空间为4GB&#xff0c;用户空间为1GB ~ 3GB&#xff0c;内核空间为3GB ~ 4GB。 为什么要划分为用户空间和内核空间呢&#xff1f; 一般处理器…...

软件试运行方案(Word)

软件试运行方案&#xff08;直接套用实际项目&#xff0c;原件获取通过本文末个人名片直接获取。&#xff09; 一、试运行目的 二、试运行的准备 三、试运行时间 四、试运行制度 五、试运行具体内容与要求...

Redis原理篇——哨兵机制

Redis原理篇——哨兵机制 1.Redis哨兵2.哨兵工作原理2.1.哨兵作用2.2.状态监控2.3.选举leader2.4.failover 1.Redis哨兵 主从结构中master节点的作用非常重要&#xff0c;一旦故障就会导致集群不可用。那么有什么办法能保证主从集群的高可用性呢&#xff1f; 2.哨兵工作原理 …...

web前端的MySQL:跨领域之旅的探索与困惑

web前端的MySQL&#xff1a;跨领域之旅的探索与困惑 在数字化浪潮的推动下&#xff0c;web前端与MySQL数据库似乎成为了两个不可或缺的领域。然而&#xff0c;当我们将这两者放在一起&#xff0c;尝试探索web前端与MySQL之间的交互与关联时&#xff0c;却发现这是一次充满困惑…...

Postgresql源码(135)生成执行计划——Var的调整set_plan_references

1 总结 set_plan_references主要有两个功能&#xff1a; 拉平&#xff1a;生成拉平后的RTE列表&#xff08;add_rtes_to_flat_rtable&#xff09;。调整&#xff1a;调整前每一层计划中varno的引用都是相对于本层RTE的偏移量。放在一个整体计划后&#xff0c;需要指向一个统一…...

Python魔法之旅专栏(导航)

目录 推荐阅读 1、Python筑基之旅 2、Python函数之旅 3、Python算法之旅 4、博客个人主页 首先&#xff0c;感谢老铁们一直以来对我的支持与厚爱&#xff0c;让我能坚持把Python魔法方法专栏更新完毕&#xff01; 其次&#xff0c;为了方便大家查阅&#xff0c;我将此专栏…...

Python第二语言(五、Python文件相关操作)

目录 1. 文件编码的概念 2. 文件的读取操作 2.1 什么是文件 2.2 open()打开函数 2.3 mode常用的三种基础访问模式 2.4 文件操作及案例 3. 文件的写入操作及刷新文件&#xff1a;write与flush 4. 文件的追加操作 5. 文件操作的综合案例&#xff08;文件备份操作&#x…...

Vue3 组合式 API:依赖注入(四)

provide() provide() 函数是用于依赖注入的一个关键部分。这个函数允许你在组件树中提供一个值或对象&#xff0c;使得任何子组件&#xff08;无论层级多深&#xff09;都能够通过 inject() 函数来访问这些值。 import { provide, ref } from vue; export default { setup(…...

Vue如何引入ElementUI并使用

Element UI详细介绍 Element UI是一个基于Vue 2.0的桌面端组件库&#xff0c;旨在构建简洁、快速的用户界面。由饿了么前端团队开发&#xff0c;提供丰富的组件和工具&#xff0c;帮助开发者快速构建高质量的Vue应用&#xff0c;并且以开放源代码的形式提供。 1. VueElementU…...

从CSV文件到3D点云:用Qt+OpenGL打造一个简易的激光雷达数据查看器

从CSV文件到3D点云&#xff1a;用QtOpenGL打造激光雷达数据查看器 激光雷达技术正在重塑自动驾驶、机器人导航和三维测绘的格局。当数百万个空间数据点从激光雷达设备中喷涌而出时&#xff0c;工程师们面临着一个关键挑战&#xff1a;如何快速验证和可视化这些原始数据&#xf…...

内容做了一大堆,流量就是起不来?初创公司低成本获流的真实解法

内容做了一大堆&#xff0c;流量就是起不来&#xff1f;初创公司低成本获流的真实解法 我见过太多这样的团队&#xff1a;每周雷打不动三篇公众号&#xff0c;两条短视频&#xff0c;外加若干条推特&#xff0c;数据面板安安静静&#xff0c;后台没有咨询&#xff0c;评论区只…...

正规全能艺术台制造厂:可靠厂商选择要点解析

正规全能艺术台制造厂选择指南&#xff1a;5大可靠厂商评估要点FAQ“选对全能艺术台制造厂&#xff0c;不是看广告多响&#xff0c;而是看这5个‘隐性指标’——合规资质、自研技术、服务体系、数据安全、内容迭代能力&#xff01;”很多公共文化场馆在采购全能艺术台时&#x…...

开源机械爪资源宝库:从入门到进阶的完整实践指南

1. 项目概述&#xff1a;一个为开源“机械爪”而生的资源宝库如果你对机器人、自动化或者开源硬件感兴趣&#xff0c;最近又在琢磨着给自己的项目加个能抓取、能操作的“手”&#xff0c;那么你很可能已经听说过或者正在寻找“OpenClaw”相关的资料。vincentkoc/awesome-opencl…...

别再手动整理了!用这个油猴脚本,5分钟搞定百度网盘群文件目录导出

百度网盘群文件目录导出神器&#xff1a;油猴脚本极简操作指南 1. 为什么需要群文件目录导出工具 百度网盘作为国内主流的云存储服务&#xff0c;群组文件共享功能被广泛用于团队协作、资源分发等场景。但官方界面存在一个明显的痛点&#xff1a;当群文件数量达到数百甚至上千时…...

Spring Cloud整合XXL-Job避坑指南:调度过期策略选错,你的定时任务可能就白跑了

Spring Cloud微服务中XXL-Job调度策略深度解析与实战避坑 在微服务架构盛行的今天&#xff0c;定时任务作为业务系统中不可或缺的一环&#xff0c;其稳定性和可靠性直接影响着核心业务流程。XXL-Job作为一款轻量级分布式任务调度平台&#xff0c;凭借其简单易用、功能强大的特性…...

5步掌握Mac视频预览革命:QLVideo让你的Finder变身全能播放器

5步掌握Mac视频预览革命&#xff1a;QLVideo让你的Finder变身全能播放器 【免费下载链接】QuickLookVideo This package allows macOS Finder to display thumbnails, static QuickLook previews, cover art and metadata for most types of video files. 项目地址: https://…...

【力扣100题】48.乘积最大子数组

题目描述 给你一个整数数组 nums&#xff0c;请你找出数组中乘积最大的非空连续子数组&#xff08;该子数组中至少包含一个数字&#xff09;&#xff0c;并返回该子数组所对应的乘积。 测试用例的答案是一个 32 位整数。注意&#xff0c;一个只包含一个元素的数组的乘积就是这个…...

3分钟搞定游戏模组:BepInEx插件框架终极入门指南

3分钟搞定游戏模组&#xff1a;BepInEx插件框架终极入门指南 【免费下载链接】BepInEx Unity / XNA game patcher and plugin framework 项目地址: https://gitcode.com/GitHub_Trending/be/BepInEx 想让你的游戏拥有无限可能&#xff1f;厌倦了游戏原有的玩法&#xff…...

用 IDENTITY 数据销毁对象处理个人数据销毁,SAP ILM 场景下的信息检索与合规闭环

做 SAP 系统里的个人数据治理,最怕的不是删除动作本身,而是删除之前没有把数据的来源、用途、保留规则、可检索性和审计链路讲清楚。一个系统里只要出现客户、联系人、消费者、会员、订阅人、业务伙伴、技术访问账号等身份相关对象,围绕这些对象产生的姓名、邮箱、手机号、登…...