plsql常用语句

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

说明:复制表(只复制结构,源表名:a 新表名:b)

SQL: select * into b from a where 1<>1

说明:拷贝表(拷贝数据,源表名:a 目标表名:b)

SQL: insert into b(a, b, c) select d,e,f from b;

说明:显示文章、提交人和最后回复时间

SQL: select a.title,ername,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

说明:外连接查询(表名1:a 表名2:b)

SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

说明:日程安排提前五分钟提醒

SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

说明:两张关联表,删除主表中已经在副表中没有的信息

SQL:

delete from info where not exists ( select * from infobz where info.infid=infobz.infid )

说明:--

SQL:

SELECT A.NUM, , B.UPD_DATE, B.PREV_UPD_DATE

FROM TABLE1,

(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE

FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND

FROM TABLE2

WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X,

(SELECT NUM, UPD_DATE, STOCK_ONHAND

FROM TABLE2

WHERE TO_CHAR(UPD_DATE,'YYYY/MM') =

TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM') ¦¦ '/01','YYYY/MM/DD') - 1, 'YYYY/MM') ) Y, WHERE X.NUM = Y.NUM (+)

AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND ) B

WHERE A.NUM = B.NUM

说明:--

SQL:

select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名称='"&strdepartmentname&"' and 专业名称='"&strprofessionname&"' order by 性别,生源地,高考总成绩

说明:从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)

SQL:

SELECT erper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear,

说明:四表联查问题:

SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

说明:得到表中最小的未使用的ID号

SQL:

SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID FROM Handle WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)

查找重复的记录

select * from fish where fish_name in

(select fish_name from

(select fish_name,count(fish_name) from fish

group by fish_name

having count(fish_name)>1));

统计某字段中所有不同的记录的个数

select nsrbm,count(nsrbm)

from t11_nsrjbqk

where ...

group by nsrbm

having count(nsrbm)>1

SELECT *

FROM letters

WHERE ((.ID IN (SELECT ID FROM letters As Tmp GROUP BY ID HAVING Count(*)>1 )))

ORDER BY ID;

在SQL Enterprise Manager裡面查詢Access裡面的數據

SELECT *

FROM OPENROWSET('msdasql', 'dsn=odbc數據源名;trusted_connection=yes',

'select * from table')

SQL中isnull函数的作用是将将两个参数字段中不为空的值取出

SELECT t1.a, t1.b, ISNULL(t1.c, t2.d)

FROM t1, t2

WHERE t1.a = t2.a

为查询的结果编上序列号

select IDENTITY(int,1,1)as id,a,b,c into #1 from table

select * from #1

SELECT rowno =

(SELECT SUM(row)

FROM (SELECT 1 AS row, *

FROM table) AS a

WHERE a.autoid <= b.autoid), *

FROM table b

ORDER BY autoid

日期型字段的操作

select * from inetsrvlog where

datepart(mm,logtime)= 3

and datepart(dd,logtime)>=1

and datepart(dd,logtime)<=15

insert into tablename (...,..) values ...,...

PL/SQL精典编程例:

說明:用SCOTT/TIGER登入ORACLE,執行看看效果,可以將某個字段中的多行放入同一行,以前以為不可能用一條SQL實現在,竟然可以。主要理解lead() over(),row_number() over()就可以啦,很奇妙的ORACLE吧,有誰有更好的辦法或SQL

相关文档
最新文档