Convert working SELECT query into UPDATE query - sql-server

In SSMS 2014 I have a working SELECT query and want to convert it to and UPDATE statement to update a field in the main table. I have tried and tried many solutions, each results in a different error.
I've managed to do simple UPDATE SET WHERE queries before, but this is a little more complex using derived tables in the WHERE clause. On Searching I found a JOIN solution but for me it does not like the RIGHT or JOIN and in that solution the SET was the last line and SSMS does not like that either so I getting a little confused now.
This is my working SELECT Statement that selects the correct SKU's:
SELECT
dbo.tblHKProducts.SKU_ID, dbo.tblHKProducts.SKU_Description,
dbo.tblHKProducts.SKU_Scan_Group
FROM
dbo.tblHKProducts
RIGHT OUTER JOIN
(SELECT
tblHKProducts_1.SKU_ID, tblHKProducts_1.SKU_Description,
dbo.ForceScanByDescriptions.FindDescription
FROM
dbo.tblHKProducts AS tblHKProducts_1
RIGHT OUTER JOIN
dbo.ForceScanByDescriptions ON tblHKProducts_1.SKU_Description = dbo.ForceScanByDescriptions.FindDescription
WHERE
(tblHKProducts_1.SKU_Description IS NOT NULL)
) AS derivedtbl_1 ON dbo.tblHKProducts.SKU_ID = derivedtbl_1.SKU_ID
I'm trying to do something like this, SKU_Scan_Group is a field in tblHKProducts:
UPDATE dbo.tblHKProducts
SET SKU_Scan_Group = N'Yes'
RIGHT OUTER JOIN
(SELECT
tblHKProducts_1.SKU_ID, tblHKProducts_1.SKU_Description,
dbo.ForceScanByDescriptions.FindDescription
FROM
dbo.tblHKProducts AS tblHKProducts_1
RIGHT OUTER JOIN
dbo.ForceScanByDescriptions ON tblHKProducts_1.SKU_Description = dbo.ForceScanByDescriptions.FindDescription
WHERE
(tblHKProducts_1.SKU_Description IS NOT NULL)
) AS derivedtbl_1 ON dbo.tblHKProducts.SKU_ID = derivedtbl_1.SKU_ID
Any help would be appreciated!
Thanks
Kev

This is quite simple, actually. This should work:
UPDATE P
SET P.SKU_Scan_Group = N'Yes'
-- SELECT P.SKU_ID, P.SKU_Description, P.SKU_Scan_Group
FROM dbo.tblHKProducts AS P
RIGHT JOIN (
SELECT P1.SKU_ID
, P1.SKU_Description
, FSBD.FindDescription
FROM dbo.tblHKProducts AS P1
RIGHT JOIN dbo.ForceScanByDescriptions AS FSBD
ON P1.SKU_Description = FSBD.FindDescription
WHERE P1.SKU_Description IS NOT NULL
) AS derivedtbl_1
ON P.SKU_ID = derivedtbl_1.SKU_ID;
P.S.
Start using aliases to reference tables your code might become quite hard to maintain later.

Related

Using multiple update or inner join is better for performance

I have the following update statement I use in trigger that have same behavior and both of it is correct but 2nd one have problem if I update both tableA_ID and ValueB at same times. But this not big issue as there is work around but if possible I want to avoid it. So what I want to know is there a big performance between this 2 and it will make me decide which one to go. If 2nd much better in performance than I will use 2nd one. But if the difference not that big I will use 1st one. I not expert in sql server. Anyone can help?
UPDATE tableA
SET tableA.Value = tableA.Value - old.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM deleted
) old ON old.tableA_ID = tableA.ID
UPDATE tableA
SET tableA.Value = tableA.Value + new.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM inserted
) new ON new.tableA_ID = tableA.ID
UPDATE tableA
SET tableA.Value = tableA.Value - old.ValueB + new.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM deleted
) old ON old.tableA_ID = tableA.ID
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM inserted
) new ON new.tableA_ID = tableA.ID
As with anything, test it to be sure.
Whether or not you should be doing this in a trigger is a long discussion.
Performance-wise, you're going to see better performance, in most circumstances, with the second query. The reason I can simply state that is that you're only looking at a single lookup for the UPDATE in the query where you update everything all at once. Whereas, the first query has to find the data to UPDATE, two times.
However, again, test this.
And yes, as #SMor says in the comments, why you need this as a trigger should absolutely be discussed. Triggers can be seriously misused and abused.

Update with Inner join or subquery - multiple relations

I need and update on Table exp_pedcom on cases where a field of another table are null ( EXP_Procimp), but these two table do not have a direct relation to each other, so I actully need to "route" it though two other tables in order to relate them. The "routed! relation works, it returns as expected, but the update doesn't. I've tryed both as a direct inner join using from and as a subquery ( which is actually easier to demonstrate, so That's what goes here.
UPDATE EXP_PEDCOM
SET STATUS_ATENDIMENTO=0
WHERE EXP_PROCIMP.DAT_RECEBIMENTO IS NOT NULL IN (SELECT EXP_PROCIMP.DAT_RECEBIMENTO
FROM
EXP_PEDCOM INNER JOIN
EXP_INVOICE_IT ON EXP_PEDCOM.NUM_PEDCOM = EXP_INVOICE_IT.NUM_PEDCOM INNER JOIN
EXP_INVOICE ON EXP_INVOICE_IT.COD_DOCUMENTO = EXP_INVOICE.COD_DOCUMENTO INNER JOIN
EXP_PROCIMP ON EXP_INVOICE.NUM_PROCIMP = EXP_PROCIMP.NUM_PROCIMP)
Any tips?
The following shows how to use it using the UPDATE... FROM... syntax. (I messed with formatting and aliases while working out what the query actually did.)
UPDATE EXP_PEDCOM
set STATUS_ATENDIMENTO = 0
from EXP_PEDCOM
inner join EXP_INVOICE_IT it
on EXP_PEDCOM.NUM_PEDCOM = it.NUM_PEDCOM
inner join EXP_INVOICE iv
on it.COD_DOCUMENTO = iv.COD_DOCUMENTO
inner join EXP_PROCIMP pr
on iv.NUM_PROCIMP = pr.NUM_PROCIMP
where pr.DAT_RECEBIMENTO is not null
This will work [caveat: I could not run and so debug possible syntax glitches], but it's hard to explain why/how this works. It's also a tad unconventional; hopefully you'll get other posts that show the "proper" way to run such queries.

left join not showing null values

I need to find the items that exist in table A but not in table B. Now that would be really simple in MySQL doing a join like this
select * from A
left join B on A.key=B.key
where B.key is null
However for some reason this is not working in MSSQL. I have created the query without the where clause to see all the results and I only see matches, not null values. Do you have any idea why this is not working?
I know I can alternatively use "when not exists" but I want to know the reason as to why with a join is not working.
I am adding the code for your review
select Absences.CustomerID, b.*
from (
select * from openquery(JUAN_INTERFACE,'select cmp_wwn from Planet_Customers where i_outcome =4')) b
left join Absences on Absences.CustomerID = b.cmp_wwn
where Absences.Type = 3223
Your where clause is filtering out null values:
where Absences.Type = 3223
You are left-joining from the openquery subquery to Absences; and then filtering only rows that have a specific (non-null) value in an Absences column.
Did you mean to join the other way around, from Absenses to openquery?

Is it possible to perform a join in Access on a second column if the first is blank?

I have this ugly source data with two columns, let's call them EmpID and SomeCode. Generally EmpID maps to the EmployeeListing table. But sometimes, people are entering the Employee IDs in the SomeCode field.
The person previously running this report in Excel 'solved' this problem by performing multiple vlookups with if statements, as well as running some manual checks to ensure results were accurate. As I'm moving these files to Access I am not sure how best to handle this scenario.
Ideally, I'm hoping to tell my queries to do a Left Join on SomeCode if EmpID is null, otherwise Left Join on EmpID
Unfortunately, there's no way for me to force validation or anything of the sort in the source data.
Here's the full SQL query I'm working on:
SELECT DDATransMaster.Fulfillment,
DDATransMaster.ConfirmationNumber,
DDATransMaster.PromotionCode,
DDATransMaster.DirectSellerNumber,
NZ([DDATransMaster]![DirectSellerNumber],[DDATransMaster]![PromotionCode]) AS EmpJoin,
EmployeeLookup.ID AS EmpLookup,
FROM FROM DDATransMaster
LEFT JOIN EmployeeLookup ON NZ([DDATransMaster]![DirectSellerNumber],[DDATransMaster]![PromotionCode]) = EmployeeLookup.[Employee #])
You can create a query like this:
SELECT
IIf(EmpID Is Null, SomeCode, EmpID) AS join_field,
field2,
etc
FROM YourTable
Or if the query will always be used within an Access session, Nz is more concise.
SELECT
Nz(EmpID, SomeCode) AS join_field,
field2,
etc
FROM YourTable
When you join that query to your other table, the Access query designer can represent the join between join_field and some matching field in the other table. If you were to attempt the IIf or Nz as part of the join's ON clause, the query designer can't display the join correctly in Design View --- it could still work, but may not be as convenient if you're new to Access SQL.
See whether this SQL gives you what you want.
SELECT
dda.Fulfillment,
dda.ConfirmationNumber,
dda.PromotionCode,
dda.DirectSellerNumber,
NZ(dda.DirectSellerNumber,dda.PromotionCode) AS EmpJoin,
el.ID AS EmpLookup
FROM
DDATransMaster AS dda
LEFT JOIN EmployeeLookup AS el
ON NZ(dda.DirectSellerNumber,dda.PromotionCode) = el.[Employee #])
But I would use the Nz part in a subquery.
SELECT
sub.Fulfillment,
sub.ConfirmationNumber,
sub.PromotionCode,
sub.DirectSellerNumber,
sub.EmpJoin,
el.ID AS EmpLookup
FROM
(
SELECT
Fulfillment,
ConfirmationNumber,
PromotionCode,
DirectSellerNumber,
NZ(DirectSellerNumber,PromotionCode) AS EmpJoin
FROM DDATransMaster
) AS sub
LEFT JOIN EmployeeLookup AS el
ON sub.EmpJoin = el.[Employee #])
What about:
LEFT JOIN EmployeeListing ON NZ(EmpID, SomeCode)
as your join, nz() uses the second parameter if the first is null, I'm not 100% sure this sort of join works in access. Worth 20 seconds to try though.
Hope it works.
You Could use a Union:
SELECT DDATransMaster.Fulfillment,
DDATransMaster.ConfirmationNumber,
DDATransMaster.PromotionCode,
DDATransMaster.DirectSellerNumber,
EmployeeLookup.ID AS EmpLookup
FROM DDATransMaster
LEFT JOIN EmployeeLookup ON
DDATransMaster.DirectSellerNumber = EmployeeLookup.[Employee #]
where DDATransMaster.DirectSellerNumber IS NOT NULL
Union
SELECT DDATransMaster.Fulfillment,
DDATransMaster.ConfirmationNumber,
DDATransMaster.PromotionCode,
DDATransMaster.DirectSellerNumber,
EmployeeLookup.ID AS EmpLookup
FROM DDATransMaster
LEFT JOIN EmployeeLookup ON
DDATransMaster.PromotionCode = EmployeeLookup.[Employee #]
where DDATransMaster.DirectSellerNumber IS NULL;

Having trouble converting SQL query to Access

I have this query which works fine in SQL Server, but not in Access, and I'm having trouble converting it. I've always heard that JET is missing some TSQL features, and I suppose complex joins is one of them.
SELECT C.[Position], TT.[Description] as TrainingType, T.ProgramTitle, T.ProgramSubTitle, T.ProgramCode, ET.CompletedDate
from HR_Curriculum C
LEFT JOIN HR_Trainings T ON C.TrainingID = T.TrainingID
LEFT JOIN HR_TrainingTypes TT ON T.TrainingTypeID = TT.TrainingTypeID
LEFT JOIN HR_EmployeeTrainings ET ON C.TrainingID = ET.TrainingID
AND (ET.AvantiRecID IS NULL OR ET.AvantiRecID = '637')
where ( c.[Position] = 'Customer Service Representative'
OR C.[Position] = 'All Employees')
order by [Position], Description, ProgramTitle
I tried putting the extra join clause down in the WHERE clause, but for some reason this does not yield the proper count of records.
When you have more than one JOIN in ms-access you need to wrap them with parenthesis like this:
SELECT C.[Position], TT.[Description] as TrainingType, T.ProgramTitle, T.ProgramSubTitle, T.ProgramCode, ET.CompletedDate
from (((HR_Curriculum C
LEFT JOIN HR_Trainings T ON C.TrainingID = T.TrainingID)
LEFT JOIN HR_TrainingTypes TT ON T.TrainingTypeID = TT.TrainingTypeID)
LEFT JOIN HR_EmployeeTrainings ET ON C.TrainingID = ET.TrainingID
AND (ET.AvantiRecID IS NULL OR ET.AvantiRecID = '637'))
where ( c.[Position] = 'Customer Service Representative'
OR C.[Position] = 'All Employees')
order by [Position], Description, ProgramTitle
or you will have the Missing Operator error
Check that your table alias are delacred with 'as'. Access doesn't like [tablename] [alias], instead try [tablename] as [alias]. I know the complex left joins shouldn't be a problem, but Access might be choking an the alias delcarations if it's returning some join error. I would also try quering out the restriction on the ET table, and then joining that to the larger query. I've noticed that trying to put restrictions on records involved in left or right joins will often not produce the correct records as Access will limit the set after the join.

Resources