【问题标题】:ORACLE If Then Else statement with counter and select queryORACLE If Then Else 语句与计数器和选择查询
【发布时间】:2014-05-07 05:55:30
【问题描述】:

我使用的是 Oracle 10.2.0.1

我有一个包含列(id、用户名、密码、计数)的表“AppUser”

我想做的是:

check username 
if (username exists )
{
check password
if (password exists)
{
set count = 0 and return 1
}
else (password not exists) 
{
check count < = 5
if true(count + 1 and return 0)
else return 2 (user locked)
}
}

我已经使序列“countid”最小值=1,最大值=5,递增1,循环为真。

现在我想制作一个包并从后面的代码中调用它。

到目前为止我有这个..但它给了我错误 ORA-00900: invalid SQL statement

IF count(select "LoginName" from "ApplicationUser" where exists (select "Count" from
"ApplicationUser" where Count<=5) and "LoginName" =:param1) >0
THEN select "Pass" from "ApplicationUser" where "Pass"=:param2;
ELSE return 0;
END IF;

【问题讨论】:

  • 您是否正在尝试构建 SQL 语句?还是 PL/SQL 块? IF 是 PL/SQL 构造,而不是 SQL 构造。您的表中实际上有一个名为“计数”的列吗?!您是否尝试将该值更新为 PL/SQL 块的一部分?
  • PL/SQL 是肯定的。是的,我在表中有一个列名 Count。我希望它是自动生成的,这就是为什么我还制作了一个引用它的序列。现在。当“用户名”存在而密码不存在并且计数位于第 5 位时,就会出现真正的问题。

标签: sql if-statement plsql oracle10g


【解决方案1】:

您尝试做的是相当复杂的语句,它很难制作,也很难维护和修改。尝试完全按照您在“我想做的是”部分中所写的方式分解您的问题。 PL/SQL 让您有可能编写出漂亮的分解代码。

begin
select username into v_user_name where ...;
if (username is not null) then
    select password into v_password from ...;
    if (v_password is not null) then
        update AppUser set count = 0 where ...;
        return 1;
    else
        select count into v_count from ...;
        if v_count<5 then
            update AppUser set count=count+1 where....;
            return 0;
        else
            return 2;
        end if;
    end if;
end if;
end;

【讨论】:

    【解决方案2】:
    update usertable
        set count = 0
    where id = @id
    
    declare @i int, @Getpassword nvarchar(50),@GetCount int
    set @i = 0 ;
    while(@i <= 5)
    begin
    
    
    select @Getcount = count from usertable where id  = @id
    
    
    if(@GetCount > 5 or @getCount = 5)
        begin
            -- block user here
            print 'user blocked'
            set @i = 6
        end
    else 
        begin
            print 'checking'
            select @Getpassword = password from usertable where id = @id
    
    
            if(@Getpassword = @password)
                begin
                    print 'validated'
                    set @i = 6
                end
            else
                begin
                    print 'not validated'
                    set @i = @i + 1;
                    print 'Current Count ' 
                    print @getcount
                    print 'updating count'
                    update usertable
                        set count = @getcount +1 
                    where id = @id
                end
            end
    end
    RETURN
    

    【讨论】:

    • 问题标记为 Oracle 和 PL/SQL。这似乎是 T-SQL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    相关资源
    最近更新 更多