存储过程sql语句
select count(virtualacc) into v_count from T_ATMMONITOR WHERE virtualacc = v_number;
⽤于存储过程中,是把count(virtualacc)统计的数量值赋予变量v_count,在后续的存储过程中调⽤v_count变量。
在sql语句中
select into from与insert into select 区别
语法:
insert into table2(field1,field2,。。。)select value1,value2,。。。from table1
要求table2必须存在
truncate,delete,drop三者的区别
delete 删除表内容,不释放空间,保留表结构
drop删除表内容和表机构,释放空间
table2存在,可以向table2加⼊符合列类型的内容,包括常量,注意,导⼊常量后,该列的所有值都是常量,如图所⽰ insert into T_ATMMONITOR (
ATMNUM
,VIRTUALACC
,CORPID
,INVMONEY
,DAYMONEY
,DATAGETTIME
,CUTOFFMONEY
,CUTOFFTIME
) select atmnum, v_number, corpid, v_now_money, v_out_money, current TIMESTAMP, 0, current TIMESTAMP
from t_atminfo where VIRTUALACC = v_number;
注意在t_atminfo表中不存在v_now_money,v_out_money,0这样的字段值,这些可以作为常量插⼊到t_atmmonitor
select into from语句
语法
select value1,value2 into table2 from table1
select ID,Name into table1 from table2
要求table2不存在,因为在插⼊的时候,系统⾃动创建table2,如果之前table2已经存在,系统提⽰表已经存在
那如果我想要MyTable2中的Name列,那我也仅仅是需要把Name列select出来,然后添加到新表中即可。
replace函数
replace(c1,c2[,c3])
在字符串c1中到字符串c2,替换成c3.
若c3为空,则在c1中删除所有c2.
replace('123123tech', '123'); would return 'tech'
replace('123tech123', '123'); would return 'tech'
replace('222tech', '2', '3'); would return '333tech'
replace('0000123', '0'); would return '123'
replace('0000123', '0', ' '); would return ' 123'
nvl函数
if else 类似
nvl(str,1)
if str ==null
return 1;
else return str;
coalesce函数
语法 COALESCE ( expression [ ,...n ] )
返回表达式中第⼀个⾮空表达式,如有以下语句:
SELECT COALESCE(NULL,NULL,3,4,5) FROM dual
其返回结果为:3
case when的⽤法
在储存过程中,这句是什么意思SUM(CASE WHEN ISNULL(A.ReceiveQty,0)>0 THEN 1 ELSE 0 END) AS SumReceiveRolls,
A表⾥ReceiveQty⼤于0,值为1,否则为0,
然后把这些1,0加起来,当成⼀列(SumReceiveRolls)查询出来。
case coalesce(c.limit, 1) when 0 then 1 else c.limit end
coalesce函数是去null的,如果c.limit值不为null,则coalesce(c.limit,1)就是c.limit本⾝值
DB2查询表中前10条记录
select * from t_user where corpid='901010300' order by userid desc fetch first N rows only
oracle数据库查询
select * from t_user where rownum<=10
timestamp数据类型查询语句
select * from t_authlog where to_char(logtime ,'yyyymmdd hh24:mi:ss')>'20150316 23:59:59'
存储过程解析
SELECT count(PLANNUM) into v_count FROM T_CASHREPNPLAN where CORPID = v_corpid and plannum = v_plannum and PLANSTATE = '04';
注:count(plannum)转换成into v_count的变量
if v_count = 0 then 注:如果这个变量统计为0,显⽰97
set v_ret = '97';
goto GOTO_RET;
end if;
数据库字段是timestamp类型,查询区间的数据
select * from t_authlog where logtime between timestamp('2015-3-18 10:10:10') and timestamp('2015-3-19 15:40:20')表中增加列,并限定默认字段的值
sql存储过程实例alter table t_warning add column overdue char(1) with default '0';
查询当前⽇期的数据
db2 => select * from t_compinvmonitor where date(gettime) = current date
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论