Multiple or condition in sql store procedure - sql

I am trying to check multiple OR conditions inside if block but its my code inside if block still gets executed even if GUIDStatement is 'ContactDetailsGUID' or 'FILECONTENTSGUID' or 'AccountGUID'.
I have a cursor which keeps assigning single value to GuidStatement variable. So at any given point of time my #GUIDStatements variable will have any single name
My If condition should not get executed when my GUIDStatement has any of the above value
My if condition is as follows. Please help.
IF ( (#GUIDStatements <> 'ContactDetailsGUID') OR (#GUIDStatements <> 'FILECONTENTSGUID') OR (#GUIDStatements <> 'AccountGUID'))

Run the code below. The first block of code, your code, returns false regardless of the value of #GUIDStatements. The second block, suggestion, returns false if #GUIDStatements is in one of the three values and true otherwise. I think, as I understand your question, thatis what you're looking for. If not please comment and I'll try to clarify.
DECLARE #GUIDStatements SYSNAME = N'not';
--
-- your code
-------------------------------------------------
IF (
(
#GUIDStatements <> 'ContactDetailsGUID'
)
OR
(
#GUIDStatements <> 'FILECONTENTSGUID'
)
OR
(
#GUIDStatements <> 'AccountGUID'
)
)
SELECT N'false'
ELSE
SELECT N'true';
--
-- suggestion
-------------------------------------------------
IF #GUIDStatements IN ( 'ContactDetailsGUID', 'FILECONTENTSGUID', 'AccountGUID' )
SELECT N'false'
ELSE
SELECT N'true';

How about:
IF #GUIDStatements NOT IN ('ContactDetailsGUID', 'FILECONTENTSGUID', 'AccountGUID')
Easier to maintain too.

Related

How to put IF condition in where section of select command in SQL?

I declare some filters on a form and pass them into my SP in SQL for running a select command and this is how I did that, but it didn't work:
DECLARE #Confirm_Filter AS BIT=NULL,
#ReciveDate_Filter AS NCHAR(10) = NULL
....
select * from .......
where
(#Confirm_Filter IS NOT NULL AND InterviewConfirm = #Confirm_Filter)
AND
(#ReciveDate_Filter IS NOT NULL AND ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)
and in the where section I want to do:
where
if #Confirm_Filter IS NOT NULL Then (Select * from ... where InterviewConfirm = #Confirm_Filter)
AND
if #ReciveDate_Filter IS NOT NULL Then (select * from .... where ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)
and I know above form is totally mistake but how could I do it?
Your problem is called dynamic search conditions. It is solved either by using dynamic SQL, or the following pattern (which is similar to your first version, but with a few changes):
select * from .......
where
(#Confirm_Filter IS NULL OR InterviewConfirm = #Confirm_Filter)
AND
(#ReciveDate_Filter IS NULL OR ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)

GETTING ERROR-- ORA-00936:MISSING EXPRESSION for below query please help on this

SELECT CASE (SELECT Count(1)
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(
SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988')
)
WHEN 0 THEN
(
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27' )
WHEN 1 THEN
(
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
ELSE NULL
END
FROM dual;
You need to recreate your query and make sure to follow the flow of the clauses properly, please check the next two links to get a better understanding :
[ORA-00936: missing expression tips]
How do I address this ORA-00936 error?
Answer: The Oracle oerr utility notes this about the ORA-00936 error:
ORA-00936 missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
The ORA-00936 happens most frequently:
1 - When you forget list of the column names in your SELECT statement.
2. When you omit the FROM clause of the SQL statement.
ora-00936-missing-expression
I hope this can help you.
You cannot use a simple select query like this. You have to use a PL/SQL block like below -
DECLARE NUM_CNT NUMBER := 0;
BEGIN
SELECT Count(1)
INTO NUM_CNT
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988');
IF NUM_CNT = 0 THEN
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27';
ELSIF NUM_CNT = 1 THEN
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
END IF;
END;

Reusing calculated column in update in same code-block

I am trying to find the (best) way to update column of table based on calculated another column.
First of all, I am using function to calculate value for 'BAS_CBR_EB_RWA' column and then I am using 'BAS_CBR_EB_RWA' value as an input for 'BAS_CBR_EB_TOTAL_CAPITAL' column calculation, as shown in below-mentioned code.
Could you please share how to reuse it to do next calculation. replacing 'BAS_CBR_EB_RWA' with right-hand calculation isn't desirable because we have too many calculation of this type and it'll confuse other users.
Thanks in advance for help.
IF INSTTABLE = 16 THEN
UPDATE LAO_DATA
SET BAS_CBR_EB_RWA = BAS2_RWA_CALC(BAS_CAPITAL_CALC_CD,
CBR_CUR_BOOK_BAL,
BAS_CAP_FACTOR_K,
V_BASEL_MIN,
V_BAS_RWA_RATE),
BAS_CBR_EB_TOTAL_CAPITAL = ROUND(BAS2_MGRL_CAPITAL(V_DATE,
BAS_CBR_EB_RWA,
0),
2),
WHERE (AS_OF_DATE = V_DATE);
--COMMIT;
END IF;
In Oracle, you can update a subquery. I'm not 100% sure if it works for UDFs, but you can try:
UPDATE (SELECT LD.*,
BAS2_RWA_CALC(BAS_CAPITAL_CALC_CD, CBR_CUR_BOOK_BAL, BAS_CAP_FACTOR_K, V_BASEL_MIN, V_BAS_RWA_RATE) as new_BAS_CBR_EB_RWA
FROM LAO_DATA LD
)
SET BAS_CBR_EB_RWA = new_BAS_CBR_EB_RWA,
BAS_CBR_EB_TOTAL_CAPITAL = ROUND(BAS2_MGRL_CAPITAL(V_DATE, nw_BAS_CBR_EB_RWA, 0), 2),
WHERE AS_OF_DATE = V_DATE;
A MERGE statement can be used. You may also replace ROWID with the primary key or a Unique key of the table.
Put all your first level of calculations with function calls inside the USING() block and the second level of calculation for RHS in the SET expression
MERGE INTO lao_data t
USING (
SELECT ROWID AS rid,bas2_rwa_calc(bas_capital_calc_cd,
cbr_cur_book_bal,
bas_cap_factor_k,
v_basel_min,v_bas_rwa_rate
) AS new_BAS_CBR_EB_RWA
FROM lao_data
WHERE as_of_date = V_DATE
)
s ON ( s.rid = t.rowid )
WHEN MATCHED THEN UPDATE
SET t.bas_cbr_eb_rwa = s.new_BAS_CBR_EB_RWA
t.bas_cbr_eb_total_capital
= round(bas2_mgrl_capital(v_date,s.nw_BAS_CBR_EB_RWA,0), 2) );

SQL Server : getting an error "Msg147, level15" Why do I get this error and how to fix it

I have the following code
IF EXISTS(SELECT #FunderID
FROM dbo.FunderCharityTbl
WHERE #ContributionAmount > ( ( sum(TotalContributions) / 100 ) *10 ))
BEGIN
RAISERROR ('Sorry contribution is refused limit is breached', 16,1)
RETRUN 99
END
And I am getting the following error
Msg 147, Level 15, State 1, Procedure InsertContribution, Line 33
An aggregate may not appear in the WHERE clause unless it is in a
subquery contained in a HAVING clause or a select list, and the column
being aggregated is an outer reference.
What I am trying to do is check if the #contributionAmount (entered amount) is greater than 10% of all the previous contributions made by the person with the entered funderID and if it is send an error message
I am relatively new to SQL and am wondering why you can't write the If Exists statement the way I did write it and what do I need to do to fix this error and have my procedure perform in the same manner as I wanted.
You can't use a Aggregate function in WHERE clause, but you can use it in HAVING clause
IF EXISTS( SELECT 1 --#FunderID
FROM dbo.FunderCharityTbl
HAVING #ContributionAmount > ((sum(TotalContributions)/100)*10)
)
You have to use GROUP BY and HAVING something like:
IF EXISTS(
SELECT #FunderID
FROM dbo.FunderCharityTbl
GROUP BY #FunderID
HAVING #ContributionAmount > ((sum(TotalContributions)/100)*10)
)
I think the message is pretty clear: You cannot use an aggregation function in a where clause. The appropriate clause is having.
The intent of your query is unclear. Why are you returning a variable value? In fact, with EXISTS, you can return anything. I prefer SELECT 1.
I would guess that you are trying to determine if #FunderID has made more than a certain level of contributions. You would do this as:
IF ( (SELECT (sum(TotalContributions)/100)*10
FROM dbo.FunderCharityTbl
WHERE FunderId = #FunderId
) > #ContributionAmount
)
BEGIN
. . .
END;
Note: I also encourage you to use BEGIN/END blocks whenever you use IF.
An alternative interpretation is that you want to determine if any funder has donated more than the specified amount:
IF (EXISTS (SELECT (sum(TotalContributions)/100)*10
FROM dbo.FunderCharityTbl
GROUP BY FunderId
HAVING sum(TotalContributions)/100)*10 > #ContributionAmount
)
)
BEGIN
. . .
END;

Variable is getting NULL after SUM function in SQL Server

I have this code that is part of a stored procedure:
SET #_Value = 0
SET #_Cont = 1;
IF(#_FlagControl = 1)
BEGIN
SELECT #_Value = SUM(Value)
FROM Person P
INNER JOIN Order O on O.CodPerson = P.CodOrder
WHERE P.CodPerson = #_CodP
AND P.CodImp <> 3
AND P.FlagSituation = 1
AND #_CodMainPerson = P.CodPerson
IF(#_Value IS NULL)
PRINT 'NULL'
ELSE
PRINT #_Value
END
If I run just this SELECT inside the "IF", it returns '0.90'. But when I run this entire query inside a procedure, it is printing NULL.
I don't have idea what is going on.
You assign #_Value
You check #_Value
You print #_ValorImposto
So if #_Value is not null, you don't print it...
Edit, it is not this
Therefore, the SELECT has no rows, which when SUMMed gives one row, making #_Value NULL
So test and fix the SELECT in the stored procedue: print #_CodP and #_CodMainPerson. SELECT without the SUM.
Not in another query with hardcoded values: this proves nothing
Note that SUM without GROUP BY always returns exactly one row.
So #_Value will always either either a non-null value or NULL
See Does COUNT(*) always return a result? for more