[error]oracle execute immediate v_sql into v_count报错:sql命令未正确结束。

weixin_40140746 2019-11-07 10:45:00

declare
v_count int;--定义变量
v_col varchar2(20);--定义变量
v_sql varchar2(2000);--定义变量
v_last_col varchar2(20);--定义变量

cursor cur_col is select column_name from user_tab_cols where table_name='TCIP_ZX_P_CRED_CARD_INFO' order by column_id;--定义游标
begin
select column_name into v_last_col from user_tab_cols where table_name='TCIP_ZX_P_CRED_CARD_INFO' and column_id=(select max(column_id) from user_tab_cols where table_name='TCIP_ZX_P_CRED_CARD_INFO');--取出表中最后一个字段放入v_last_col
open cur_col;--打开游标
loop --执行循环
fetch cur_col into v_col;--取出游标内容到变量v_col
v_sql:='select count(*) from TEST where '||v_col||' is null';
[color=#FF0000]execute immediate v_sql into v_count;--执行动态sql
if v_count>0--如果查询有空值的字段不为空,则输出此字段名
then
dbms_output.put_line(v_col);
end if;
exit when v_col=v_last_col; --退出循环条件
end loop;--结束循环
close cur_col;--关闭游标
end;

【err】ORA-00933:SQL命令未正确结束
...全文
554 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 6 楼 weixin_40140746的回复:
加过分数了,老铁再帮我看看吧
看啥?现在还编译不过去吗?你回复的时候要引用我的回复,不然我看不到你的,只能打开才能看到,系统不会通知
weixin_40140746 2019-11-07
  • 打赏
  • 举报
回复
加过分数了,老铁再帮我看看吧
  • 打赏
  • 举报
回复
引用 4 楼 weixin_40140746 的回复:
改了之后又报错成ora-00933;sql命令未正确结束,在 execute immediate v_sql into v_count这一行;(分号已加)
严谨,分数呢,老铁!
weixin_40140746 2019-11-07
  • 打赏
  • 举报
回复
改了之后又报错成ora-00933;sql命令未正确结束,在 execute immediate v_sql into v_count这一行;(分号已加)
  • 打赏
  • 举报
回复
open cur_col;--打开游标 loop --执行循环 fetch cur_col into v_col;--取出游标内容到变量v_col v_sql:='select count(*) from TEST where '||v_col||' is null'; [color=#FF0000]execute immediate v_sql into v_count;--执行动态sql if v_count>0--如果查询有空值的字段不为空,则输出此字段名 then dbms_output.put_line(v_col); end if; exit when v_col=v_last_col; --退出循环条件 end loop;--结束循环 close cur_col;--关闭游标 --------------------------------------------------------------------------------- 你怎么能够将表插入到一个varchar2的变量里面呢,最起码也应该是一个表变量啊!直接改成这样就行了 for i in cur_col loop v_Sql := 'select counr(1) from test where '||i.column_name||' is null'; execute immediate v_sql into v_count if v_count>0--如果查询有空值的字段不为空,则输出此字段名 then dbms_output.put_line(v_col); end if; end loop;
weixin_40140746 2019-11-07
  • 打赏
  • 举报
回复
修改之后报错“对变量v_col的引用无效”statement ignored,报错位置还是在这一行。
  • 打赏
  • 举报
回复
v_sql:='select count(*) from TEST where '||v_col||' is null'; v_sql:='select count(*) from TEST where '||v_col.column_name||' is null'; 这个是一个嵌套表,要指定具体字段
  • 打赏
  • 举报
回复
引用 9 楼 weixin_40140746 的回复:
[quote=引用 8 楼 yoyohey 的回复:] [quote=引用 7 楼 yoyohey 的回复:] [quote=引用 6 楼 weixin_40140746的回复:]加过分数了,老铁再帮我看看吧
看啥?现在还编译不过去吗?你回复的时候要引用我的回复,不然我看不到你的,只能打开才能看到,系统不会通知[/quote]仔细看一下你的匿名块,你这个运行报错,这个test表是用来做啥的 select count(*) from TEST where 是不是应该改成 select count(*) from TCIP_ZX_P_CRED_CARD_INFO where; 还有你是想找这个里面哪些个字段值为空吧,既然表都是到了,直接写成union all不就行了吗? 比如:

select 'a' as col from table
where a is null
union all
select 'b' as col from table
where b is null
[/quote] test改成表名TCIP_ZX_P_CRED_CARD_INFO,这个我已经改过了的。一直显示sql未正常结束。 我就是想查询一下一张表里有哪些字段的值全为空,因为字段名比较多,想只通过表名来进行查询,所以参考网上教程写了这个declare。[/quote]能远程吗?私信你了,远程看看
weixin_40140746 2019-11-07
  • 打赏
  • 举报
回复
引用 8 楼 yoyohey 的回复:
[quote=引用 7 楼 yoyohey 的回复:] [quote=引用 6 楼 weixin_40140746的回复:]加过分数了,老铁再帮我看看吧
看啥?现在还编译不过去吗?你回复的时候要引用我的回复,不然我看不到你的,只能打开才能看到,系统不会通知[/quote]仔细看一下你的匿名块,你这个运行报错,这个test表是用来做啥的 select count(*) from TEST where 是不是应该改成 select count(*) from TCIP_ZX_P_CRED_CARD_INFO where; 还有你是想找这个里面哪些个字段值为空吧,既然表都是到了,直接写成union all不就行了吗? 比如:

select 'a' as col from table
where a is null
union all
select 'b' as col from table
where b is null
[/quote] test改成表名TCIP_ZX_P_CRED_CARD_INFO,这个我已经改过了的。一直显示sql未正常结束。 我就是想查询一下一张表里有哪些字段的值全为空,因为字段名比较多,想只通过表名来进行查询,所以参考网上教程写了这个declare。
  • 打赏
  • 举报
回复
引用 7 楼 yoyohey 的回复:
[quote=引用 6 楼 weixin_40140746的回复:]加过分数了,老铁再帮我看看吧
看啥?现在还编译不过去吗?你回复的时候要引用我的回复,不然我看不到你的,只能打开才能看到,系统不会通知[/quote]仔细看一下你的匿名块,你这个运行报错,这个test表是用来做啥的 select count(*) from TEST where 是不是应该改成 select count(*) from TCIP_ZX_P_CRED_CARD_INFO where; 还有你是想找这个里面哪些个字段值为空吧,既然表都是到了,直接写成union all不就行了吗? 比如:

select 'a' as col from table
where a is null
union all
select 'b' as col from table
where b is null

3,499

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧