oracle procedure with case It depends from parameter - oracle

Hi I want to make a procedure like following:
CREATE OR REPLACE PROCEDURE SOL.INSERT_LD_NEXTPROCESS (vgroupid NUMBER)
IS
VPERIODID VARCHAR2 (10);
vPROCSESSID NUMBER;
CURSOR c
IS
SELECT COMPANYID,
GROUPID,
PERIODID,
FN_PPROCESSCURRENT
FROM LIQUIDATIONSDETAILS
WHERE PROCESSID = FN_PPROCESSPREVIOUS
AND (UNCOLLECTED > 0 OR INVOICE = 0)
I want to add an extra filter it depends from parameter:
CASE WHEN vgroupid > -1 then
AND GROUPID = vgroupid
ELSE
NULL
END
...
so the where cause like
WHERE PROCESSID = FN_PPROCESSPREVIOUS
AND (UNCOLLECTED > 0 OR INVOICE = 0) AND GROUPID = vgroupid
when vgroupid = -1 then I need all records an when vgroupid > -1 then I need only the records in vgroupid
any idea?

CURSOR c
IS
SELECT COMPANYID,
GROUPID,
PERIODID,
FN_PPROCESSCURRENT
FROM LIQUIDATIONSDETAILS
WHERE PROCESSID = FN_PPROCESSPREVIOUS
AND (UNCOLLECTED > 0 OR INVOICE = 0)
AND (((GROUPID = vgroupid) AND (vgroupid > -1)) OR (vgroupid = -1))
for example:
if vgroupid = -1, then last condition will be (((GROUPID = -1) AND (-1 > -1)) OR (-1 = -1)) or ((forever_false AND forever_false) OR (forever_true)) or (-1 = -1) - all records
instead, if vgroupid = 123 last condition will be (((GROUPID = 123) AND (123 > -1)) OR (123 = -1)) or (((GROUPID = 123) and forever_true) OR (forever_false)) or (GROUPID = 123) - only 123 GROUPID

Related

Problem using multiple CASE in WHERE clause

I'm trying to create an SQL that added two more conditions when the specific column(value) is matched.
Example:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOLD_COLUMN
END = 1
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOLD_COLUMN_1
END = 1
It is working when AGE = 18 it will return value that ADULTHOOLD_COLUMN = 1 and ADULTHOOLD_COLUMN_1 = 1
but when age is not equal to 18 it will not return any rows.
I was expecting that all of the data(with 1 or 0) will return.
ADULTHOOLD_COLUMN(Value in db is 1 or 0)
ADULTHOOLD_COLUMN_1(Value in db is 1 or 0)
Sounds a bit strange, but ok, looks like you need else:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOD_COLUMN
ELSE 1
END = 1
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOD_COLUMN_1
ELSE 1
END = 1
Or maybe just to make it more readable:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND (AGE is null OR AGE!='18' OR AGE = '18' AND ADULTHOOD_COLUMN = 1)
AND (AGE is null OR AGE!='18' OR AGE = '18' AND ADULTHOOD_COLUMN_1 = 1)
PS. Are you sure that AGE is not number? If that's a number datatype, you need to replace '18' to 18

define multiple variables in select query in oracle

Getting error.. unable to define 2 parameters in one query
define price = 99.969000
define isin = '9128283C2';
WITH A AS
(select BUY_SELL,CASE WHEN BUY_SELL = 0 THEN (&price - 0.0025) ELSE (&price + 0.0025) END BS,PRODUCT_ISIN,QUANTITY
from table1 t where t.t_dt = '01-nov-17'
AND BUY_SELL = 0
AND quantity = 25 and product_isin = &isin)
SELECT * FROM A where row_num = 1;

Oracle case statement not working

I want to use case statement for a column like below
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE NVL (amount, 0) = 0 THEN checkVal = 0
ELSE
checkVal = 1
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
But I am getting error as
ORA-00923: FROM keyword not found where expected
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE WHEN NVL(amount, 0) = 0 THEN 0 ELSE 1 END AS checkVal
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no
you're missing an END
SELECT sr_no, TYPE, stage, party_name, amount, remarks, exp_type, exp_id,
voucher_no, cheque_no, cheque_dt, chq_favr_name, attachment,
CASE WHEN NVL (amount, 0) = 0 THEN checkVal = 0
ELSE
checkVal = 1
END
FROM xxcus.xxacl_pn_expense_info
WHERE mkey = '354'
AND ((NVL (amount, 0) <> 0) OR (party_name IS NOT NULL))
ORDER BY sr_no

CASE not working in oracle

For one of my column in query I want a CASE statement.
The scenario is
If my column name ltt.f_complete task_completed value is 0 then the value shd be NO and if 1 then YES.
I tried like below but getting as
ORA-00905: missing keyword
here is query
SELECT flv.property_name project_name, flv.building building_name,
flv.flat_no unit_no,
ldet.customer_fname
|| ' '
|| ldet.customer_mname
|| ' '
|| ldet.customer_lname customer_name,
la.booking_date holding_date, ltt.start_date task_date,
tid.task_desc task_type, fol.next_follow_up_date,
ltt.remarks task_comment, ltt.f_complete task_completed,
CASE TASK_VALUE WHEN '0' THEN 'NO'
WHEN '1' THEN 'YES',
act.act_desc activity_description, flv.follow_type followup_type,
fol.remarks followup_comment,
fol.next_follow_up_date next_followup_date,
actt.act_desc next_todo_activity
FROM xxcus.xxacl_pn_leases_all la,
(SELECT *
FROM xxcus.xxacl_pn_lease_det
WHERE sr_no = 1) ldet,
xxcus.xxacl_pn_lease_task_trl ltt,
xxcus.xxacl_pn_customer_followup fol,
xxacl_pn_flat_det_v flv,
xxcus.xxacl_pn_hold_task_v tid,
xxcus.xxacl_pn_hold_act_v act,
xxcus.xxacl_pn_followup_type_v flv,
xxcus.xxacl_pn_hold_act_v actt
WHERE la.booking_no = ltt.draft_form_no(+)
AND ltt.draft_form_no = fol.booking_no(+)
AND ltt.task_id = fol.task_id(+)
AND ltt.task_sr_no = fol.task_sr_no(+)
AND la.delete_flag = 'N'
AND la.booking_no = ldet.booking_no
AND fol.followup_date = TO_DATE (SYSDATE - 1)
AND la.flat_id = flv.flat_id
AND ltt.task_id = tid.task_id
AND fol.activity_id = act.act_id
AND fol.followup_type_id = flv.follow_type_id
AND fol.next_activity_id = actt.act_id
kindly suggest what is wrong
UPDATE
SELECT flv.property_name project_name, flv.building building_name,
flv.flat_no unit_no,
ldet.customer_fname
|| ' '
|| ldet.customer_mname
|| ' '
|| ldet.customer_lname customer_name,
la.booking_date holding_date, ltt.start_date task_date,
tid.task_desc task_type, fol.next_follow_up_date,
ltt.remarks task_comment, ltt.f_complete task_completed,
act.act_desc activity_description, flv.follow_type followup_type,
fol.remarks followup_comment,
fol.next_follow_up_date next_followup_date,
actt.act_desc next_todo_activity
FROM xxcus.xxacl_pn_leases_all la,
(SELECT *
FROM xxcus.xxacl_pn_lease_det
WHERE sr_no = 1) ldet,
xxcus.xxacl_pn_lease_task_trl ltt,
xxcus.xxacl_pn_customer_followup fol,
xxacl_pn_flat_det_v flv,
xxcus.xxacl_pn_hold_task_v tid,
xxcus.xxacl_pn_hold_act_v act,
xxcus.xxacl_pn_followup_type_v flv,
xxcus.xxacl_pn_hold_act_v actt
WHERE la.booking_no = ltt.draft_form_no(+)
AND ltt.draft_form_no = fol.booking_no(+)
AND ltt.task_id = fol.task_id(+)
AND ltt.task_sr_no = fol.task_sr_no(+)
AND la.delete_flag = 'N'
AND la.booking_no = ldet.booking_no
AND fol.followup_date = TO_DATE (SYSDATE - 1)
AND la.flat_id = flv.flat_id
AND ltt.task_id = tid.task_id
AND fol.activity_id = act.act_id
AND fol.followup_type_id = flv.follow_type_id
AND fol.next_activity_id = actt.act_id
The keyword you are missing is END. It should read like this:
CASE TASK_VALUE WHEN '0' THEN 'NO'
WHEN '1' THEN 'YES' END

How to write lambda (linq) expression for sql?

I have an Sql statement for which I need go generate corresponding Lambda Expression (Linq).
Here is the SQL
Declare #CurrentUserId as int
Declare #CurrentDate as datetime
Set #CurrentDate = GetDate()
Set #CurrentUserId = 1
Select C.conferenceId,C.specialtyId,C.name,C.city,S.abbr as statebbbr,CTRY.name as countryname,C.startdate,C.enddate
from Conferences C
Inner join (
Select distinct SpecialtyId
from UserContent
Where UserId = #CurrentUserId and DeletedFlag = 0
) DT on C.SpecialtyId = DT.SpecialtyId
Left outer join State S on C.StateId = S.StateId
Inner join Country CTRY on C.CountryId = CTRY.CountryId
Where C.DisplayStartDate <= #CurrentDate
and C.DisplayEndDate >= #CurrentDate
and C.DeletedFlag = 0
and C.Publish = 1
Order by C.startdate ASC
What wolud be the lambda(linq) expression for this?
Assume data context is in variable context
from c in context.conferences
join ctry in context.country on c.CountryId equals ctry.CountryId
join s1 in context.State on c.StateId equals s.StateId into s2
from s in s2.DefaultIfEmpty()
where
c.DisplayStartDate <= System.DateTime.Now
&& c.DisplayEndDate >= System.DateTime.Now
&& c.DeletedFlag == 0 // or false if represented as a bool
&& c.Publish == 1 // or true if represented as a bool
&& context.UserContent.Any(
x => x.SpecialityId == c.specialityId
&& x.UserId == currentUserId
&& x.DeletedFlag == 0
// or if represented as a bool "&& !x.DeletedFlag"
)
select new {
c.ConferenceId,
c.SpecialtyId,
c.name,
c.city,
stateabbr = s.abbr,
countryname = ctry.name,
c.startdate,
c.enddate
}

Resources