SQL Update using Case results - sql

I need to update a value in file1 with the contents of a field in another file2 with a matching key, but only if a row is found in file2 that matches. Otherwise, update file1 field with a 'Q' literal.
This works, but seems redundant, and takes too long? Suggestions?
update ZXU
set XUATTN = case when (select count(*) from ZXK
where XKUSER = 'TOMTEST') > 0
then (select XKAUTH from ZXK
where XKUSER = 'TOMTEST')
else 'Q'
end
where XUUSER='TOMTEST'

You can use COALESCE():
update ZXU
set XUATTN = COALESCE( (select k.XKAUTH from ZXK k where k.XKUSER = ZHU.XUUSER), 'Q')
where XUUSER = 'TOMTEST';
I will note that this (and your code) will generate an error if the subquery returns more than one row.

Related

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 : Update value when the value in the database is null

I know this is already asked question and possible to be close.
But i really want a answer, I already searched through the internet, Read documentations, Blogs, and Question to SO.
This is my Query so Far,
declare #count numeric
select #count = (select count(1) from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq))
update E496_TitleReference
set PrintStatus = '{0}',Is_AESM=isnull(-1,Is_AESM)
from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq)
if ##rowcount <> #count
begin
rollback tran
Print "Error: There is an error on table E496_TitleReference."
return
end
go
For eg, In my table in Database i have column name Is_AESM, In Is_AESM column it have 4 values.
Is_AESM
NULL
NULL
-1
-2
Something like this.
Now when i run my script, it has no problem when i run it,
Is_AESM=isnull(-1,Is_AESM)
In this query it will detect if Is_AESM is null, it will update Is_AESM = -1 if not it will retain the value.
Now my problem is, if my query detect Is_AESM has a null value, it will update all the value to -1.
Is_AESM
-1
-1
-1
-1
The result is something like that. Now i want is update only the null value not all the value in column Is_AESM.
I think this query is wrong Is_AESM=isnull(-1,Is_AESM).
Any ideas will be a big help.
You may try with coalsece() function
update E496_TitleReference
set PrintStatus = '{0}',Is_AESM=coalsece(Is_AESM,-1)
from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq)
you need to replace order of parameters.
Is_AESM=isnull(Is_AESM, -1)
You can use COALSECE function. It returns the first non-null entry from the given list. So:
Is_AESM= COALSECE(IS_AESM,-1)
This will return IS_AESM value if it is not null (since it is the first non-null value)
Else if IS_AESM is NULL then it returns -1 (since it is the non-null value)

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

SQL Update when not null

I'm trying to set a column in the fabcon table only if the original column is null.
This is the code I've already tried.
UPDATE dbo.fabcon
SET ext = COALESCE(ext, ( SELECT CASE WHEN [<3] IS NOT NULL THEN '3' END AS extent
FROM dbo.spreadsheetData ))
This is the error I'm getting:
Sub-query returned more than 1 value. This is not permitted when the sub-query follows =, !=, <, <= , >, >= or when the sub-query is used as an expression.
Can anyone see where I've gone wrong?
Thanks. :)
EDIT: the two tables fabcon and spreadsheetData are linked by a column called main1
EDIT2: I've updated the query to this:
UPDATE dbo.fabcon
SET ext = (SELECT CASE WHEN [<3] IS NOT NULL THEN '3' END
FROM dbo.spreadsheetData ssd
WHERE ssd.id = fabcon.id
)
WHERE ext IS NULL;
However, its still failing with the same error.
You need a link between the table fabcon and spreadsheetData. Assuming it is called id:
UPDATE dbo.fabcon
SET ext = (SELECT CASE WHEN [<3] IS NOT NULL THEN '3' END)
FROM dbo.spreadsheetData ssd
WHERE ssd.id = fabcon.id
)
WHERE ext IS NULL;
Note that I removed the coalesce() and replaced it with a where clause for the update. This prevents the query from updating rows unnecessarily (with unchanged values).
DECLARE #PAth INT
Select #path = COALESCE(ext,'')+[<3]+';' FROM dbo.spreadsheetData
UPDATE dbo.fabcon
SET ext = CASE WHEN #PAth IS NOT NULL
THEN '3' ELSE ''
END AS Extent
FROM dbo.fabcon f WHERE f.id = #path
may be this works i think
This should work for you. Assuming I have understood your original query correctly
UPDATE dbo.fabcon
SET
ext = ssd.[column to insert data from]
FROM
dbo.spreadsheetData ssd
WHERE
ext IS NULL
AND
fabcon.id = ssd.id
This assumes the id in the ssd table is the id from the fabcon table. Otherwise just change the id matching based on your column specification
UPDATE dbo.fabcon
SET ext = (SELECT TOP 1 CASE WHEN [<3] IS NOT NULL THEN '3' END
FROM dbo.spreadsheetData ssd
WHERE ssd.main1 = fabcon.main1
ORDER BY 1
)
WHERE ext IS NULL;
with the 2 tables related via "main1" (as now indicated) and an arbitrary "TOP 1" thrown in to avoid the error, this might work.
but: the best way to get an answer is to provide some sample data and an expected result

conditional handling of update sql script depending on input parameter

I am new to SQL and using Oracle 11. I need to write a sql script which uses different update command based on whether the input param is null or not null.
I need something like this
['&' followed by the parameter name is the way i see parameters being used in other such
script for our project]
IF &PRG_ID IS NULL
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID);
ELSE
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE
P.PROGRAM_ID = &PRG_ID AND EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID);
END IF;
Something like (is not tested):
UPDATE PROGRAM_TABLE P SET HANDLED_IND = 'Y' WHERE EXISTS
(SELECT 1 FROM HANDLED_PROGRAM H WHERE H.PROGRAM_ID = P.PROGRAM_ID)
AND (&PRG_ID IS NULL OR P.PROGRAM_ID = &PRG_ID);
But take into account this change may lead to the performance degradation for the case when PRG_ID has definite value.