0
votes

Below code shows error near datatype Varchar and also near if statement - syntax error at or near "VARCHAR"

i have tried checking case sensitive and assigning value.

Declare
Userstate VARCHAR := 'Active' ;
begin
if u.disabled_at is null
then
Userstate = 'Inactive';
else
Userstate = 'Active';
end if
end

select o.id as Organisationid,o.name as OrganisationName, orl.user_id as "UserID", u.username as "UserName" ,
Userstate as "Active/Inactive" ,orl.role as "UserType"
from org_user_roles orl
Full outer join orgs o on
o.id = orl.org_id 
Full outer join users u on
orl.user_id  = u.id

i have to declare a variable and use it in if-else statement. Where u.disabled_at is a column from table users. Next i m trying to join my tables to get output in select statement and variable from if-else statement.

1
This needs more context. Where does u.disabled_at come from? Post a minimal reproducible example. - sticky bit
u - users table and disabled_at - column in the table. - Ankita Badal
You need a DO block for that - a_horse_with_no_name

1 Answers

1
votes

You don't need any plsql at all here, no variable declarations and no assignments. In your query, just use a case expression:

select
  o.id as "OrganisationID",
  o.name as "OrganisationName",
  u.id as "UserID",
  u.username as "UserName",
  (case when u.disabled_at is null
    then 'Inactive'
    else 'Active'
  end) as "Active/Inactive",
  orl.role as "UserType"
from
  org_user_roles orl
  full outer join orgs o on o.id = orl.org_id 
  full outer join users u on orl.user_id = u.id