Home > Database > Mysql Tutorial > body text

Oracle 存储过程中使用date 时、分、秒丢失

WBOY
Release: 2016-06-07 16:43:24
Original
1275 people have browsed it

今天有一开发兄弟找我,说Oracle 出现一奇怪现象,在存储过程中赋date类型的值,时、分、秒都丢失了,下面来做个试验:

今天有一开发兄弟找我,说Oracle 出现一奇怪现象,在存储过程中赋date类型的值,时、分、秒都丢失了,下面来做个试验:
 
SQL> drop table test purge;
 SQL> create table test
    (
      fill_date  date
    );
 SQL> insert into test values(sysdate);
 SQL> commit;
 SQL> select to_char(fill_date,'yyyy-MM-dd HH24:mi:ss') from test;
 TO_CHAR(FILL_DATE,'
 -------------------
 
2014-07-18 17:47:22
 
SQL> CREATE OR REPLACE PROCEDURE test_p
 is
 s_sql varchar2(500);
 s_date date;
 begin
 s_date:= to_date('2014-07-18 17:24:32','yyyy-MM-dd HH24:mi:ss');
 s_sql := 'UPDATE test SET fill_date = '''||s_date ||'''';
 execute immediate s_sql;
 commit;
 end;
 

/
 
SQL> call test_p();
 

SQL> select to_char(fill_date,'yyyy-MM-dd HH24:mi:ss') from test;
 TO_CHAR(FILL_DATE,'
 -------------------
 2014-07-18 00:00:00    --可以看到确实时、分、秒已经丢失,怀疑是隐式转换导致
 

--调整下存储过程,,看打印出什么来。
 


SQL> CREATE OR REPLACE PROCEDURE test_p
 is
 s_sql varchar2(500);
 s_date date;
 begin
 s_date:= to_date('2014-07-18 17:24:32','yyyy-MM-dd HH24:mi:ss');
 s_sql := 'UPDATE test SET fill_date = '''||s_date ||'''';
 dbms_output.put_line(s_sql);
 execute immediate s_sql;
 commit;
 end;
 

/
 


SQL> set serveroutput on
 

SQL> call test_p();
 UPDATE test SET fill_date = '18-7月 -14'
 调用完成。
 
--确认发生了隐式转换,那就使用绑定变量了
 
SQL> CREATE OR REPLACE PROCEDURE test_p
 is
 s_sql varchar2(500);
 s_date date;
 begin
 s_date:= to_date('2014-07-18 17:24:32','yyyy-MM-dd HH24:mi:ss');
 s_sql := 'UPDATE test SET fill_date = :1';
 execute immediate s_sql using s_date;
 commit;
 end;
 

/
 
SQL> call test_p();
 调用完成。
 

SQL> select to_char(fill_date,'yyyy-MM-dd HH24:mi:ss') from test;
 TO_CHAR(FILL_DATE,'
 -------------------
 2014-07-18 17:24:32

Java中用JDBC调用Oracle存储过程和函数

Oracle存储过程本地编译方式

Oracle 存储过程及REF CURSOR的使用

Oracle存储过程中提示“权限不足”的解决办法

Oracle利用存储过程返回结果集开发报表

Oracle存储过程中临时表的使用技巧

本文永久更新链接地址:

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!