Unix Sql -- Updating One table from a 2nd table -- null value issue - sql

I have been looking at the web for hours trying to determine what is wrong with my code. I keep receiving the ORA-01407: cannot update ("AMIOWN"."MEMBER"."LANGUAGE_X") to NULL. I need to update the language in the member table when the language in the contract table is not null. The 3rd table is required in order to drill down to specific members. Below is the code:
update member m
set language_x =
(select language_x
from contract c
where m.contract_nbr = c.contract_nbr and
c.language_x is not null and
m.member_nbr =
(select member_nbr
from member_span ms
where m.member_nbr = ms.member_nbr and
ms.void = ' ' and
ms.carrier = 'PM' and
ms.prog_nbr = 'GP' and
ms.region = 'TR' and
(ms.ymdeff <= 20160218 or ms.ymdeff > 20160218) and
ms.ymdend > 20160218
)
);
Some of the postings also suggested adding another line after the last parenthesis checking for:
where exists (select 1 from contract c where m.contract_nbr = c.contract_nbr and c.language_x is not null);
I am working in a unix environment, tables are contained in an amisys database.
Thank you for any suggestions.

you need to add the where condition to your update statement
update member m
set language_x = (select language_x
from contract c
where m.contract_nbr = c.contract_nbr
and c.language_x is not null
and m.member_nbr = (select member_nbr
from member_span ms
where m.member_nbr = ms.member_nbr
and ms.void = ' '
and ms.carrier = 'PM'
and ms.prog_nbr = 'GP'
and ms.region = 'TR'
and (ms.ymdeff <= 20160218 or ms.ymdeff > 20160218)
and ms.ymdend > 20160218)
)
where exists (select language_x
from contract c
where m.contract_nbr = c.contract_nbr
and c.language_x is not null
and m.member_nbr = (select member_nbr
from member_span ms
where m.member_nbr = ms.member_nbr
and ms.void = ' '
and ms.carrier = 'PM'
and ms.prog_nbr = 'GP'
and ms.region = 'TR'
and (ms.ymdeff <= 20160218 or ms.ymdeff > 20160218)
and ms.ymdend > 20160218)
);
Below is a more elegant solution, but depending on your datamodel it might or might not work
Update
(
select m.language_x as oldval
c.language_x as newval
from member m
,contract c
where m.contract_nbr = c.contract_nbr
and c.language_x is not null
and m.member_nbr = (select member_nbr
from member_span ms
where m.member_nbr = ms.member_nbr
and ms.void = ' '
and ms.carrier = 'PM'
and ms.prog_nbr = 'GP'
and ms.region = 'TR'
and (ms.ymdeff <= 20160218 or ms.ymdeff > 20160218)
and ms.ymdend > 20160218)
) t
set t.oldval =t.newval

Related

This SQL query never stops execution when data IN clause have a long list of data

SELECT DISTINCT
KV_CODE
FROM
PERSON, PERSON2, ORGANIZATIONRIGHTS
WHERE
KV_CODE IN (1000 entries),
AND ((EXISTS (SELECT kc_startdate
FROM organization
WHERE v_mainorgcode = kv_orgcode
AND c_mainorgregistrynr = kc_registrynr
AND (c_enddate IS NULL OR c_enddate = '' OR c_enddate >= '20161022')
)
))
OR (v_mainorgcode IS NULL OR v_mainorgcode = '')
AND ORGANIZATIONRIGHTS.kv_personcode = '100000'
AND c_mainorgregistrynr = ORGANIZATIONRIGHTS.kc_orgregistry
AND v_mainorgcode = ORGANIZATIONRIGHTS.kv_orgcode
ABD (c_enddate = '' OR c_enddate IS NULL OR c_enddate >= '20161022')

How to use bitwise operator in existing sql query?

Here is my sql query. I have column name "ExpenseBucketCoverage" in claim table in which I am storing bitwise operators store multiple values in one column like below
MED_COPAY = 1, MED_DED= 10, MED_COINS = 100, RX_COPAY = 1, RX_DED= 10, RX_COINS = 100
I want to replace hard coded value like MED_COPAY, MED_COINS, MED_DED, RX_DED, RX_COINS & RX_COPAY in query by using ExpenseBucketCoverage column value. Can some one please tell me how can I do that?
Someone has suggested me below soultion
retrieve data from claim and left joining the first matched record in eligibility. And then add custom code to loop through the datarows to split the rows by covered expense bucket, and set the service category code in-memory column based on the ExpenseBucketCoverage value for the claim.
SELECT
e.categoryid,
c.servicetype,
'II' AS RepordType,
e.TPAId AS TPA_Id,
e.EmployerCode,
e.SubscriberId,
e.MemberID,
c.ServiceFrom,
c.ServiceTo,
CASE
WHEN e.categoryid IN( 'MED_DED', 'RX_DED' ) THEN
deductible
WHEN e.categoryid IN( 'MED_COINS', 'RX_COINS' ) THEN
isnull(coins,0)
WHEN e.categoryid IN( 'MED_COPAY', 'RX_COPAY' ) THEN
copay
ELSE 0
END AS ClaimAmount,
'' AS AccountTypeCode,
'1' ClaimsCrossoverAutoPay,
e.CategoryId,
CASE c.ServiceType
WHEN 'H' THEN
CASE e.PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS PayProvider,
CASE c.ServiceType
WHEN 'H' THEN
CASE PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS ReimbursementMethod,
CASE c.ServiceType
WHEN 'H' THEN c.Provider
WHEN 'P' THEN ''
END AS ProviderId,
'1' EnforceAccountEffectiveDates,
c.Id,
c.ClaimNumber + e.CategoryId as 'ExternalClaimNumber',
c.ProviderName,
c.CarrierId + ';' + c.SourceClaimNumber AS Notes
FROM Claim c
INNER JOIN Eligibility e ON e.TPAId = c.TPAId AND e.EIN = c.EIN AND
c.Processed = 'Y' AND e.FilterType = 'Eligibility'
AND c.TPAId='PrimePay'
AND (c.ServiceFrom >= e.BenefitEffectiveDate
AND c.ServiceFrom <=e.BenefitTermDate)
AND ( ( c.PayorID = c.PatientSSN
AND e.SubscriberSSN = c.PatientSSN
AND (c.EmployeeFirstName = c.PatientFirstName
AND c.EmployeeLastName = c.PatientLastName)
AND(e.MemberSSN = '' OR e.MemberSSN = NULL)
AND(e.MemberFirstName = '' OR e.MemberFirstName = NULL)
AND(e.MemberLastName = '' OR e.MemberLastName = NULL))
OR((c.PayorID != c.PatientSSN AND e.MemberSSN = c.PatientSSN
AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName = c.PatientLastName)
OR(c.PayorID != c.PatientSSN AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName= c.PatientLastName)))
AND (( c.Servicetype ='P'
AND e.CategoryID IN('RX_COINS','RX_COPAY', 'RX_DED' ))
OR ( c.Servicetype = 'H'
AND e.CategoryID IN( 'MED_COINS','MED_COPAY', 'MED_DED' )))

Invalid column error in SQL Server 2008

I am trying to run the following query,
DECLARE #p_UserName as nvarchar(100)
DECLARE #p_Department as int
DECLARE #p_Section as int
DECLARE #p_SubSection as int
DECLARE #p_PermissionGroup as int
DECLARE #p_DistributionGroup as int
DECLARE #p_Permission as nvarchar(100)
DECLARE #p_IfPerChecked as bit
DECLARE #p_Role as int
SET #p_UserName = ''
SET #p_Department = NULL
SET #p_Section = NULL
SET #p_SubSection = NULL
SET #p_PermissionGroup = NULL
SET #p_DistributionGroup = NULL
SET #p_Permission = ''
SET #p_Role = NULL
SELECT Users.EnglishName,
(SELECT Designation.TitleEnglish FROM Designation WHERE Users.Designation = Designation.ID) AS [Role],
(SELECT Department.TitleEnglish FROM Department WHERE Users.DepartmentID = Department.ID) AS [Department],
(SELECT Section.TitleEnglish FROM Section WHERE Users.SectionID = Section.SectionID) AS [Section],
(SELECT SubSection.TitleEnglish FROM SubSection WHERE Users.SubSectionID = SubSection.SubSectionID) AS [Sub-Section],
(SELECT Groups.TitleEnglish FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 0
) AS [Permissions Group],
(SELECT Groups.TitleEnglish FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 1
) AS [Distribution Group],
(CASE
WHEN Users.ApplyUserRights = 1 THEN dbo.fn_GetUserPermissions('Users', Users.UserID)
WHEN Users.ApplyUserRights = 0 THEN dbo.fn_GetUserPermissions('Groups', (SELECT GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID))
END) AS [Permissions]
FROM Users WHERE
((#p_UserName <> NULL OR #p_UserName <> '') AND #p_UserName = Users.UserName) OR
((#p_Department <> NULL OR #p_Department <> 0) AND #p_Department = Users.DepartmentID) OR
((#p_Section <> NULL OR #p_Section <> 0) AND #p_Section = Users.SectionID) OR
((#p_SubSection <> NULL OR #p_SubSection <> 0) AND #p_SubSection = Users.SubSectionID) OR
((#p_PermissionGroup <> NULL OR #p_PermissionGroup <> 0) AND #p_PermissionGroup = (
SELECT Groups.GroupID FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 0
)) OR
((#p_DistributionGroup <> NULL OR #p_DistributionGroup <> 0) AND #p_DistributionGroup = (
SELECT Groups.GroupID FROM Groups WHERE
(SELECT UserRights.GroupID FROM UserRights WHERE Users.UserID = UserRights.UserID) = Groups.GroupID
AND Groups.IsDistribution = 1
)) OR
(1 = CASE
WHEN Users.ApplyUserRights = 1 AND #p_Permission = 'AllowChangePassword' THEN 0
ELSE dbo.fn_CheckPemission(#p_Permission, UserID)
END) OR
((#p_Role <> NULL OR #p_Role <> 0) AND #p_Role = Users.Designation) OR
(1 = 1)
GROUP BY CASE #GroupBy
WHEN 'DepartmentID' THEN Users.DepartmentID
WHEN 'SectionID' THEN Users.SectionID
WHEN 'SubSectionID' THEN Users.SubSectionID
ELSE Users.EnglishName
END
And I keep getting the error
Column 'Users.EnglishName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Please help!
Like the message states, you need to group by the EnglishName column unless it is used in an aggregate function(Max or sum or likewise).
The issue is evident from your error message. Use GROUP BY in case of aggregate functions.

Create Clustered/Non Clustered Index in view

I am having a view which returns value to a table. The process takes a long time... So I felt it works better if go for indexing on view. Can anyone plz guide me to add index to view.
ALTER View [dbo].[GetApplicationBudgetAndUtilized]
as
Select *, (convert(money,isnull(TotalBudget,0)) - convert(money,isnull(Utilized,0))) as [Left] from (
SELECT Distinct PM.PKID AS ProgramID, F.PKID as FundID, I.PKID as ProjectID,
MYB.PKID AS MultiYearBudgetID,
(Select Isnull(Sum(IsNull(PBC1.BudgetAmount,0)),0) from ProgramBudgetConfiguration PBC1
Where pbc1.MultiYearBgtIdFkId = pbc.MultiYearBgtIdFkId and PBC1.IsActive = 1 and LOVBudgetTypeIDFKID = 'BT_INC' group
by pbc1.MultiYearBgtIdFkId) AS TotalProgramBudget,
App.TotalBudget,
I.PKID,
(Case when exists (Select 'x' from InstallationTransactionHeader Where ParentPrjNumber = I.PKID and IsDelete = 0)
then
(case
when not exists (select cast(isnull(I1.CustInstallIncAmt,0.00) + isnull(I1.SPInstIncentiveAmt,0.00) + isnull(I1.ThirdPartyIncentive,0.00) as Money)
from InstallationTransactionHeader I1 where I1.ParentPrjNumber is not null and I1.StatusFKID in ('ITS_SUB','ITS_APP','ITS_VRF') and
I1.ParentPrjNumber = I.PKID and I1.FundRequestIDFKID is not null) then (select '0')
else
(select isnull(sum(cast(isnull(I1.CustInstallIncAmt,0.00) + isnull(I1.SPInstIncentiveAmt,0.00) + isnull(I1.ThirdPartyIncentive,0.00) as Money)),0)
from InstallationTransactionheader I1 where I1.StatusFKID in ('ITS_SUB','ITS_APP','ITS_VRF') and I1.ParentPrjNumber is not null and
I1.ParentPrjNumber = I.PKID and I1.isdelete = 0 and I1.FundRequestIDFKID is not null group by I1.parentPrjNumber)
End)
else
--isnull(cast(isnull(I.CustInstallIncAmt,0.00) + isnull(I.SPInstIncentiveAmt,0.00) + isnull(I.ThirdPartyIncentive,0.00) as Money),0)
(case
when not exists (select isnull(cast(isnull(I.CustInstallIncAmt,0.00) + isnull(I.SPInstIncentiveAmt,0.00) + isnull(I.ThirdPartyIncentive,0.00) as Money),0) from
InstallationTransactionHeader I where StatusFKID in ('ITS_SUB','ITS_APP','ITS_VRF') and IsDelete = 0 and
PKID = I.PKID and I.FundRequestIDFKID is not null) then 0--(select '0')
else
(select isnull(sum(cast(isnull(I1.CustInstallIncAmt,0.00) + isnull(I1.SPInstIncentiveAmt,0.00) + isnull(I1.ThirdPartyIncentive,0.00) as Money)),0)
from InstallationTransactionheader I1 where I1.StatusFKID in ('ITS_SUB','ITS_APP','ITS_VRF') and IsDelete = 0 and I1.FundRequestIDFKID is not null and
PKID = I.PKID)
End)
End) as Utilized
--Cast(App.TotalBudget - (isnull(I.CustInstallIncAmt,0.00) + isnull(I.SPInstIncentiveAmt,0.00) + isnull(I.ThirdPartyIncentive,0.00)) as Money) as [Left]
FROM
( SELECT PKID as FundID, ProgramID AS ProgramID, (ISNULL(IncentiveAmount,0.00) + ISNULL(CustInstallIncAmt, 0.00) + ISNULL(SPInstIncentiveAmt, 0.00) ) AS TotalBudget
FROM dbo.FundRequestHeader AS F WHERE (IsDelete = 0) AND (IsActive = 1) AND (StatusFKID in ('IAS_APP','IAS_CAN'))) AS App
INNER JOIN dbo.ProgramMaster AS PM
INNER JOIN dbo.MultiYearBudget AS MYB ON PM.PKID = MYB.ProgramIDFKID
INNER JOIN dbo.ProgramBudgetConfiguration AS PBC ON MYB.PKID = PBC.MultiYearBgtIdFkId ON App.ProgramID = PM.PKID
INNER JOIN dbo.FundRequestHeader AS F ON F.PKID = App.FundID
INNER JOIN dbo.InstallationTransactionHeader AS I ON I.FundRequestIDFKID = F.PKID
WHERE (PM.IsActive = 1) AND (MYB.IsActive = 1) AND (PBC.IsActive = 1) AND
(PBC.LOVBudgetTypeIDFKID = 'BT_INC') And (F.StatusFKID in ('IAS_APP','IAS_CAN'))
--and (1=case when(select ProgramTypeIDFKID from InstallationTransactionHEader where ProgramTypeIDFKID='IT_PRE' and ParentPrjNumber is null and PaymentSchedule=1)='IT_PRE' then 0 else 1 end)
--and I.ProgramTypeIDFKID <> 'IT_PRE' or I.PaymentSchedule = 0 or (I.ProgramTypeIDFKID = 'IT_PRE' and I.PaymentSchedule = 1 and I.ParentPrjNumber is not null)
and MYB.PKID = F.BudgetPeriodID and ((MYB.IsActive = 1))-- or ((MYB.status = 0) and F.StatusFKID ='IAS_APP'))
and (MYB.Status = 1 or (MYB.Status =(case when exists(select M.status from MultiYearBudget M, FundRequestHeader F
where F.ProgramID=M.ProgramIDFKID and F.ProgramTypeIDFKID <>'IT_FCFS' and M.Pkid=F.BudgetPeriodID
and F.StatusFKID='IAS_APP' and F.ProgramID=PM.PKID and M.Status=0 )then 0 else 1 end))
)
)temp
GO
Indexed views have certain limitations
In your case, I can immediately see some blockers
SELECT *
Derived tables
So read this article, and see how far you get...
However, you should be able to look the execution plan identify bottlenecks where you can add indexes to the bases tables

SQL's stored procedure

Select #vCtlPeriod = period from ctl
IF #vCtlPeriod = #Period
Begin
SET #vHdr = 'StkHdr'
SET #vDtl = 'StkDtl'
End
ELSE
Begin
SET #vHdr = 'HStkHdr'
SET #vDtl = 'HStkDtl'
End
SELECT H.Loc, D.MatCod, D.MatQty, H.Slipno, H.SapWH,
P.InvPri, P.InvPri * D.MatQty as InvAmt, P.ProCat, P.MaiGrp, P.SubGrp1,P.SerCod ,
SUBSTRING(Z.LotNum,1,4) As ProdMth, F.UpdGSPI,
Class = Case WHen F.UpdGSPI = '9' Then 'D'
When Cast(SUBSTRING(Z.LotNum,1,2) AS INT) * 12 + Cast(SUBSTRING(Z.LotNum,3,2) AS INT)>= #vPerClsA Then 'A'
When Cast(SUBSTRING(Z.LotNum,1,2) AS INT) * 12 + Cast(SUBSTRING(Z.LotNum,3,2) AS INT)>= #vPerClsB Then 'B'
When Cast(SUBSTRING(Z.LotNum,1,2) AS INT) * 12 + Cast(SUBSTRING(Z.LotNum,3,2) AS INT)>= #vPerClsC1 Then 'C1'
When Cast(SUBSTRING(Z.LotNum,1,2) AS INT) * 12 + Cast(SUBSTRING(Z.LotNum,3,2) AS INT)>= #vPerClsC2 Then 'C2'
ELSE 'C3'
END,
FGType = Case F.UpdGSPI WHen '1' Then 'Production'
When '2' Then 'Trading'
When '3' Then 'Non GSPI'
When '9' Then 'DisContinue'
Else
'Not Maintain'
End,
FGSTATUS = Case When NOT ISNULL(Z.HolCod,'')='' or NOT ISNULL(Z.ResCod,'')='' Then 'Hold/Reserved'
When NOT ISNULL(Pd.Slipno,'')='' OR NOT ISNULL(Tg.Slipno,'')='' Then 'Pick'
ELSE ''
End,
Tg.SONum , Tg.Seq1, Tg.SptNum, Tg.Qty, Od.LPNum ,
Z.LotNum, Z.DocRefNo, IsNull(Z.HolCod,'') As HolCod, IsNull(Z.HolInf,'') As HolInf, IsNull(Z.ResCod,'') As ResCod, IsNull(Z.ResInf,'') As ResInf
FROM #vHdr H Join #vDtl D ON D.Slipno = H.Slipno And H.Period = #Period
Join Prd P ON D.MatCod = P.ProCod
Join LOCW L ON L.Loc = H.Loc AND L.Pickable <> 'Z' AND L.Putable <> 'Z' AND L.WhCod =H.SapWh
Join ZV00 Z ON Z.SlipNo = H.SlipNo
LEFT JOIN PrdFG F on F.Procod = D.MatCod
LEFT JOIN (SELECT Slipno, MatCod, Qty =Sum(QtyPck) from PickD group by Slipno, matcod) Pd on D.SlipNo = Pd.Slipno and D.MatCod = Pd.MatCod
LEFT JOIN TagLotQty Tg ON TG.SlipNo = D.SlipNo and Tg.procod = D.MatCod
LEFT JOIN OrdD Od ON Od.SONum = Tg.SONum and Od.Seq1 = Tg.Seq1
Above is some of my code in my stored procedure, I intend to check the current period from the ctl file, if the period is current period then I will get from current transaction table or else it will be retrieved from history transaction table.
But I failed at the last select statement. Can we predefined the table name in the variable?
No, we can't. You should use Dynamic SQL or move your SELECT into IF statement
Isn't it simpler to just:
Select #vCtlPeriod = period from ctl
IF #vCtlPeriod = #Period
Begin
SELECt A.*, B.* from StkHdr A Join StkDtl B ON A.Slipno = B.Slipno
End
ELSE
Begin
SELECt A.*, B.* from HStkHdr A Join HStkDtl B ON A.Slipno = B.Slipno
End
You have to use Dynamic SQL like this:
Select #vCtlPeriod = period from ctl
IF #vCtlPeriod = #Period
Begin
SET #vHdr = 'StkHdr'
SET #vDtl = 'StkDtl'
End
ELSE
Begin
SET #vHdr = 'HStkHdr'
SET #vDtl = 'HStkDtl'
End
declare #cmd varchar(500)
select #cmd = "SELECT A.*, B.* from " + #vHDr + " A Join " + #vHdtl + " B ON A.Slipno = B.Slipno "
exec (#cmd)