这个是我的存储过程, s_date := to_date(startTime,'yyyy-mm-dd hh24:mi:ss');
我用这样把字符串转成date,但是传进来的字符串是有时分秒的,
转换之后就只剩下年月日了,类似23-12月-17, 第一个if里面是可以正确执行的,后面的elsif 有日期比较的都报错了
报这个错,求大佬看看
s_date := to_date(startTime,'yyyy-mm-dd hh24:mi:ss'); 改为s_date := to_date(startTime,''yyyy-mm-dd hh24:mi:ss''); 2个单引号,时间比较的时候注意两边格式是否一致
to_char(to_date(date, data本身类型),想要的类型)
create or replace procedure copy_Data3(tablename in varchar2,startTime in varchar2,endTime in varchar2)
as
dynamic_insert_sql varchar2(500);
s_date date;
e_date date;
begin
if startTime is null and endTime is null then
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t';
elsif startTime is null and endTime is not null then
e_date := to_date(endTime,'yyyy-mm-dd hh24:mi:ss');
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t where t.TJSJ<='||e_date;
elsif startTime is not null and endTime is null then
s_date := to_date(startTime,'yyyy-mm-dd hh24:mi:ss');
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t where t.TJSJ>='||s_date;
elsif startTime is not null and endTime is not null then
s_date := to_date(startTime,'yyyy-mm-dd hh24:mi:ss');
e_date := to_date(endTime,'yyyy-mm-dd hh24:mi:ss');
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t where t.TJSJ>='||s_date||' and t.TJSJ<='||e_date;
end if;
execute immediate dynamic_insert_sql;
COMMIT;
end;
上面的图片太小,重新贴一下代码
报错是你本身传入的参数就有问题,本身存储过程没有问题。
你检查一下你的传入的参数。
问题出在这里。导致_to_date失败_
s_date varchar2(100);
e_date varchar2(100);
s_date := 'to_date('''||startTime||''',''yyyy-mm-dd hh24:mi:ss'')';
create or replace procedure copy_Data3(tablename in varchar2,startTime in varchar2,endTime in varchar2)
as
dynamic_insert_sql varchar2(500);
s_date VARCHAR2(100);
e_date VARCHAR2(100);
begin
if startTime is null and endTime is null then
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t';
elsif startTime is null and endTime is not null then
e_date := 'to_date('''||endTime||''',''yyyy-mm-dd hh24:mi:ss'')';
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t where t.TJSJ<='||e_date;
elsif startTime is not null and endTime is null then
s_date := 'to_date('''||startTime||''',''yyyy-mm-dd hh24:mi:ss'')';
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t where t.TJSJ>='||s_date;
elsif startTime is not null and endTime is not null then
s_date := 'to_date('''||startTime||''',''yyyy-mm-dd hh24:mi:ss'')';
e_date := 'to_date('''||endTime||''',''yyyy-mm-dd hh24:mi:ss'')';
dynamic_insert_sql := 'insert into '||tablename||' select t.*,sysdate,'||1||' from '||tablename||'@DBLINK_QYPT2 t where t.TJSJ>='||s_date||' and t.TJSJ<='||e_date;
end if;
execute immediate dynamic_insert_sql;
COMMIT;
end;