mysql存储过程 怎么用like 得不到结果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql存储过程 怎么用like 得不到结果相关的知识,希望对你有一定的参考价值。
BEGIN
select count(id) from userlog where keypress = 'begin' and createtime like 'shijian%' GROUP BY userid;
END
上面shijian 是in参数,我如果直接写2011-12-21 那就能查询到结果,如果用shijian这个参数传入的话,就的不到结果集
大概如下:
BEGIN
select count(id) from userlog where keypress = 'begin' and createtime like '‘’+ shijian + ‘’‘%' GROUP BY userid;
END追问
还是不行 不是用+ 我用的concat连的 也不行
追答试试:
select count(id) from userlog where keypress = 'begin' and createtime like concat(’\‘’,shijian,’\'%') GROUP BY userid;
或者:
select count(id) from userlog where keypress = 'begin' and createtime like concat("'", shijian ,"'%") GROUP BY userid;
BEGIN
select count(id) from userlog where keypress = 'begin' and createtime like '2011-12-21%' GROUP BY userid;
END
如果这样就能有结果
必须得2011-12-21 时间都是2012-12-12 12:12:12这个中类型的 但是我查询得查询具体2011-12-21这一天的数据 转换成时间戳 怎么查啊
追答UNIX_TIMESTAMP(2012-12-12 12:12:12) (转换成时间戳函数)
select count(id) from userlog where keypress = 'begin' and createtime like 'UNIX_TIMESTAMP(2012-12-12 12:12:12)%' GROUP BY userid;
ORACLE 存储过程怎么返回临时表结果集
如题,我在ORACLE怎么返回一个临时表里的数据
MD,真TM麻烦,比SQLSERVER二多了。。。。
CREATE OR REPLACE PROCEDURE Report_Month_Responsibility(
o_cur OUT SYS_REFCURSOR
)
IS
STR VARCHAR2(200);
BEGIN
STR:=' CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID INT,
ANAME VARCHAR2(20)
) ON COMMIT PRESERVE ROWS';
execute immediate STR;
STR:='INSERT INTO REPROTTEST(ID,ANAME) VALUES(1,''1'')';
execute immediate STR;
STR:='SELECT * FROM REPROTTEST';
OPEN o_cur FOR STR; -- 给游标变量赋值
END Report_Month_Responsibility;
上面是写的一个测试的,麻烦高手帮俺瞅瞅,看有没要改的地方,或者是测试会有什么问题,我测试好像报错了。。不知道怎么出来一个结果集。。。。。
SHEN的答案试过了,首先建的时候就过不去
select count(*) into tb_count from dba_tables where table_name='REPROTTEST';
这里会报错。。。。。
我把你的IF那段注释了,直接
STR:=' CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID INT,
ANAME VARCHAR2(20)
) ON COMMIT PRESERVE ROWS';
execute immediate STR;
然后出去测试也会报错,ORA-01031: insufficient privileges
SQL> CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
2 ID NUMBER,
3 ANAME VARCHAR2(20)
4 ) ON COMMIT DELETE ROWS;(也可以用PRESERVER ROWS,看实际需求)
Table created
SQL> create or replace procedure report_month_responsibility(
2 o_cur out sys_refcursor)
3 as
4 begin
5 insert into reprottest(id,aname) values(1,'1');
6 open o_cur for select * from reprottest;
7 end report_month_responsibility;
8 /
Procedure created
SQL> set serverout on
SQL> declare
2 v_id number;
3 v_aname varchar2(20);
4 o_cur sys_refcursor;
5 begin
6 report_month_responsibility(o_cur);
7 fetch o_cur into v_id,v_aname;
8 while o_cur%found loop
9 dbms_output.put_line('输出结果:'||v_id||','||v_aname);
10 fetch o_cur into v_id,v_aname;
11 end loop;
12 commit;
13 end;
14 /
输出结果:1,1
PL/SQL procedure successfully completed
你的那个ORA-01031: insufficient privileges,是权限不足的问题。
SQL代码:
CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID NUMBER,
ANAME VARCHAR2(20)
) ON COMMIT DELETE ROWS;
create or replace procedure report_month_responsibility(
o_cur out sys_refcursor)
as
begin
insert into reprottest(id,aname) values(1,'1');
open o_cur for select * from reprottest;
end report_month_responsibility;
declare
v_id number;
v_aname varchar2(20);
o_cur sys_refcursor;
begin
report_month_responsibility(o_cur);
fetch o_cur into v_id,v_aname;
while o_cur%found loop
dbms_output.put_line('输出结果:'||v_id||','||v_aname);
fetch o_cur into v_id,v_aname;
end loop;
commit;
end;
/ 参考技术A STR:=' CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID INT,
ANAME VARCHAR2(20)
) ON COMMIT PRESERVE ROWS';
-------------------------------------------------------------------
这个是全局临时表,oracle下临时表在会话断时不会删除表定义的,仅是清掉记录罢啦(这个跟sql server是不同的).所以,你的大概可以改成这样:
CREATE OR REPLACE PROCEDURE Report_Month_Responsibility(
o_cur OUT SYS_REFCURSOR
)
IS
STR VARCHAR2(200);
tb_count INT;
BEGIN
--先判断全局临时表是否存在,没存在则重新建立:
select count(*) into tb_count from dba_tables where table_name='REPROTTEST';
if tb_count=0 then
STR:=' CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID INT,
ANAME VARCHAR2(20)
) ON COMMIT PRESERVE ROWS';
execute immediate STR;
end if;
STR:='INSERT INTO REPROTTEST(ID,ANAME) VALUES(1,''1'')';
execute immediate STR;
COMMIT;
STR:='SELECT * FROM REPROTTEST';
OPEN o_cur FOR STR; -- 给游标变量赋值
END Report_Month_Responsibility;
/***********************************************************/
下面是调用存储过程并返回记录中每个字段值的调用示例:
declare
v_ID INT;
v_ANAME VARCHAR2(20);
--定义游标:
v_account_cur SYS_REFCURSOR;
begin
--调用存储过程:
Report_Month_Responsibility(v_account_cur);
fetch v_account_cur into v_ID,v_ANAME;
--用循环显示游标中的记录:
while v_account_cur%found loop
dbms_output.put_line('The value of column ID is: '||v_ID);--打引列ID
dbms_output.put_line('The value of column ANAME is: '||v_ANAME); --打引列ANAME
fetch v_account_cur into v_ID,v_ANAME;
end loop;
close v_account_cur;
execute immediate 'truncate TABLE REPROTTEST';
exception
when others then
dbms_output.put_line(sqlerrm);
end; 参考技术B ORA-01031: insufficient privileges
这个错是否你没有DBA权限或者是create any table的权限。
没用过临时表,不过永久表你这样写是OK的。 参考技术C 为什么用临时表,能用sql搞定的就用sql搞定
以上是关于mysql存储过程 怎么用like 得不到结果的主要内容,如果未能解决你的问题,请参考以下文章