只读事务和sql函数

只读事务

只读事务是指只允许执行 查询操作,而不允许执行任何的dml(增、删、改)操作的事务。使用只读事务可以确保用户只能取个时刻的数据。

机票代售业务,每天的一个时刻统计今天的销售量,这个就使用只读事务,

设置只读事务以后,尽管其他绘画可能会提交事务,但是只读事务将不再取最新数据的变化,从而保证可以取的特定时间点的数据信息。

 

设置只读事务

Set transaction read only

 

 

SQL函数的使用

 

字符函数

字符函数式oracle中最长用的函数:

1. lower(char):将字符串转化成小写的格式

2. upper(char): 将字符串转化成大写的格式

3. length(char):返回字符串的长度

4. substr(char,m,n):取字符串的字串

 

例子

1. 将所有员工的名字按照小写的方式显

select lower(ename) from emp;

 

2. 将所有员工的名字按照大写的方式显示

select upper(ename) from emp;

3. 显示正好为6个字符的员工的姓名

select *from emp where length(ename)=6;

4. 显示所有员工名字的前4个字符

select substr(ename,1,4)from emp ;

5. 以首字母大写的方式显示所有员工的姓名

select upper(substr(ename,1,1)) from emp;

6. 以首字母大写后面字母为小写的方式显示所有员工的姓名

a. 取出首字母为大写:select upper(substr(ename,1,1)) from emp;

b. 第二个字符到最后为小写:select lowersubstr(ename,2,length(ename)-1from emp;

c. Select upper(substr(ename,1,1))||lower(substr(ename,2,length(ename)-1)) from emp;

d. 合并这两个内容:

select upper(substr(ename,1,1))||lower(substr(ename,2,length(ename)-1))from emp;

    

 

replace (修改的字段,‘原字符‘修改后的字符’);

 

名字中的a替换成&

select replace(ename ,'A','&')from emp ;

instrchar_1,char_2,[n[,m]])取出字串在字符串中的位置

 

 

 

数学函数

数学函数的输入参数和返回值得数据类型都是数字类型。数学函数包括:cos,cosh,exp,ln,log,sin,sinh,sqrt,tan,tanh,acos,asin,atan,round,最常用的:

1. round(n,[m]);  用于执行四舍五入,如果去掉m就是四舍五入到整数,m为正数的时候,则四舍五入到小数点的m位后,如果为负数则四舍五入到小数点的m位前。

select ename ,(round(sal)+round(comm))*13 from emp;

select ename ,(round(sal,1)+round(comm))*13 from emp;

 

2. trunk(n,[m]);用于截取数字,如果省掉m,就截取小数部分,如过m是正数就截取到小数点的m位后,如果m是负数,则截取到小数点的前m

select ename ,trunc(comm,2), comm from emp where ename='小鸡';

3. mod(m,n); 取模函数

select mod(10,2) from dual;\\dual表是用来测试的表(亚元表)再做oracle 测试的,虚拟的表

4. floor(n);返回小于或是等于n的最大整数(再这个整数向下取整)

select ename ,floor(comm) ,comm from emp where ename=scott;

5. ceil(n)返回大于或者等于n的最小整数(再这个整数向上取整)

select ename ,ceil(comm) ,comm from emp where ename=scott;

 

对数字的处理,长用再财务系统和银行系统中,不同的处理方法,对财务的报表有不同的结果。

 

显示在一个月为30天的情况下所有员工的日薪。忽略余数。

select trunc(sal/30),ename from emp; 

select floor(sal/30),ename from emp; 

 

自己备用的,对别人没有用 CREATE FUNCTION SplitStr (@splitString varchar(8000), @separate varchar(10)) RETURNS @returnTable table(id int, col_Value varchar(50)) AS BEGIN declare @thisSplitStr varchar(50) declare @thisSepIndex int declare @lastSepIndex int declare @i int set @lastSepIndex = 0 set @i = 1 if Right(@splitString ,len(@separate)) @separate set @splitString = @splitString + @separate set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex) while @lastSepIndex <= @thisSepIndex begin set @thisSplitStr = SubString(@splitString ,@lastSepIndex,@thisSepIndex-@lastSepIndex) set @lastSepIndex = @thisSepIndex + 1 set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex) insert into @returnTable values(@i, @thisSplitStr) set @i = @i + 1 end return END go --drop procedure sp_add_userFunction create procedure sp_add_userFunction @functionList varchar(5000), @userId varchar(50) as DECLARE @count INTEGER DECLARE @index INTEGER declare @functionId varchar(50) set @count = (select count(*) from SplitStr(@functionList,',')) set @index = 0 begin transaction delete from xt_user_function where user_id = @userId if @@error 0 begin rollback transaction--发生错误则回滚事务,无条件退出l return end while @index<@count begin set @functionId = (select col_Value from SplitStr(@functionList,',') where id = @index + 1) insert into xt_user_function(function_id, user_id) values (@functionId, @userId) SET @index=@index+1 end if @@error 0 begin rollback transaction--发生错误则回滚事务,无条件退出l return end commit transaction --两条语句都完成,提交事务 go
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值