SQL 归来
1. PL/SQL 转义
- select order#, ……… from ****
- select col1 from A where col2 like '%\_keywors%' escape '\';
- update A set url = 'homepage.aspx?code=32' || '&' || 'active=0' --&
update A set url = 'homepage.aspx?code=32' || chr(38) || 'active=0' --&
2. EXTRACT
| 日付の要素 | element 引数 | |
| 年 | YEAR | |
| 月 | MONTH | |
| 日 | DAY | |
| 時 | HOUR | |
| 分 | MINUTE | |
| 秒 | SECOND | |
| 時 | タイムゾーン 情報あり |
TIMEZONE_HOUR |
| 分 | TIMEZONE_MINUTE | |
| タイムゾーン名称 | TIMEZONE_REGION | |
| タイムゾーン略称 | TIMEZONE_ABBR | |
SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL
3. TRUNC
SELECT SYSDATE S1,
TRUNC(SYSDATE) S2, --返回当前日期,无时分秒
TRUNC(SYSDATE,'YEAR') YEAR, --返回当前年的1月1日,无时分秒
TRUNC(SYSDATE,'MONTH') MONTH , --返回当前月的1日,无时分秒
TRUNC(SYSDATE,'DAY') DAY --返回当前星期的星期天,无时分秒
FROM DUAL
| S1 | S2 | YEAR | MONTH | DAY |
| 2015/3/11 11:30 | 2015/3/11 | 2015/1/1 | 2015/3/1 | 2015/3/8 |
4. 调用存储过程
DECLARE
MY_STR_DATE VARCHAR2(100);
BEGIN
MY_STR_DATE := 'ASDFASDFASDF';
DBMS_OUTPUT.put_line(MY_STR_DATE);
END;
5. Exists -- 替换 minus 和 intersect
select *
from (select 1 col1, 'a' col2
from dual
union all
select 2 col1, 'b' col2
from dual) t
where not exists (select 'x'
from (select 1 col1, 'a' col2
from dual
union all
select 2 col1, 'b' col2
from dual) w
where t.col1 = w.col1
and t.col2 = w.col2)
另外: 如果select的内容都出自一个表的时候,比如上面的t和w有不同的列,而最终结果只选择t表中的列,最好用Exists
6. 全角/半角转换(link)
SELECT
TO_MULTI_BYTE(SYS.UTL_I18N.TRANSLITERATE('アAあ11','hwkatakana_fwkatakana')) as full_,
TO_single_BYTE(SYS.UTL_I18N.TRANSLITERATE('アAあ11','kana_hwkatakana')) as half
FROM DUAL
| FULL_ | HALF |
| アAあ11 | アAア11 |
7. 用A表的数据更新B表的数据
| CODE | NAME |
| 1 | xxxx |
| 2 | yyyy |
| 3 | zzzz |
| 4 | mmmm |
| 5 | wwwww |
| 6 | ttttt |
| CODE | NAME |
| 1 | 111111 |
| 2 | 222222 |
| 3 | 333333 |
| 9 | 999999 |
方法1:
update a_code s
set s.name =
(select e.name
from a_data e
where e.code = s.code
and rownum < 2)
where s.code in (select code from a_data);
方法2:
merge into a_code t
using a_data d
on (t.code = d.code) -- 带括号
when matched then
update
set t.name = d.name;
| CODE | NAME |
| 1 | 111111 |
| 2 | 222222 |
| 3 | 333333 |
| 4 | mmmm |
| 5 | wwwww |
| 6 | ttttt |
8. (NOT) IN / EXISTS
select code from t where code IN (1,2) => select code from t where code = 1 or code = 2
select code from t where code NOT IN (1,2,null) => select code from t where code <> 1 and code <> 2 and code <> null
code <> null 的值为unknown,所以NOT IN (1,2,null)的返回的结果集为空,如果用not in就需要把含有null的过滤掉
或则改用Exists
select code from t where NOT EXISTS (select 1 from x where t.code = x.code)
9. coalesce / NVL
coalesce (val1, val2, val3) :
coalesce 可以有多个参数,当val1的值为null的时候,返回val2,如果val2也为空,则返回val3,最后一个参数不能为null即可;如果最后一个参数为null,则返回错误。
NVL(val1, val2) :
NVL只有2个参数,当val1为null的时候,直接返回val2,即使val2为null也不出错;如果val1和val2的数据类型不同,oracle会进行隐式转换,如果转换失败,则返回ERROR;
NVL会同时计算val1和val2的值,对于coalesce只是在val1为null的时候,才会去计算val2的值
另:NVL2(val1, 'completed', 'n/a')
当val1的值为null的时候,返回'n/a',否则返回'completed'
10. 游标更新 where current of
cursor c_f is
select a,b from f where length(b) = 5 for update; open c_f;
loop
fetch c_f into v_a, v_b;
exit when c_f%notfound;
update f set a=v_a*v_a where current of c_f;
end loop;
11. Join 写法
select id_num, txt_num, id_lang, txt_lang, txt_trans
from numbers_en
join translations using (id_num)
left join lang using (id_lang);
join translations using id_num 等同于 join translations on numbers_en.id_num = translation.id_num
12. Function VS Procedure: from LINK

13. 赋权限
GRANT SELECT, UPDATE ON "schema1"."table1" TO "schema2" ;
14. Pipelined Table Functions: LINK
Data is said to be pipelined if it is consumed by a consumer (transformation) as soon as the producer (transformation) produces it, without being staged in tables or a cache before being input to the next transformation.
Pipelining enables a table function to return rows faster and can reduce the memory required to cache a table function's results.
create function
gen_numbers(n in number default null)
return array
PIPELINED
as
begin
for i in 1 .. nvl(n,999999999)
loop
pipe row(i);
end loop;
return;
end;
/
-----------------------------------------------------------
/*
select * from TABLE(gen_numbers(3)); COLUMN_VALUE
------------
1
2
3 OR select *
from (
select *
from (select * from table(gen_numbers(49)))
order by dbms_random.random
)
where rownum <= 6
/ COLUMN_VALUE
------------
47
42
40
15
48
23
*/
15. Foreign Key: On delete -> No Action / Cascade / Set null
No Action: prevents deleting a parent when there are children
Cascade: when a referenced parent table row is removed all the child are removed automatically
Set null: set col to null in child when parent table row is removed
16. unicode TO string
'基準日: ' => UNISTR('\57fa\6e96\65e5\003a\3000')
http://unicodelookup.com/ unicode 编码查询
string TO unicode ???
17. 频繁插入删除操作,可能导致高水位线,重新分析下表,可能使对表引用的查询更快一点
analyze table XXX compute statistics
18. LEFT JOIN :)
1. select count from left join B on (A.id = B.id and B.col = 'xxx')
where 1 =1
2. select count(*) from left join B on (A.id = B.id)
where 1 =1 AND B.col = 'xxx'
1,2 的结果可能会不同,
19. running total
Table XXX:

select tag, insdate, amount,
nvl(lag(amount) over(partition by tag order by insdate), 0) lag_total,
nvl(lead(amount) over(partition by tag order by insdate), 0)
from xxx

20. check English/digital characters only
[1]
select 1 from dual where REGEXP_LIKE('sd巣', '[^ -~]', 'i') union all
select 1 from dual where REGEXP_LIKE('abc', '[^ -~]', 'i')
[2]
select translate( 'sd巣abcd', chr(0) || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,- ', chr(0) ) from dual
21. NOCOPY
TYPE connection IS RECORD (
host VARCHAR2(255), -- Host name of SMTP server
port PLS_INTEGER, -- Port number of SMTP server
tx_timeout PLS_INTEGER, -- Transfer time-out (in seconds)
private_tcp_con utl_tcp.connection, -- For internal use only
private_state PLS_INTEGER -- For internal use only
); FUNCTION helo(c IN OUT NOCOPY connection,
domain IN VARCHAR2) RETURN reply;
NOCOPY is a IN OUT COPY Which is used avoid the overhad of copying IN OUT Parameter Values.
If IN OUT parameter returns huge records we can specify the NOCOPY to copy the values.
The PLSQL Engine first makes a copy of the record and then during program executing makes a changes to that copy.
22. bitwise operator:BITAND
oracle中只有BITAND函数;
位或:
BITOR(x,y) = (x + y) - BITAND(x, y);
异或:
BITXOR(x,y) = BITOR(x,y) - BITAND(x,y) = (x + y) - BITAND(x, y) * 2;
应用:赋权,权限判断的时候可以用这个(判断一个角色是否有多个权限)
23. LISTAGG
select listagg(code, ';') within group (order by code) from table_codes
结果:001; 002; 003
24.
SQL 归来的更多相关文章
- struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决
最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myec ...
- 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目
最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...
- SQL Server 大数据搬迁之文件组备份还原实战
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...
- Sql Server系列:分区表操作
1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...
- SQL Server中的高可用性(2)----文件与文件组
在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...
- EntityFramework Core Raw SQL
前言 本节我们来讲讲EF Core中的原始查询,目前在项目中对于简单的查询直接通过EF就可以解决,但是涉及到多表查询时为了一步到位就采用了原始查询的方式进行.下面我们一起来看看. EntityFram ...
- 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...
- 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...
随机推荐
- datatable和list的转换
在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有list<T>检索方便.但是数据以ILIST形式返回,就为我们在.NET中使用传统的数据绑定造成了不便.下 ...
- [AlwaysOn Availability Groups]AlwaysOn Ring Buffers
AlwaysOn Ring Buffers 一些AlwaysOn的诊断信息可以从SQL Server ring buffers.或者从sys.dm_os_ring_buffers.ring buffe ...
- Oracle数据库的 增、删、改、查
有时候数据库的查询语句一时想不起来,或不确定是不是语句写的正确,现在整理了一下标准的基本查询语句,便于以后牢记: .数据操作语言 DML:添加(insert into).修改(update set ...
- MySql.Data.Entity 在EF中解析uint的枚举时有BUG
当枚举继承uint类型时无法获取值.
- netty学习资料
netty学习资料推荐官方文档和<netty权威指南>和<netty in action>这两本书.下面收集下网上分享的资料 netty官方参考文档 Netty 4.x Use ...
- 浅谈Virtual Machine Manager(SCVMM 2012) cluster 过载状态检测算法
在我们使用scvmm2012的时候,经常会看到群集状态变成了这样 点开看属性后,我们发现是这样 . 发现了吗?Over-committed,如果翻译过来就是资源过载,或者说资源过量使用了,那么这个状态 ...
- 学习《Hardware-Efficient Bilateral Filtering for Stereo Matching》一文笔记。
个人收藏了很多香港大学.香港科技大学以及香港中文大学里专门搞图像研究一些博士的个人网站,一般会不定期的浏览他们的作品,最近在看杨庆雄的网点时,发现他又写了一篇双边滤波的文章,并且配有源代码,于是下载下 ...
- 国内优秀的Android资源
因为一些大家都知道的原因,Android很多官方出品的优秀开发资源在国内无法访问. 国内的同行们对此也做出了很多努力,有很多朋友通过各种手段把很多优秀的资源搬运到了国内,为国内android开发者提供 ...
- ubuntu-Linux系统读取USB摄像头数据(gspca)
将摄像头图像保存为jpg格式.摄像头需要是gspca免驱的.uvc若用uvc格式的需要在图像中插入Huffman表.否则无法正常显示. 程序代码: #include <stdio.h> # ...
- 安全测试 - SQL注入
1. 工具测试: 使用SQLMAP进行扫描 2. 手工测试: 观察参数的值value是否为数字型.如果是数字型进行数字型测试,否则跳到第4步进行字符型测试(例如如果出现a那说明是字符型,如果出现2则将 ...