现在需要这样一条sql语句。
如果查到的数据有一条则执行一条查询语句。如果没查到数据,就执行另外一条语句。
比如:select id, name  from A where name=1;
如果有数据  就执行:  sql1
没数据 就执行sql2.
该怎么把这个用条件判断拼起来。

解决方案 »

  1.   

    这个只能通过PL/SQL的语法来实现啊,正常的SQL语句不行~~~你想写这样的语句达到什么业务效果啊,这些通常可以通过程序或者存储过程都可以控制的,尽量在程序上控制比较好吧~
      

  2.   

    大概是这样: select A from tableA where xxx;
    如果查出结果了,就用这个结果
    如果没查出数据
    就用不要where条件的那个结果: 就是 select A from tableA。
      

  3.   

    写俩sql.
    sql1+and exists(select 1 from A where name=1);
    sql2+and not exists(select 1 from A where name=1);
      

  4.   

    select a from tablea where *** or not exists(select a from tablea where ***);
      

  5.   

    当然用plsql逻辑是最清晰的,就是写的过程麻烦些
      

  6.   

    这个可以, 不过还有一个问题。  where条件中的那个值是传参进来的。  这里面要用到2次怎么办呢
    select a from tablea where a=? or not exists(select a from tablea where a=?)
      

  7.   

    不行呀。 
    是这一样的 你只给框架提供一个sql语句。 参数是他设置的
    像只这样
    DAO.executeSQL(sql)
    DAO.setParam(xxx);// 框架只传一个参数,但sql语句里面有两个问号呀
      

  8.   


    ---这样吧
    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;
      

  9.   

    我觉得可以判断传进来的值是否为空如果不为空的话 就执行有where语句的那条 如果为空的话就执行没有where语句的那条
      

  10.   


    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;
      

  11.   

    不行哦,  我能做的工作就是往配置文件里写一sql。其他的都是框架做的。
    框架就传一个值。哎
      

  12.   

    关键框架就传一个参数。 我sql里面要用同一个参数两次怎么办。
      

  13.   

    给你个最简单的方法,根部不用拼sql,
    你可以做一个条件选择语句,比如;if else
    然后在if中判断是否为空,若为空将sql1赋值给sql
    else中将sql2赋值给sql
    最后执行的时候就执行sql就行了
      

  14.   

    select * from  (DECODE((select (1) from table1 )>0,select * from table1 ,select * from table2)) tmp