how to fix sql case syntax - sql-server

work on sql-server-2005
SELECT A.specific_customization_id ,
A.customization_id ,
A.customization_value ,
A.customization_price ,
A.customization_cost ,
A.customization_code ,
A.customization_check ,
A.sort_order ,
A.option_code ,
A.product_id ,
A.image_1 ,
A.image_2 ,
A.MainProductID ,
A.customization_product_id ,
A.inactive ,
A.customization_description ,
A.customization_select ,
A.UOM ,
A.allow_recur ,
auto_reorder = CASE A.MainProductID --it's an int type column
WHEN NULL THEN A.allow_recur
ELSE ( SELECT allow_recur
FROM wc_product
WHERE product_id = A.MainProductID
)
END
FROM dbo.wc_product_specific A
WHERE A.product_id = 1133
Using the above query I want to set auto_reorder column value .want to set two login
When My MainProductID column is null I want to set allow_recur column value
When it’s not null I want to set another table column value
My logic 2) works fine ,fail to understand why does 1) not work? Help me to fix the problem?
If have any query plz ask thanks in advance.

Does this work:?
SELECT A.specific_customization_id ,
A.customization_id ,
A.customization_value ,
A.customization_price ,
A.customization_cost ,
A.customization_code ,
A.customization_check ,
A.sort_order ,
A.option_code ,
A.product_id ,
A.image_1 ,
A.image_2 ,
A.MainProductID ,
A.customization_product_id ,
A.inactive ,
A.customization_description ,
A.customization_select ,
A.UOM ,
A.allow_recur ,
A.auto_reorder = CASE --it's an int type column
WHEN A.MainProductID IS NULL THEN A.allow_recur
ELSE B.allow_recur
END
FROM dbo.wc_product_specific A
left outer join wc_product B on B.product_id = A.MainProductID
WHERE A.product_id = 1133

Related

Slow Query only for specific values in a Where IN Clause

I wonder if anyone has come across this behaviour before in the following query if I filter on "i.InspectionTypeID IN (x)" if x results in a large number (like about 2/3 of the entire result set of about 600 000) It takes a significantly longer amount of time to return even no results (15-30 seconds vs instantly otherwise). I can include the query and the execution plans if that helps. I can see a few differences between the queries but no idea how to refactor to improve it. We have indexes around the place that should (and have) improved performance in other places but I seem to be stuck on this one. Indexes don't appear to affect performance. At least what I tried.
Slow Query Plan: https://www.brentozar.com/pastetheplan/?id=SyBDgxSeO
EDIT: New Plan and Updated the Query as per advice from #charlieface
Here's the query:
declare #currentUserID int = 1018
declare #currentCompanyID int = 43
declare #status int = 0
declare #pageSize int = 2147483647
declare #pageNumber int = 1
DECLARE #notPreferredAssetIDsTable TABLE (id INT)
INSERT INTO #notPreferredAssetIDsTable
SELECT RecordID FROM SettingList sl
WHERE SettingID = (SELECT TOP 1 ID FROM Setting
WHERE SystemUserID = #currentUserID AND Name = 'Preferred Assets' AND Status = #status)
AND Value = '0'
AND Status = #status
SELECT i.ID As ID, a.AssetTypeID, at.Name as AssetTypeName, a.Code as AssetCode, a.Name as AssetName, CASE WHEN sections.SectionCount = 1 THEN cr.SectionName ELSE '' END as SectionName, c.Name as ContractName, reg.Name as RegionName, i.Carriageway,
startStatus.StatusDate as StartDate, startPoint.Name as StartPointName, i.ChainageFrom,
CASE WHEN i.Carriageway IS NULL THEN NULL ELSE i.ChainageFrom - (CASE WHEN i.Carriageway = 0 THEN startPoint.Forward ELSE startPoint.Reverse END) END StartDistancePast,
completedStatus.StatusDate As EndDate, endPoint.Name as EndPointName, i.ChainageTo,
CASE WHEN i.Carriageway IS NULL THEN NULL ELSE i.ChainageTo - (CASE WHEN i.Carriageway = 0 THEN endPoint.Forward ELSE endPoint.Reverse END) END EndDistancePast,
CASE WHEN i.Carriageway IS NULL THEN '' ELSE (CASE WHEN (ISNULL(OtherDirectionInspection.ID, 0) > 0) THEN 'Forward/Reverse'
WHEN i.Carriageway = 0 THEN 'Forward' ELSE 'Reverse' END) END as CarriagewayName,
dbo.GetInspectionTypeNamesForInspectionID(i.ID) AS InspectionTypeName, JobCount.Total As JobCount, i.CreatedDate,
CreatedUserName.DisplayName as CreatedUserName, AssignedUserName.DisplayName as AssignedUserName,
i.Comments, i.EntireRoad, i.PlannedDate, latestStatus.InspectionStatus AS LatestInspectionStatus, completedStatus.StatusDate AS CompletedDate, it.CompanyID as InspectionTypeCompanyID, i.ContractID, i.InspectionTypeID, sections.SectionCount,
it.Category as InspectionTypeCategory, latestStatus.ModifiedDeviceID as DeviceID, latestStatus.ModifiedUserID as LatestStatusModifiedUserID, i.JobID, i.CapitalWorkID, cw.Name as CapitalWorkName, AssignedUser.Name as AssignedUserEmail,
InspectionRouteName.Name as InspectionRouteName, i.InspectionGroupID,
il.Location AS InspectionLocation,
i.OtherDirectionInspectionID , CustomText1 , CustomBit1 , CustomDate1 , CustomNumber1 , CustomReferenceItemID1 , CustomText2 , CustomBit2 , CustomDate2 , CustomNumber2 , CustomReferenceItemID2 , CustomText3 , CustomBit3 , CustomDate3 , CustomNumber3 , CustomReferenceItemID3 , CustomText4 , CustomBit4 , CustomDate4 , CustomNumber4 , CustomReferenceItemID4 , CustomText5 , CustomBit5 , CustomDate5 , CustomNumber5 , CustomReferenceItemID5 , CustomText6 , CustomBit6 , CustomDate6 , CustomNumber6 , CustomReferenceItemID6 , CustomText7 , CustomBit7 , CustomDate7 , CustomNumber7 , CustomReferenceItemID7 , CustomText8 , CustomBit8 , CustomDate8 , CustomNumber8 , CustomReferenceItemID8 , CustomText9 , CustomBit9 , CustomDate9 , CustomNumber9 , CustomReferenceItemID9 , CustomText10 , CustomBit10 , CustomDate10 , CustomNumber10 , CustomReferenceItemID10 , CustomText11 , CustomBit11 , CustomDate11 , CustomNumber11 , CustomReferenceItemID11 , CustomText12 , CustomBit12 , CustomDate12 , CustomNumber12 , CustomReferenceItemID12 , CustomText13 , CustomBit13 , CustomDate13 , CustomNumber13 , CustomReferenceItemID13 , CustomText14 , CustomBit14 , CustomDate14 , CustomNumber14 , CustomReferenceItemID14 , CustomText15 , CustomBit15 , CustomDate15 , CustomNumber15 , CustomReferenceItemID15 , CustomText16 , CustomBit16 , CustomDate16 , CustomNumber16 , CustomReferenceItemID16 , CustomText17 , CustomBit17 , CustomDate17 , CustomNumber17 , CustomReferenceItemID17 , CustomText18 , CustomBit18 , CustomDate18 , CustomNumber18 , CustomReferenceItemID18 , CustomText19 , CustomBit19 , CustomDate19 , CustomNumber19 , CustomReferenceItemID19 , CustomText20 , CustomBit20 , CustomDate20 , CustomNumber20 , CustomReferenceItemID20 , CustomText21 , CustomBit21 , CustomDate21 , CustomNumber21 , CustomReferenceItemID21 , CustomText22 , CustomBit22 , CustomDate22 , CustomNumber22 , CustomReferenceItemID22 , CustomText23 , CustomBit23 , CustomDate23 , CustomNumber23 , CustomReferenceItemID23 , CustomText24 , CustomBit24 , CustomDate24 , CustomNumber24 , CustomReferenceItemID24 , CustomText25 , CustomBit25 , CustomDate25 , CustomNumber25 , CustomReferenceItemID25 , CustomText26 , CustomBit26 , CustomDate26 , CustomNumber26 , CustomReferenceItemID26 , CustomText27 , CustomBit27 , CustomDate27 , CustomNumber27 , CustomReferenceItemID27 , CustomText28 , CustomBit28 , CustomDate28 , CustomNumber28 , CustomReferenceItemID28 , CustomText29 , CustomBit29 , CustomDate29 , CustomNumber29 , CustomReferenceItemID29 , CustomText30 , CustomBit30 , CustomDate30 , CustomNumber30 , CustomReferenceItemID30 , CustomText31 , CustomBit31 , CustomDate31 , CustomNumber31 , CustomReferenceItemID31 , CustomText32 , CustomBit32 , CustomDate32 , CustomNumber32 , CustomReferenceItemID32 , CustomText33 , CustomBit33 , CustomDate33 , CustomNumber33 , CustomReferenceItemID33 , CustomText34 , CustomBit34 , CustomDate34 , CustomNumber34 , CustomReferenceItemID34 , CustomText35 , CustomBit35 , CustomDate35 , CustomNumber35 , CustomReferenceItemID35 , CustomText36 , CustomBit36 , CustomDate36 , CustomNumber36 , CustomReferenceItemID36 , CustomText37 , CustomBit37 , CustomDate37 , CustomNumber37 , CustomReferenceItemID37 , CustomText38 , CustomBit38 , CustomDate38 , CustomNumber38 , CustomReferenceItemID38 , CustomText39 , CustomBit39 , CustomDate39 , CustomNumber39 , CustomReferenceItemID39 , CustomText40 , CustomBit40 , CustomDate40 , CustomNumber40 , CustomReferenceItemID40 , CustomText41 , CustomBit41 , CustomDate41 , CustomNumber41 , CustomReferenceItemID41 , CustomText42 , CustomBit42 , CustomDate42 , CustomNumber42 , CustomReferenceItemID42 , CustomText43 , CustomBit43 , CustomDate43 , CustomNumber43 , CustomReferenceItemID43 , CustomText44 , CustomBit44 , CustomDate44 , CustomNumber44 , CustomReferenceItemID44 , CustomText45 , CustomBit45 , CustomDate45 , CustomNumber45 , CustomReferenceItemID45 , CustomText46 , CustomBit46 , CustomDate46 , CustomNumber46 , CustomReferenceItemID46 , CustomText47 , CustomBit47 , CustomDate47 , CustomNumber47 , CustomReferenceItemID47 , CustomText48 , CustomBit48 , CustomDate48 , CustomNumber48 , CustomReferenceItemID48 , CustomText49 , CustomBit49 , CustomDate49 , CustomNumber49 , CustomReferenceItemID49 , CustomText50 , CustomBit50 , CustomDate50 , CustomNumber50 , CustomReferenceItemID50
FROM Inspection i
INNER JOIN InspectionType it On i.InspectionTypeID = it.ID
INNER JOIN SystemUser CreatedUser On CreatedUser.ID = i.CreatedUserID
INNER JOIN Contract c On i.ContractID = c.ID
INNER JOIN Region reg On c.RegionID = reg.ID
INNER JOIN InspectionLocation il ON il.InspectionID = i.ID
LEFT OUTER JOIN Asset a On i.AssetID = a.ID
LEFT OUTER JOIN SystemUser AssignedUser On AssignedUser.ID = i.AssignedUserID
LEFT OUTER JOIN Road r On i.RoadID = r.ID
LEFT OUTER JOIN AssetType at On it.AssetTypeID = at.ID
LEFT OUTER JOIN Point startPoint On i.StartPointID = startPoint.ID
LEFT OUTER JOIN Point endPoint On i.EndPointID = endPoint.ID
LEFT OUTER JOIN CapitalWork cw On i.CapitalWorkID = cw.ID
CROSS APPLY dbo.GetNameForUser_Inline(i.CreatedUserID, i.ContractID, #currentCompanyID, 0) As CreatedUserName
OUTER APPLY dbo.GetNameForUser_Inline(i.AssignedUserID, i.ContractID, #currentCompanyID, 0) As AssignedUserName
OUTER APPLY
(
SELECT TOP 1 SectionName
FROM ContractRoad cr
WHERE cr.RoadID = r.ID
AND cr.ContractID = i.ContractID
AND ((i.Carriageway = 0 AND i.ChainageFrom < cr.ChainageToForward AND cr.ChainageFromForward < i.ChainageTo)
OR (i.Carriageway = 1 AND i.ChainageFrom < cr.ChainageFromReverse AND cr.ChainageToReverse < i.ChainageTo))
AND cr.Status = #status
) cr
OUTER APPLY
(
SELECT count(SectionName) as SectionCount
FROM ContractRoad cr
WHERE cr.RoadID = r.ID
AND cr.ContractID = i.ContractID
AND ((i.Carriageway = 0 AND i.ChainageFrom < cr.ChainageToForward AND cr.ChainageFromForward < i.ChainageTo)
OR (i.Carriageway = 1 AND i.ChainageFrom < cr.ChainageFromReverse AND cr.ChainageToReverse < i.ChainageTo))
AND cr.Status = #status
) sections
OUTER APPLY
(
SELECT TOP 1 StatusDate, LocalTime.LocalTime as StatusDateLocal
FROM InspectionStatus
CROSS APPLY tzdb.UtcToLocal_Inline(StatusDate, 'Australia/Sydney') as LocalTime
WHERE Status = #status
AND InspectionID = i.ID
AND InspectionStatus = 1
ORDER BY StatusDate
) startStatus
OUTER APPLY
(
SELECT TOP 1 StatusDate, LocalTime.LocalTime as StatusDateLocal
FROM InspectionStatus
CROSS APPLY tzdb.UtcToLocal_Inline(StatusDate, 'Australia/Sydney') as LocalTime
WHERE Status = #status
AND InspectionID = i.ID
AND InspectionStatus = 5
ORDER BY StatusDate
) completedStatus
OUTER APPLY
(
SELECT TOP 1 ID, StatusDate, InspectionStatus, ModifiedDeviceID, ModifiedUserID
FROM InspectionStatus
WHERE Status = #status AND InspectionStatus.InspectionStatus <> 7
AND InspectionID = i.ID
ORDER BY CASE WHEN InspectionStatus.InspectionStatus = 6 THEN 1 ELSE 0 END, StatusDate DESC
) latestStatus
OUTER APPLY
(
SELECT COUNT(ID) As Total FROM Job
WHERE InspectionID = i.ID
AND ParentJobID IS NULL
AND Status = #status
) JobCount
OUTER APPLY
(
SELECT i2.ID
FROM dbo.Inspection i2
WHERE i2.OtherDirectionInspectionID = i.ID
AND Status = #status
) OtherDirectionInspection
OUTER APPLY
(
SELECT TOP 1 ir.Name FROM InspectionRoute ir
INNER JOIN InspectionGroup ig ON ig.InspectionRouteID = ir.ID
WHERE ir.Status = 0
AND ir.CompanyID = 43
AND ig.Status = 0
AND ig.ID = i.InspectionGroupID
) InspectionRouteName
WHERE i.Status = 0 AND i.ParentInspectionID IS NULL
AND il.Status = 0
--AND ((i.AssetID IS NOT NULL AND i.AssetID NOT IN (SELECT ID FROM #notPreferredAssetIDsTable)) OR i.AssetID IS NULL)
AND it.Category <> 2
AND c.IsArchived = 0
AND it.AssetTypeID = 3 AND a.AssetTypeID IS NOT NULL AND a.AssetTypeID = 3
AND i.InspectionTypeID IN (3)
AND i.ContractID IN (90,118)
AND (latestStatus.InspectionStatus IN (5,6,1,2,3) OR completedStatus.StatusDate IS NOT NULL OR latestStatus.InspectionStatus IS NULL)
AND ((completedStatus.StatusDateLocal >= '2021-02-01 00:00:00' AND completedStatus.StatusDateLocal <= '2021-02-05 23:59:59') OR (i.PlannedDate >= '2021-02-01 00:00:00' AND i.PlannedDate <= '2021-02-05 23:59:59') OR (i.CreatedDate >= '2021-02-01 00:00:00' AND i.CreatedDate <= '2021-02-05 23:59:59' AND (latestStatus.InspectionStatus not in (6, 5) OR latestStatus.InspectionStatus IS NULL)))
AND ((CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 43) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 1
AND i.ContractID IN (90,118)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 79
AND i.ContractID IN (90)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 80
AND i.ContractID IN (90)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 81
AND i.ContractID IN (90)) OR (CASE WHEN i.AssignedUserID IS NULL THEN CreatedUser.CompanyID ELSE AssignedUser.CompanyID END = 82
AND i.ContractID IN (90)))
ORDER BY ID
OFFSET #pageSize * (#pageNumber - 1) ROWS
FETCH NEXT #pageSize ROWS ONLY
OPTION (RECOMPILE)

Need help to update supervisor in Sql

I have a employee table , from where I am finding out the supervisor(team lead) and the manager details based on the User ID . I have a SSIS package which appends newly created Users into my table.
Below is what i am doing currently .
appending newly created user ID into my Sql table from the master table.
finding the supervisor and the manager , based on the employee USER ID.
Now , when an existing employee changes his manager or the supervisor, how can I update the new changes from the master table to my sql table. I dont want to truncate and load the table either since this table is used in an application.
here is the query:
-- load new employee details
INSERT INTO [dbo].[Active_Directory_COE]
(
[USER_ID]
, [USER_TYPE_NM]
, [USER_CLASS]
, [LAST_NM]
, [FIRST_NM]
, [MIDDLE_INIT]
, [EMAIL_ADDRESS]
, [EMP_JOB_DESC]
, [BUSINESS_UNIT_CD]
, [BUSINESS_UNIT_NM]
, [BUSINESS_SEGMENT]
, [SUPERVISOR_ID]
, [EMP_FULL_NM]
, [MANAGER_NAME]
, [SUPERVISOR_NAME]
, [SUPERVISOR_EMAIL_ADDRESS]
, [MANAGER_EMAIL_ADDRESS]
, [TEAM]
, [REGION]
, [SUB_REGION]
, [SALES_OFFICE]
, [SALES_GROUP]
, [EMPLOYEE_STATUS]
, [SURVEY_GROUP]
, [SURVEY_APPLICABLE]
, [REGION_CSSC]
, [SALES_ORG]
, [SALES_OFFICE_MULTIPLE]
, [TE_JOINING_DT]
)
SELECT
[A1].[USER_ID]
, [A1].[USER_TYPE_NM]
, [A1].[USER_CLASS]
, [A1].[LAST_NM]
, [A1].[FIRST_NM]
, [A1].[MIDDLE_INIT]
, [A1].[EMAIL_ADDRESS]
, [A1].[EMP_JOB_DESC]
, [A1].[BUSINESS_UNIT_CD]
, [A1].[BUSINESS_UNIT_NM]
, [A1].[BUSINESS_SEGMENT]
, [A1].[SUPERVISOR_ID]
, [A1].[EMP_FULL_NM]
, [D].[EMP_FULL_NM] AS [Manager_Name]
, [C].[EMP_FULL_NM] AS [Supervisor_name]
, [C].[EMAIL_ADDRESS] AS [Supr_Email_AD]
, [D].[EMAIL_ADDRESS] AS [Mangaer_Email_Ad]
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, [A1].[SERVICE_CREDIT_DT]
FROM
(
SELECT
[A].[USER_ID]
, [A].[USER_TYPE_NM]
, [A].[USER_CLASS]
, [A].[LAST_NM]
, [A].[FIRST_NM]
, [A].[MIDDLE_INIT]
, [A].[EMAIL_ADDRESS]
, [A].[EMP_JOB_DESC]
, [A].[BUSINESS_UNIT_CD]
, [A].[BUSINESS_UNIT_NM]
, [A].[BUSINESS_SEGMENT]
, [A].[SUPERVISOR_ID]
, [A].[EMP_FULL_NM]
, [A].[SERVICE_CREDIT_DT]
FROM [dbo].[Active_Directory] [A]
LEFT OUTER JOIN [dbo].[Active_Directory_COE] [B]
ON [A].[USER_ID] = [B].[USER_ID]
WHERE [B].[USER_ID] IS NULL
) [A1]
INNER JOIN [dbo].[Active_Directory] [C]
ON [C].[USER_ID] = [A1].[SUPERVISOR_ID]
INNER JOIN [dbo].[Active_Directory] [D]
ON [C].[SUPERVISOR_ID] = [D].[USER_ID];

while selecting the data from tables im getting the error

SQL :
select E.ID
, cp = (
select ISNULL(C.CNAME, E.CP) CP
from TBL_EXPEDITE_MASTER E
left join [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C on C.spid = E.CP
)
, E.ECO_ORDER
, E.DN
, E.CBUK
, E.XMPF_ID
, E.CP
, E.RECEIVED
, E.CP_OWNER
, E.CP_CONTACT_NUMBER
, CP_EMAIL
, E.CPREF
, E.SITE_CONTACT_NAME
, E.SITE_CONTACT_NUMBER
, E.EXISTING_CRD
, E.NEW_CRD
, E.EXPEDITE_CRD
, E.status
, E.LINE
, E.CHARGEABLE
, E.RESPONSE
, LAST_UPDATED
, ORDER_TYPE
, INITIALRESP
, NOTES
, BTW_EXPEDITE_REF --1.2
, ProductType --1.2
, InstallationAddress --1.2
, SITE_CONTACT_OTHER --1.2
, FastTrackReason --1.2
, FastTrackFOC --1.2
, FOC_Reason --1.2
, CP_EMAIL_OTHER --1.2
, Received_Via --1.2
, E2EData --1.2
, E.organisationName
, E.postCode
, E.thoroughfareDescriptor
, E.county
, E.country
from TBL_EXPEDITE_MASTER E with (nolock)
Error Message
1:
You are getting this error because in the First Select the SubQuery you are using has returned more than 1 Record for the same row.
You May try using top 1 to avoid this error or any aggregate functions or Join the subquery as a separate CTE will also work fine.
For a Quick Fix, you can change the query like this
SELECT cp =
(
SELECT TOP 1 ISNULL(C.CNAME, E.CP) CP
FROM TBL_EXPEDITE_MASTER E
LEFT JOIN [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C ON C.spid = E.CP
ORDER BY C.CNAME, E.CP
);
but looking at your query, I think this will also work
SELECT
E.ID,
cp = ISNULL(C.CNAME, E.CP),
E.ECO_ORDER,
E.DN,
E.CBUK,
E.XMPF_ID,
E.CP,
E.RECEIVED,
E.CP_OWNER,
E.CP_CONTACT_NUMBER,
CP_EMAIL,
E.CPREF,
E.SITE_CONTACT_NAME,
E.SITE_CONTACT_NUMBER,
E.EXISTING_CRD,
E.NEW_CRD,
E.EXPEDITE_CRD,
E.STATUS,
E.LINE,
E.CHARGEABLE,
E.RESPONSE,
LAST_UPDATED,
ORDER_TYPE,
INITIALRESP,
NOTES,
BTW_EXPEDITE_REF,
ProductType,
InstallationAddress,
SITE_CONTACT_OTHER,
FastTrackReason,
FastTrackFOC,
FOC_Reason,
CP_EMAIL_OTHER,
Received_Via,
E2EData,
E.organisationName,
E.postCode,
E.thoroughfareDescriptor,
E.county,
E.country
FROM TBL_EXPEDITE_MASTER E WITH (NOLOCK)
LEFT JOIN [NEPTUNE122].BB_PROVISION.DBO.BI_CUSTOMERS C
ON C.spid = E.CP

SSRS “Subquery returned more than 1 value”

I'm using MS Visual Studio 2012. I'm having an issue with writing a subquery. This is the entire query as follows:
SELECT ACCOUNTNUM
, NAME
, ADDRESS
, PHONE
, TELEFAX
, INVOICEACCOUNT
, CUSTGROUP
, PAYMTERMID
, CURRENCY
, DIMENSION
, CELLULARPHONE
, STATISTICSGROUP
, PAYMMODE
, NAMEALIAS
, CONTACTPERSONID
, STREET
, PARTYID
, SEGMENTID
, TAXGROUP
, DATAAREAID
, ISNULL((
SELECT PERCENT1 AS DiscCount
FROM PRICEDISCTABLE
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM)
), 0) / 100 AS DiscCount
, (
SELECT NAME
FROM CONTACTPERSON
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM)
AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID)
) AS ContactName
, (
SELECT PHONE
FROM CONTACTPERSON AS CONTACTPERSON_1
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM)
AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID)
) AS ContactPhone
, ISNULL((
SELECT STATGROUPNAME
FROM CUSTSTATISTICSGROUP
WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
AND (CUSTSTATISTICSGROUP = CUSTTABLE.STATISTICSGROUP)
), 0) AS StatisticsName
FROM CUSTTABLE
WHERE CUSTGROUP = #ty
AND DATAAREAID = N'OTN'
AND STATISTICSGROUP LIKE #ss;
and then I get the error
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
Where the issue comes from?
Issue is that one of your subqueries returns more than one value (HEY, ERROR DESCRIPTION SAYS THAT).
How to find which one?
Try commenting out each subquery in your select statement until your query works fine.
How to fix it?
That's probably the best case to use CROSS APPLY. That'd be your query:
SELECT ACCOUNTNUM
, NAME
, ADDRESS
, PHONE
, TELEFAX
, INVOICEACCOUNT
, CUSTGROUP
, PAYMTERMID
, CURRENCY
, DIMENSION
, CELLULARPHONE
, STATISTICSGROUP
, PAYMMODE
, NAMEALIAS
, CONTACTPERSONID
, STREET
, PARTYID
, SEGMENTID
, TAXGROUP
, DATAAREAID
, ISNULL(T1.DiscCount, 0) / 100 AS DiscCount
, T2.ContactName
, T3.ContactPhone
, ISNULL(T4, 0) AS StatisticsName
FROM CUSTTABLE
CROSS APPLY (
SELECT PERCENT1 AS DiscCount
FROM PRICEDISCTABLE
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM
) AS T1
CROSS APPLY (
SELECT NAME
FROM CONTACTPERSON
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM
AND CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID
) AS T2
CROSS APPLY (
SELECT PHONE
FROM CONTACTPERSON AS CONTACTPERSON_1
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM
AND CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID
) AS T3
CROSS APPLY (
SELECT STATGROUPNAME
FROM CUSTSTATISTICSGROUP
WHERE DATAAREAID = CUSTTABLE.DATAAREAID
AND CUSTSTATISTICSGROUP = CUSTTABLE.STATISTICSGROUP
) AS T4
WHERE CUSTGROUP = #ty
AND DATAAREAID = N'OTN'
AND STATISTICSGROUP LIKE #ss;
Also, these can be rewritten as simple INNER JOINS.
When encountering this error, try running the subqueries individually and if more than one record is returned then refine your conditions to ensure only one record is returned.
Of course, if more than one record is wanted then you need to change your query to use a join instead of a subquery.

Combining NOT EXISTS with INSERT ... SELECT

The following code inserts a new row into a table based on a value in another row but it also allows duplicate data. I want to use NOT EXISTS so that if there is already a row with that value then it won't insert another one, but not sure how to integrate this.
INSERT INTO [Grading].[dbo].[tblObservations]
([FormID]
,[Data]
,[UserID]
,[DateOfObservation]
,[Final]
,[ValidTo]
,[ID_Grading]
,[ID_ObservationKind]
,[Created]
,[Modified]
,[RowVersion])
SELECT [FormID]
,'0'
,[UserID]
,[DateOfObservation]
,[Final]
,[ValidTo]
,[ID_Grading]
,40
,[Created]
,[Modified]
,[RowVersion]
FROM [Grading].[dbo].[tblObservations]
WHERE [ID_ObservationKind] = 39 AND [Data] = 'No' AND [Final] = 1
Any help appreciated.
Try this one -
USE [Grading]
INSERT INTO [dbo].[tblObservations]
(
FormID
, Data
, UserID
, DateOfObservation
, Final
, ValidTo
, ID_Grading
, ID_ObservationKind
, Created
, Modified
, [RowVersion]
)
SELECT
t.FormID
, '0'
, t.UserID
, t.DateOfObservation
, t.Final
, t.ValidTo
, t.ID_Grading
, 40
, t.Created
, t.Modified
, t.[RowVersion]
FROM dbo.tblObservations t
LEFT JOIN dbo.tblObservations t2 ON
t.FormID = t2.FormID
AND t2.Data = '0'
AND t.UserID = t2.UserID
AND t.DateOfObservation = t2.DateOfObservation
AND t.Final = t2.Final
AND t.ValidTo = t2.ValidTo
AND t.ID_Grading = t2.ID_Grading
AND t2.ID_ObservationKind = 40
AND t.Created = t2.Created
AND t.Modified = t2.Modified
AND t.[RowVersion] = t2.[RowVersion]
WHERE t.ID_ObservationKind = 39
AND t.Data = 'No'
AND t.Final = 1
AND t2.FormID IS NULL
Or try this -
INSERT INTO [dbo].[tblObservations]
(
FormID
, Data
, UserID
, DateOfObservation
, Final
, ValidTo
, ID_Grading
, ID_ObservationKind
, Created
, Modified
, [RowVersion]
)
SELECT
t.FormID
, '0'
, t.UserID
, t.DateOfObservation
, t.Final
, t.ValidTo
, t.ID_Grading
, 40
, t.Created
, t.Modified
, t.[RowVersion]
FROM dbo.tblObservations t
WHERE t.ID_ObservationKind = 39
AND t.Data = 'No'
AND t.Final = 1
AND NOT EXISTS(
SELECT 1
FROM dbo.tblObservations t2
WHERE t2.ID_ObservationKind = 39
AND t2.Data = 'No'
AND t2.Final = 1
)
You can try
IF NOT EXISTS
(
SELECT 1
FROM [Grading].[dbo].[tblObservations]
WHERE [ID_ObservationKind] = 39 AND [Data] = 'No' AND [Final] = 1
)
BEGIN
-- Insert script
END
You're looking to do a "merge" or "upsert" in SQL. Depending on your database, there are a number of ways to do this. If you are using MySQL, you'll want to use it's INSERT... ON DUPLICATE KEY UPDATE syntax.

Resources