ORACLE If Then Else语句带计数器和选择查询

时间:2014-05-07 05:55:31

标签: sql if-statement plsql oracle10g

我正在使用Oracle 10.2.0.1

我有一个表“AppUser”,其中包含列(id,username,pass,count)

我想做的是:

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,inc为1,循环为真。

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

到目前为止我有这个..但它给了我错误ORA-00900:无效的SQL语句

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;

2 个答案:

答案 0 :(得分:1)

你尝试做的是相当复杂的陈述,这很难制作,很难维护和修改。尝试完全按照你在&#34;中所写的那样分解你的问题。我想做的是&#34;部分。 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;

答案 1 :(得分:0)

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