TO jiezhi(浪子) :select decode(col1,null,'null',col1) into v_col1 ...; decode()中的东西是什么意思?怎么还有'null'?
關于decode: http://gigabase.idi.ntnu.no/oradoc/server.901/a90125/functions33.htm#1017439 'null'只是一個字符串罷了,你可以換成別的。比如'data not found'什么的。 這么處理是因為如果select ... into 沒有select到任何數據的話會出錯,所以總是讓它select出值來,這樣就不出錯了。你好象是用ms sql的吧?
to jiezhi(浪子) : 我用的是pl/sql 问题是要select很多字段,字段不为空, sum每一个字段
SQL> set serveroutput on SQL> declare 2 t_col2 varchar2(20); 3 begin 4 select nvl(aaa,'111') into t_col2 from tn where 1=2; 5 exception when no_data_found then 6 t_col2 := 'is null'; 7 DBMS_OUTPUT.PUT_LINE('col2 is:'||t_col2); 8 end; 9 / col2 is:is nullPL/SQL 过程已成功完成。SQL>
declare v1 varchar2(10); v2 varchar2(10); ... begin select sum(col1),sum(col2),... into v1,v2,... from tn where ...; exception when other then v1:=...; v2:=...; end; /
decode()中的东西是什么意思?怎么还有'null'?
http://gigabase.idi.ntnu.no/oradoc/server.901/a90125/functions33.htm#1017439
'null'只是一個字符串罷了,你可以換成別的。比如'data not found'什么的。
這么處理是因為如果select ... into 沒有select到任何數據的話會出錯,所以總是讓它select出值來,這樣就不出錯了。你好象是用ms sql的吧?
我用的是pl/sql
问题是要select很多字段,字段不为空,
sum每一个字段
SQL> declare
2 t_col2 varchar2(20);
3 begin
4 select nvl(aaa,'111') into t_col2 from tn where 1=2;
5 exception when no_data_found then
6 t_col2 := 'is null';
7 DBMS_OUTPUT.PUT_LINE('col2 is:'||t_col2);
8 end;
9 /
col2 is:is nullPL/SQL 过程已成功完成。SQL>
v1 varchar2(10);
v2 varchar2(10);
...
begin
select sum(col1),sum(col2),... into v1,v2,... from tn where ...;
exception
when other then
v1:=...;
v2:=...;
end;
/