oracle sql中如何动态处理值带有0和倍数?

m1m5dgzv  于 2023-01-04  发布在  Oracle
关注(0)|答案(1)|浏览(126)

以下5001(参数s_id)是动态调用的。这是不起作用的,不返回任何行从学生表。什么是这个问题的根本原因?#

select * from student where 
   ( ( 1 = CASE WHEN to_char('5001') = to_char(0) THEN
                            1
                        ELSE
                            0
                    END )
              OR student.roll_id IN ( 5001 ) );
3df52oht

3df52oht1#

假设表中至少有一行-如果没有ROLL_ID = 5001(数字)的行,并且参数s_id的值从不为0(数字零),则where子句将不选择行。如果存在ROLL_ID = 5001的行,则将选择该行,如果参数s_id为0(数字零),则将选择所有行...
只是稍微构建了一下SQL(它和以前一样)
测试1:存在ROLL_ID = 5001的行

WITH
    students AS
        (   Select 1 "ROLL_ID", 'Not 5001' "SOME_COLUMN" From dual  Union All   
            Select 5001 "ROLL_ID", 'Here I am' "SOME_COLUMN" From dual      )

Select  * 
From    students 
Where   ( ( CASE WHEN to_char(5001) = to_char(0) THEN 1
            ELSE 0 
            END = 1 )
          OR
            students.ROLL_ID IN ( 5001 ) 
        );
--  
--  R e s u l t :
--     ROLL_ID SOME_COLUMN
--  ---------- -----------
--        5001 Here I am

测试2:相同代码-使用新样本数据,其中不存在ROLL_ID = 5001的行

WITH
    students AS
        (   Select 1 "ROLL_ID", 'Not 5001' "SOME_COLUMN" From dual  Union All   
            Select 5002 "ROLL_ID", 'Here I was' "SOME_COLUMN" From dual     )¸
-- same sql returns no rows

测试3:样本数据仍然没有ROLL_ID = 5001但param s_id = 0的行

Select  * 
From    students 
Where   ( ( CASE WHEN to_char(0) = to_char(0) THEN 1
            ELSE 0 
            END = 1 )
          OR
            students.ROLL_ID IN ( 5001 ) 
        );
--  
--  R e s u l t :
--     ROLL_ID SOME_COLUMN
--  ---------- -----------
--           1 Not 5001    
--        5002 Here I was

测试3将选择所有行-始终

相关问题