oracle update with case statement issue - sql

Hi I am trying to execute following Update statement
UPDATE rebate_admin.PRODUCT_REBATE_ITEM_RULE_GROUP SET rule_priority_Nbr =
CASE
WHEN ( rule_priority_Nbr = 3) THEN (rule_priority_Nbr+1)
WHEN ( rule_priority_Nbr = 4 ) THEN (rule_priority_Nbr-1)
END
WHERE DEFAULT_RULE_IND = 1 AND ACTIVE_IND= 1 AND RULE_PRIORITY_NBR NOT IN (9999999900, 1)
AND PRODUCT_REBATE_DETAIL_SEQ IN (843 )
It fails with error:
Cannot update (%s) to NULL.

You have a NOT NULL constraint on rule_priority_Nbr column. Your CASE statement doesnt have the ELSE, so when rule_priority_Nbr not 3 and not 4 the CASE will return null
Try like this for instance
UPDATE rebate_admin.PRODUCT_REBATE_ITEM_RULE_GROUP
SET rule_priority_Nbr =
CASE WHEN ( rule_priority_Nbr = 3) THEN (rule_priority_Nbr+1)
WHEN ( rule_priority_Nbr = 4 ) THEN (rule_priority_Nbr-1)
ELSE rule_priority_Nbr
END
WHERE DEFAULT_RULE_IND = 1
AND ACTIVE_IND= 1
AND RULE_PRIORITY_NBR NOT IN (9999999900, 1)
AND PRODUCT_REBATE_DETAIL_SEQ IN (843 )

Related

How to update column values which is resulted in select statement

I'm trying to find the even or odd numbers from given values, the thing is I want to update the E_and_o column which is resulted in select statement. can someone help me please.
Don't know where I'm doing it wrong.
Create table EvenRodd(
Nrow INT,
E_and_O VARCHAR(15));
///Select statement//
Select nrow, E_and_O,
Case
When Mod(nrow, 2) != 0 then 'Odd'
When Mod(nrow, 2) = 0 then 'Even'
end
From EvenRodd;
//Update //////
Update evenrodd set e_and_o = (Select evenrodd.nrow,
Case
When Mod(evenrodd.nrow, 2) != 0 then 'Odd'
When Mod(evenrodd.nrow, 2) = 0 then 'Even'
end
From EvenRodd) WHERE e_and_o IS NULL;
Update evenrodd set e_and_o =
Case
When Mod(nrow, 2) != 0 then 'Odd'
When Mod(nrow, 2) = 0 then 'Even'
end
EDIT: if you want to set e_and_o as computed column, and not update it every time:
ALTER TABLE evenrodd DROP COLUMN e_and_o;
ALTER TABLE evenrodd ADD e_and_o AS
Case
When Mod(nrow, 2) != 0 then 'Odd'
When Mod(nrow, 2) = 0 then 'Even'
end;
I would bet that you pass too many values out from inner select query. Try something like this:
/Update //////
Update evenrodd set e_and_o = (Select
Case
When Mod(evenrodd.nrow, 2) != 0 then 'Odd'
When Mod(evenrodd.nrow, 2) = 0 then 'Even'
end
From EvenRodd) WHERE e_and_o IS NULL;

SQL Server - CASE on where clause

I'm working on a stored procedure that is a select query. Under the WHERE clause I am filtering the return results to return only if there is a PageTitle= 'STA'
Here is am example of my query :
#InputCountryId INT,
#InputIndustryId INT
AS
BEGIN
SELECT
r.IdCountry,
r.IdIndustry,
r.PageTitle,
r.IndustryName
From dbo.Reports r
WHERE
r.IdCountry = #InputCountryId
r.IdIndustry = #InputIndustryId
r.PageTitle = 'STA'
END
The ending condition r.PageTitle I would like it to be applied ONLY IF InputCountry = 1 if not; do not include the filter.
I've attempted this by including a CASE. I am having a syntax error any time I try and introduce this case. Is this something that is possible? Am I implementing it incorrectly?
Here is an example of the stored proc with the CASE included.
#InputCountryId INT,
#InputIndustryId INT
AS
BEGIN
SELECT
r.IdCountry,
r.IdIndustry,
r.PageTitle,
r.IndustryName
From dbo.Reports r
WHERE
r.IdCountry = #InputCountryId
r.IdIndustry = #InputIndustryId
CASE WHEN #InputCountryId = 1 THEN
r.PageTitle = 'STA'
END
END
Try it this way:
WHERE
r.IdCountry = #InputCountryId and
r.IdIndustry = #InputIndustryId and
(#InputCountryId <> 1 or r.PageTitle = 'STA')
You dont need case statement. You can use OR clause
WHERE
r.IdCountry = #InputCountryId
r.IdIndustry = #InputIndustryId
(#InputCountryId != 1 OR r.PageTitle = 'STA')
this only filters PageTitle with STA when InputCountry is 1
I can't test with your data, but here is a CASE in a WHERE clause that works.
DECLARE #Variable INT = 3
SELECT GETDATE()
WHERE 1 =
CASE
WHEN #Variable = 1 THEN 1
WHEN #Variable = 3 THEN 1
WHEN #Variable = 5 THEN 1
ELSE 0
END

Nested CASE WHEN statement

I have one CASE WHEN condition (CompanyGUID and LineGUID) and I need 4 different calculations with 4 different columns.
The calculation works fine, but I just wonder is any way to make it more convenient? like for example maybe using COALESCE trick in this case?
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN isnull(ddaWC.archexMod,1)
END as ExperienceMod,
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN case when convert(int,ddawc.premModTtl) <= 0 then 1
when convert(int,ddawc.premModTtl) >= 0
then (1 + ddaWC.premschmod / ddawc.premModTtl) end
END as ScheduleMod,
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN isnull(ddaWC.TMpercent,1) END as TerritoryMod,
CASE WHEN dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
THEN case when convert(int,ddaWC.SchedPercent) = 0 or
ddaWC.SchedPercent is not null
then (isnull(ddaWC.archexMod,1)
* (convert(decimal(5,2),isnull(ddaWC.SchedPercent,1))))
* isnull(ddaWC.TMpercent,1)
when ddaWC.SchedPercent is null
then 1 END
END as EffectiveMod
Something like this? The OUTER APPLY will perform a row-based select which return NULLs in case of your condition not being fullfilled. Otherwise it will return the same values you've specified. This will only work, if you really need the same logic for all columns.
SELECT otherColumn
,ConditionalColumns.*
FROM YourTable
OUTER APPLY
(
SELECT isnull(ddaWC.archexMod,1) as ExperienceMod,
case when convert(int,ddawc.premModTtl) <= 0 then 1
when convert(int,ddawc.premModTtl) >= 0
then (1 + ddaWC.premschmod / ddawc.premModTtl) end as ScheduleMod,
isnull(ddaWC.TMpercent,1) as TerritoryMod,
case when convert(int,ddaWC.SchedPercent) = 0 or ddaWC.SchedPercent is not null
then (isnull(ddaWC.archexMod,1)
* (convert(decimal(5,2),isnull(ddaWC.SchedPercent,1))))
* isnull(ddaWC.TMpercent,1)
when ddaWC.SchedPercent is null
then 1 END as EffectiveMod
WHERE dbo.tblCompanyLocations.CompanyGUID = '29634AF7-D0A2-473D-9574-405C23E10F02'
AND dbo.tblQuotes.LineGUID = '1CB72920-B3FC-4822-8030-37B50A2810EB'
) AS ConditionalColumns
Like this:
somewhere before code:
Declare #compGuid char(37) = '29634AF7-D0A2-473D-9574-405C23E10F02'
Declare #lineGuid char(37) = '1CB72920-B3FC-4822-8030-37B50A2810EB'
and modify From Clause in SQL statement to define aliases
cl for tblCompanyLocations, and
q for tblQuotes
then the case in the Select clause of the SQL code could be reduced to:
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then isnull(ddaWC.archexMod,1) end ExperienceMod,
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then case when cast(ddawc.premModTtl as int) > 0
then (1 + ddaWC.premschmod / ddawc.premModTtl)
else 1 end end ScheduleMod,
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then isnull(ddaWC.TMpercent,1) end TerritoryMod,
case when cl.CompanyGUID = #compGuid
and q.LineGUID = #lineGuid
then case when cast(ddaWC.SchedPercent as int) = 0
then isnull(ddaWC.archexMod,1)
* cast(ddaWC.SchedPercent as decimal(5,2)))
* isnull(ddaWC.TMpercent,1)
when ddaWC.SchedPercent is null
then 1 end end EffectiveMod
which is a bit more readable... (check the logic though)

SQL stored procedure with CASE

Hello i have a quation with my stored procedure , i use 2 cases here , the first case it shows me right values and its OK, the second shows me only Null values in the field TirType , i don't understand what is the problem
CREATE VIEW dbo.YUITY
SELECT CAST(dbo.SC5116.CODE AS int) AS код, dbo.SC5116.DESCR AS Наименование,
CAST(dbo.SC3420.CODE AS int) AS TIR, dbo.SC3420.SP4947 AS Date,
CASE WHEN SC3420.SP4949 <> ' 0 ' THEN 'ПовышСтрах' ELSE 'ОснСтрах' END AS VID,
CASE WHEN dbo.SC3420.SP9214 = '714' THEN '4v' END AS TirType
FROM dbo.SC3420 INNER JOIN
dbo.SC5116 ON dbo.SC3420.SP3422 = dbo.SC5116.ID
WHERE (dbo.SC3420.SP4947 <> '01.01.1753')
Don't forget END after CASE. It will return NULL if no ELSE is specified and value is not '714'.
CREATE VIEW dbo.YUITY
AS
SELECT CAST(dbo.SC5116.CODE AS int) AS код, dbo.SC5116.DESCR AS Наименование,
CAST(dbo.SC3420.CODE AS int) AS TIR, dbo.SC3420.SP4947 AS Date,
CASE WHEN SC3420.SP4949 <> ' 0 ' THEN 'ПовышСтрах' ELSE 'ОснСтрах' END AS VID,
CASE WHEN dbo.SC3420.SP9214 = '714' THEN '4v' ELSE '' END AS TirType
FROM dbo.SC3420 INNER JOIN
dbo.SC5116 ON dbo.SC3420.SP3422 = dbo.SC5116.ID
WHERE (dbo.SC3420.SP4947 <> '01.01.1753')

Is this a syntax error?

IF( #ActiveStatus = 1 )
BEGIN
#PasswordStatus = 3
END
ELSE
SET #PasswordStatus = (SELECT password_status
FROM tbl_user
WHERE login_name = #LoginName_fromApp
AND password=#Pass
)
I am using Microsoft SQL Server Management Studio..
yes
In IF clause you need to set variable so use select or set, do not write directly #PasswordStatus = 3 write set/select #PasswordStatus = 3
try
if( #ActiveStatus = 1 )
begin
--chage of code
set #PasswordStatus = 3 --or select #PasswordStatus = 3
END
else
begin
Set #PasswordStatus = (select password_status from tbl_user where login_name=#LoginName_fromApp and password=#Pass)
end
EDIT
Comment of -# marc_s Use SET #Var = 3 if you're just setting a "scalar" value - use SELECT #Var = ID FROM dbo.Table1 if you're actually selecting from a table (or view)
when you r setting valuer to any variable use Set as followed
if( #ActiveStatus = 1 )
begin
Set #PasswordStatus = 3
END
else
begin
Set #PasswordStatus = (select password_status from tbl_user where
login_name=#LoginName_fromApp and password=#Pass)
end
Select #PasswordStatus
Another one
select
#PasswordStatus = case when #ActiveStatus = 1 then 3 else password_status end
from
tbl_user
where
login_name = #LoginName_fromApp and password=#Pass