我在程序包中有一个程序,我想实现一个逻辑,如果临时表有0行,它不会将临时表插入主表,然后转到程序包的下一个程序。
IF (not exists(select 1 from temp)) THEN
RETURN;
ELSE
EXECUTE IMMEDIATE 'TRUNCATE TABLE main';
INSERT --+APPEND
INTO main
Select * from temp;
EXECUTE IMMEDIATE 'TRUNCATE TABLE temp';
END IF;
使用此解决方案,程序包编译时出错。
有人能给我一些提示吗?
答案 0 :(得分:1)
你可以使用循环,没有任何变量,只需要第一次迭代,就像这样
FOR a in (select 1 from temp where rownum = 1) LOOP
EXECUTE IMMEDIATE 'TRUNCATE TABLE main';
INSERT --+APPEND
INTO main
Select * from temp;
EXECUTE IMMEDIATE 'TRUNCATE TABLE temp';
END LOOP;
答案 1 :(得分:1)
只计算一行,然后测试结果是0还是1:
$ rails c
/Users/my_username/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0/lib/action_controller/test_case.rb:49:in `initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)
from /Users/my_username/.rvm/gems/ruby-2.3.0/bundler/gems/draper-57a514133bc2/lib/draper/view_context/build_strategy.rb:41:in `new'
from /Users/my_username/.rvm/gems/ruby-2.3.0/bundler/gems/draper-57a514133bc2/lib/draper/view_context/build_strategy.rb:41:in `block in controller'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/andand-1.3.3/lib/andand.rb:60:in `me'
from /Users/my_username/.rvm/gems/ruby-2.3.0/bundler/gems/draper-57a514133bc2/lib/draper/view_context/build_strategy.rb:40:in `controller'
from /Users/my_username/.rvm/gems/ruby-2.3.0/bundler/gems/draper-57a514133bc2/lib/draper/view_context/build_strategy.rb:30:in `call'
from /Users/my_username/.rvm/gems/ruby-2.3.0/bundler/gems/draper-57a514133bc2/lib/draper/view_context.rb:49:in `build'
from /Users/my_username/.rvm/gems/ruby-2.3.0/bundler/gems/draper-57a514133bc2/lib/draper/railtie.rb:63:in `block in <class:Railtie>'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/railtie.rb:226:in `block in run_console_blocks'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/railtie.rb:247:in `each'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/railtie.rb:247:in `each_registered_block'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/railtie.rb:226:in `run_console_blocks'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/application.rb:463:in `block in run_console_blocks'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/engine/railties.rb:13:in `each'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/engine/railties.rb:13:in `each'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/application.rb:463:in `run_console_blocks'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/engine.rb:442:in `load_console'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/console.rb:34:in `initialize'
from /Users/my_username/.rvm/gems/ruby-2.3.0/gems/railties-
--clipped 441 characters to enter stack overflow question edit
答案 2 :(得分:0)
最简单的方法是使用变量进行检查:
--- suggested edit: add condition to select 1 row at most and avoid
-- counting big table.
select count(1) into v_count from temp where rownum <=1;
IF (v_count=0) THEN
RETURN;
ELSE
EXECUTE IMMEDIATE 'TRUNCATE TABLE main';
INSERT --+APPEND
INTO main
Select * from temp;
EXECUTE IMMEDIATE 'TRUNCATE TABLE temp';
END IF;
答案 3 :(得分:0)
这里的一些答案使用了SELECT INTO
方法,但我发现这些方法有点棘手。
如果例如SELECT ColumnA INTO vcColumnA FROM Temp
没有任何记录,则最终会出现错误ORA-01403: no data found
。
如果你有一个更大的数据库,那很难找到。
要遍历表并使用值执行某些操作,我认为游标和记录更安全。
例如:
DECLARE
CURSOR cTemp IS
SELECT ColumnA, ColumnB
FROM Temp;
rTemp cTemp%ROWTYPE;
BEGIN
OPEN cTemp;
LOOP
FETCH cTemp INTO rTemp;
-- Exit when we read all lines in the Temp table.
EXIT WHEN cTemp%NOTFOUND;
--Do something with every row.
--For example, print ColumnB.
DBMS_OUTPUT.PUT_LINE(rTemp.ColumnB);
END LOOP;
CLOSE cTemp;
END;
/