2016-01-06
1.查看表结构
DESC customers;
2.编辑SQL语句
SQL> select customer_id,first_name,last_name from customers where customer_id = 1; CUSTOMER_ID FIRST_NAME LAST_NAME ----------- ---------- ---------- 1 John Brown SQL> 1 1* select customer_id,first_name,last_name SQL> append , dob --在行尾添加", dob" 1* select customer_id,first_name,last_name, dob SQL> list --查看sqlplus缓存区所有行 1 select customer_id,first_name,last_name, dob 2 from customers 3* where customer_id = 1 SQL> change /customer_id = 1/customer_id = 2 --将最后一行"customer_id = 1"改为"customer_id = 2" 3* where customer_id = 2 SQL> run --执行sqlplus缓存区的查询,同/ 1 select customer_id,first_name,last_name, dob 2 from customers 3* where customer_id = 2 CUSTOMER_ID FIRST_NAME LAST_NAME DOB ----------- ---------- ---------- --------- 2 Cynthia Orange 05-FEB-68 SQL> / --执行sqlplus缓存区的查询,同run CUSTOMER_ID FIRST_NAME LAST_NAME DOB ----------- ---------- ---------- --------- 2 Cynthia Orange 05-FEB-68
3.保存、检索并运行文件
SQL> select customer_id,first_name,last_name from customers where customer_id = 1; CUSTOMER_ID FIRST_NAME LAST_NAME ----------- ---------- ---------- 1 John Brown SQL> save /tmp/cust_query.sql --将sqlplus缓存区的内容保存到磁盘目录 Created file /tmp/cust_query.sql SQL> get /tmp/cust_query.sql --将磁盘上的脚本读入sqlplus缓存区 1 select customer_id,first_name,last_name 2 from customers 3* where customer_id = 1 SQL> / CUSTOMER_ID FIRST_NAME LAST_NAME ----------- ---------- ---------- 1 John Brown SQL> start /tmp/cust_query.sql --执行磁盘目录上的sql脚本 CUSTOMER_ID FIRST_NAME LAST_NAME ----------- ---------- ---------- 1 John Brown
SQL> define _editor = ‘vim‘; --改变默认编辑器
SQL> edit --编辑sqlplus缓存区的内容 Wrote file afiedt.buf 1 select customer_id,first_name,last_name 2 from customers 3* where customer_id = 2 SQL> / CUSTOMER_ID FIRST_NAME LAST_NAME ----------- ---------- ---------- 2 Cynthia Orange
SQL> spool /tmp/cust_results.txt --将sqlplus的输出结果保存到磁盘文件中 SQL> / CUSTOMER_ID FIRST_NAME LAST_NAME ----------- ---------- ---------- 2 Cynthia Orange SQL> spool off
4.格式化列
column product_id format 99 column name heading product_name format a13 word_wrapped column description format a13 word_wrapped column price format $99.99 select product_id,name,description,price from products where product_id < 6;
5.设置页面大小
set pagesize 100 --设置一页显示的行数 --页面大小最大为50000,默认14
6.设置行大小
set linesize 50 --设置一行显示的字符数,默认80
7.清除列格式
column product_id clear clear columns
8.使用变量
select product_id,name,price from products where product_id = &v_product_id; --使用变量 &v_product_id Enter value for v_product_id: 2 old 3: where product_id = &v_product_id new 3: where product_id = 2 PRODUCT_ID product_name PRICE ---------- ------------- ------- 2 Chemistry $30.00 SQL> / Enter value for v_product_id: 3 old 3: where product_id = &v_product_id new 3: where product_id = 3 PRODUCT_ID product_name PRICE ---------- ------------- ------- 3 Supernova $25.99
SQL> set verify off --禁止显示旧行和新行 SQL> / Enter value for v_product_id: 4 PRODUCT_ID product_name PRICE ---------- ------------- ------- 4 Tank War $13.95
SQL> set verify on --重新显示新旧行 SQL> / Enter value for v_product_id: 1 old 3: where product_id = &v_product_id new 3: where product_id = 1 PRODUCT_ID product_name PRICE ---------- ------------- ------- 1 Modern $19.95 Science
SQL> set define ‘#‘ --修改变量定义符为‘#‘ select product_id,name,price from products where product_id = #v_product_id; Enter value for v_product_id: 4 old 3: where product_id = #v_product_id new 3: where product_id = 4 PRODUCT_ID product_name PRICE ---------- ------------- ------- 4 Tank War $13.95 SQL> set define ‘&‘ --将变量定义符改回‘&‘
select name,&v_col from &v_table where &v_col = &v_val; --使用变量替换表名和列名 Enter value for v_col: product_type_id old 1: select name,&v_col new 1: select name,product_type_id Enter value for v_table: products old 2: from &v_table new 2: from products Enter value for v_col: product_type_id Enter value for v_val: 1 old 3: where &v_col = &v_val new 3: where product_type_id = 1
select name,&&v_col from &v_table where &&v_col = &v_val; --使用&&避免重复输入变量 Enter value for v_col: product_type_id old 1: select name,&&v_col new 1: select name,product_type_id Enter value for v_table: products old 2: from &v_table new 2: from products Enter value for v_val: 1 old 3: where &&v_col = &v_val new 3: where product_type_id = 1
SQL> define v_product_id = 4 --使用define命令定义变量 SQL> define v_product_id DEFINE V_PRODUCT_ID = "4" (CHAR) SQL> select product_id,name,price from products 3 where product_id = &v_product_id; old 3: where product_id = &v_product_id new 3: where product_id = 4 PRODUCT_ID product_name PRICE ---------- ------------- ------- 4 Tank War $13.95 SQL> define DEFINE _DATE = "06-JAN-16" (CHAR) DEFINE _CONNECT_IDENTIFIER = "unicode" (CHAR) DEFINE _USER = "STORE" (CHAR) DEFINE _PRIVILEGE = "" (CHAR) DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR) DEFINE _EDITOR = "ed" (CHAR) DEFINE _O_VERSION = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR) DEFINE _O_RELEASE = "1102000400" (CHAR) DEFINE V_COL = "product_type_id" (CHAR) DEFINE V_PRODUCT_ID = "4" (CHAR)
SQL> accept v_customer_id number format 99 prompt ‘Customer id: ‘ --使用accept命令定义并设置变量 Customer id: 4 SQL> accept v_date date format ‘DD-MON-YYYY‘ prompt ‘Date: ‘ Date: 06-MAY-2012 SQL> accept v_password char prompt ‘Password: ‘ hide Password: SQL> define DEFINE _DATE = "06-JAN-16" (CHAR) DEFINE _CONNECT_IDENTIFIER = "unicode" (CHAR) DEFINE _USER = "STORE" (CHAR) DEFINE _PRIVILEGE = "" (CHAR) DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR) DEFINE _EDITOR = "ed" (CHAR) DEFINE _O_VERSION = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR) DEFINE _O_RELEASE = "1102000400" (CHAR) DEFINE V_COL = "product_type_id" (CHAR) DEFINE V_PRODUCT_ID = "4" (CHAR) DEFINE V_CUSTOMER_ID = 4 (NUMBER) DEFINE V_DATE = "06-MAY-2012" (CHAR) DEFINE V_PASSWORD = "1234567" (CHAR)
SQL> undefine v_col SQL> undefine v_product_id SQL> undefine v_customer_id SQL> undefine v_date SQL> undefine v_password --使用undefine命令删除变量 SQL> define DEFINE _DATE = "06-JAN-16" (CHAR) DEFINE _CONNECT_IDENTIFIER = "unicode" (CHAR) DEFINE _USER = "STORE" (CHAR) DEFINE _PRIVILEGE = "" (CHAR) DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR) DEFINE _EDITOR = "ed" (CHAR) DEFINE _O_VERSION = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR) DEFINE _O_RELEASE = "1102000400" (CHAR)
9.创建简单报表
vim /tmp/report1.sql
--suppress display of the statements and verification message set echo off --禁止显示脚本中的SQL语句 set verify off --禁止显示验证消息 select product_id,name,price from products where product_id = &v_product_id; --使用临时变量v_product_id
SQL> @ /tmp/report1.sql Enter value for v_product_id: 2 PRODUCT_ID product_name PRICE ---------- ------------- ------- 2 Chemistry $30.00
vim /tmp/report2.sql
--suppress display of the statements and verification message set echo off set verify off accept v_product_id number format 99 prompt ‘Enter product id: ‘ --使用已定义变量v_product_id select product_id,name,price from products where product_id = &v_product_id; --clear up undefine v_product_id
SQL> @ /tmp/report2.sql Enter product id: 4 PRODUCT_ID product_name PRICE ---------- ------------- ------- 4 Tank War $13.95
vim /tmp/report3.sql
--suppress display of the statements and verification message set echo off --禁止显示脚本中的SQL语句 set verify off --禁止显示验证消息 select product_id,name,price from products where product_id = &1; --向脚本中的变量传递值
SQL> @ /tmp/report3.sql 4 PRODUCT_ID product_name PRICE ---------- ------------- ------- 4 Tank War $13.95
vim /tmp/report4.sql
--suppress display of the statements and verification message set echo off --禁止显示脚本中的SQL语句 set verify off --禁止显示验证消息 select product_id,product_type_id,name,price from products where product_id = &1 and price > &2; --向脚本中的多个变量传递值
vim /tmp/report5.sql
--添加页眉 ttitle left ‘Run date: ‘ _date center ‘Run by the‘ sql.user ‘ user‘ right ‘Page: ‘ format 999 sql.pno skip 2 --添加页脚 btitle center ‘Thanks for running the report‘ right ‘Page: ‘ format 999 sql.pno set echo off set verify off set pagesize 15 set linesize 70 clear columns column product_id heading id format 99 column name heading ‘Product Name‘ format a20 word_wrapped column description heading Description format a30 word_wrapped column price heading Price format $99.99 select product_id,name,description,price from products; clear columns ttitle off btitle off
vim /tmp/report6.sql
--计算小计 break on product_type_id --根据列值的范围分隔输出结果 compute sum of price on product_type_id --计算一列的值 set echo off set verify off set pagesize 20 set linesize 70 clear columns column price heading Price format $999.99 select product_type_id,name,price from products order by product_type_id; clear columns
10.帮助信息
help help index
11.自动生成SQL语句
select ‘drop table ‘ || table_name||‘;‘ from user_tables order by table_name; ‘DROPTABLE‘||TABLE_NAME||‘;‘ ------------------------------------------ drop table CUSTOMERS; drop table EMPLOYEES; drop table PRODUCTS; drop table PRODUCT_TYPES; drop table PURCHASES; drop table SALARY_GRADES;
【参考资料】
[1] Jason Price.精通Oracle Database 12c SQL&PLSQL编程(第3版).[M].北京:清华大学出版社,2014
时间: 2024-10-31 03:37:50