现在需要这样一条sql语句。
如果查到的数据有一条则执行一条查询语句。如果没查到数据,就执行另外一条语句。
比如:select id, name from A where name=1;
如果有数据 就执行: sql1
没数据 就执行sql2.
该怎么把这个用条件判断拼起来。
如果查到的数据有一条则执行一条查询语句。如果没查到数据,就执行另外一条语句。
比如:select id, name from A where name=1;
如果有数据 就执行: sql1
没数据 就执行sql2.
该怎么把这个用条件判断拼起来。
如果查出结果了,就用这个结果
如果没查出数据
就用不要where条件的那个结果: 就是 select A from tableA。
sql1+and exists(select 1 from A where name=1);
sql2+and not exists(select 1 from A where name=1);
select a from tablea where a=? or not exists(select a from tablea where a=?)
是这一样的 你只给框架提供一个sql语句。 参数是他设置的
像只这样
DAO.executeSQL(sql)
DAO.setParam(xxx);// 框架只传一个参数,但sql语句里面有两个问号呀
---这样吧
create or replace procedure p1(v_name xs.xm%type)
as
v_zxf number;
v_count number;
type cursor1 is ref cursor;
cur1 cursor1;
type cursor2 is ref cursor;
cur2 cursor2;
v_xm varchar2(20);
v_xb char(2);
begin
select count(*) into v_count from xs where xm=v_name;
if nvl(v_count,0)<>0 then
open cur1 for select xm from xs;
loop
fetch cur1 into v_xm;
exit when cur1% notfound;
dbms_output.put_line(v_xm);
end loop;
close cur1;
else
open cur2 for select xb from xs;
loop
fetch cur2 into v_xb;
exit when cur2% notfound;
dbms_output.put_line(v_xb);
end loop;
close cur2;
end if;
end;
declare
counter number;
str1 varchar2(200);
str2 varchar2(200);
begin
str1 := 'select * from dept';
str1 := 'select * from tb';
select count(1) into counter from dept where id = 1;
if counter > 0 then
execute immediate str1;
else
execute immediate str2;
end if;
end;
框架就传一个值。哎
你可以做一个条件选择语句,比如;if else
然后在if中判断是否为空,若为空将sql1赋值给sql
else中将sql2赋值给sql
最后执行的时候就执行sql就行了