declare counted int := 0; strSql varchar2(1000) := ''; begin strSql := 'select count(1) from hi_indicators'; execute immediate strSql into counted; if (counted = 0) then dbms_output.put_line('the table is empty.'); else dbms_output.put_line('the table has ' || to_char(counted) || ' records'); end if; end;--结果 the table has 46 records
大概意思就是where条件是变化的罢,根据其它的列的值。 可以使用union 请参考:SELECT * FROM 表名 WHERE 列名1 = 特定值 AND 列名2 = 某个值 UNION SELECT * FROM 表名 WHERE 列名1 != 特定值 AND 列名2 = 另外某个值;
这个应该可以满足要求了 select * from aa表 where 1==1 and a=条件一 union all select * from aa表 where 1<>1 and b=条件二
SELECT * FROM TAB1 WHERE (FIELD1 = 'A' AND FIELD2 = 'B') OR (FIELD1 <> 'A' AND FIELD3 = 'C')
大家弄错我的意思,不好意思啊!我的意思是where后面加的是SQL语句比如,select* from 表名 where if(true){表名.name='张三'}else{ 表名.id=263547 } 这个意思。因为某些原因我不能写存储过程,不能在后台进行sql拼接。只能用SQL语句弄出来。
counted int := 0;
strSql varchar2(1000) := '';
begin
strSql := 'select count(1) from hi_indicators';
execute immediate strSql into counted;
if (counted = 0) then
dbms_output.put_line('the table is empty.');
else
dbms_output.put_line('the table has ' || to_char(counted) || ' records');
end if;
end;--结果
the table has 46 records
可以使用union
请参考:SELECT * FROM 表名
WHERE 列名1 = 特定值 AND 列名2 = 某个值
UNION
SELECT * FROM 表名
WHERE 列名1 != 特定值 AND 列名2 = 另外某个值;
select * from aa表
where 1==1 and a=条件一
union all
select * from aa表
where 1<>1 and b=条件二
WHERE (FIELD1 = 'A' AND FIELD2 = 'B')
OR (FIELD1 <> 'A' AND FIELD3 = 'C')
表名.id=263547
}
这个意思。因为某些原因我不能写存储过程,不能在后台进行sql拼接。只能用SQL语句弄出来。
FIELD1 = 'A' 和 FIELD1 <> 'A' 这2个条件只有1个成立
所以(FIELD1 = 'A' AND FIELD2 = 'B') 等价于 IF(TRUE){...}
(FIELD1 <> 'A' AND FIELD3 = 'C') 等价于 ELSE{...}
...;
elsif 1=2 then
..;
else
..;
end if;
WHEN COL > 1 THEN
'1'
ELSE
'4'
END
FROM TABLE应该可以实现,或者可以用存储过程