Incorrect syntax near the keyword 'INNER'. On Simple SQL query? - sql

I am getting an error on a query which worked for another similar task but in this case does not. All I want to do is copy the values from a column in one table to another:
UPDATE dbo.JobClients
SET JobClients.[Status] = dbo.Jobs.[Status]
INNER JOIN dbo.JobClients
ON dbo.Jobs.Id = dbo.JobClients.JobId
I added the square brackets around the "Status" because it was highlighting blue and I thought it may be a reserved word, but even so the error isn't pointing to that being the problem:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'INNER'.
Any ideas greatly appreciated!

You are missing the FROM clause. Try this instead:
UPDATE c
SET c.[Status] = j.[Status]
FROM dbo.JobClients AS c
INNER JOIN dbo.JobClients AS j ON j.Id = c.JobId

Related

Getting Error when using SQL With Common Table Expression (CTE)

I am new to SQL. I am trying to use SQL CTE but I keep getting the error:
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ')'.
I am using ms-sql and reading the following blog for guide.
This is my query
WITH parents(BranchCode, SOLD,BANKERSCOUNT, [TARGET]) AS
(
SELECT MS.ParentBranchCode,
SUM(NP.SOLD) SOLD,
SUM(NP.BANKERSCOUNT) BANKERSCOUNT,
SUM(NP.[TARGET]) [TARGET]
FROM NEDLLIFEPARTICIPATION NP
INNER JOIN m_Structure MS
ON MS.BranchCode = NP.BranchCode
GROUP BY MS.ParentBranchCode, NP.Year, NP.MONTH, NP.ProductId
)
Does this give you an error? If not then it might be that you simply have not included a select statement following your common table expression. This would explain why your error is showing an issue with the closing bracket, it is just telling you (if my assumption is right) that your CTE is not being used in a query (and therefore will not compile).
WITH parents(BranchCode, SOLD,BANKERSCOUNT, [TARGET]) AS
(
SELECT MS.ParentBranchCode,
SUM(NP.SOLD) SOLD,
SUM(NP.BANKERSCOUNT) BANKERSCOUNT,
SUM(NP.[TARGET]) [TARGET]
FROM NEDLLIFEPARTICIPATION NP
INNER JOIN m_Structure MS
ON MS.BranchCode = NP.BranchCode
GROUP BY MS.ParentBranchCode, NP.Year, NP.MONTH, NP.ProductId
)
select * from parents;

Syntax Error: ON RIGHT when trying to match a substring in Impala

Does anyone know why I am receiving this error? I am using SQL in IMPALA and it wont run. Theres a yellow underline under mem_register_hsty_view and transparency_services_summary_2018.
Here is my code:
use sndbx_dx;
SELECT
r.member_identifier,
n.fst_nme
FROM mem_register_hsty_view n
JOIN transparency_services_summary_2018 r
ON RIGHT(TRIM(r.member_identifier),4) = LEFT(n.fst_nme,4)
ORDER BY
r.id_key,
r.group_number,
n.fst_nme;
Here is the error:
AnalysisException: Syntax error in line 1:undefined: ...ervices_summary_2018 r ON RIGHT(TRIM(r.member_identifi... ^ Encountered: RIGHT Expected: CASE, CAST, DEFAULT, EXISTS, FALSE, IF, INTERVAL, NOT, NULL, REPLACE, TRUNCATE, TRUE, IDENTIFIER CAUSED BY: Exception: Syntax error
From the current Impala documentation the functions for taking some number of characters from the left or right of the string appear to actually be STRLEFT and STRRIGHT, respectively. Apply this to your current query gives:
SELECT
r.member_identifier,
n.fst_nme
FROM mem_register_hsty_view n
INNER JOIN transparency_services_summary_2018 r
ON STRRIGHT(TRIM(r.member_identifier), 4) = STRLEFT(n.fst_nme, 4)
ORDER BY
r.id_key,
r.group_number,
n.fst_nme;

SQL Server : merge using Join on Source Table fails to bind

I am writing a SQL Server Merge statement but can't seem to get the syntax correct. Would someone please take a look to see where I'm going wrong?
Any help you can give is most appreciated.
What I have is two tables that I'd like to merge (w_materialmarketprices2 and d_component). My source (d_component) table requires me to do a join to a tax table (d_tax).
Everything works fine except for when I try to add the additional tax table into my join. The reason I need the tax table is because it contains a tax rate which I don't have in my d_component table (although I do have the corresponding tax code).
My comparison criteria between w_materialmarketprices2 and d_component includes the tax rate in the calculation.
Here's my code:
MERGE [DWH].[dbo].[w_materialmarketprices2] AS A
USING
(SELECT
[comp_code], [comp_desc], [comp_o_un], [comp_type],
[comp_ccy], [comp_tx], [comp_net_price], [comp_per],
[comp_doc_date], [comp_last_update], [comp_latest],
D.[tax_rate] AS TaxRate
FROM
[DWH].[dbo].[d_component]) AS B
INNER JOIN
[DWH].[dbo].[d_tax] AS D ON D.[tax_code] = B.[comp_tx]
ON
A.[mp_comp_code] = B.[comp_code] AND A.[mp_valid_date] = B.[comp_doc_date] AND B.[comp_net_price]>0 AND A.[mp_price_inc_vat] = ROUND(((B.[comp_net_price]/B.[comp_per])*(1+TaxRate),3) AND A.[mp_budget_actual] = 'PO Actual' AND B.[comp_type] ='P100' AND (left(B.[comp_code],1)='S' OR left(B.[comp_code],1)='R')
WHEN NOT MATCHED BY TARGET
THEN
INSERT ([mp_budget_actual], [mp_comp_code], [mp_comp_desc], [mp_unit], [mp_unit_qty], [mp_qualified_supplier], [mp_ccy], [mp_price_inc_vat], [mp_valid_date], [mp_last_update], [mp_latest])
VALUES ('PO Actual', B.[comp_code], B.[comp_desc], B.[comp_o_un], 1, 'Y', B.[comp_ccy], ROUND(((B.[comp_net_price]/B.[comp_per])*(1+TaxRate),3), B.[comp_doc_date], B.[comp_last_update], B.[comp_latest])
;
The error I'm getting is:
Msg 4145, Level 15, State 1, Line 20
An expression of non-boolean type specified in a context where a condition is expected, near ','.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'B'.
,D.[tax_rate] AS TaxRate shows up as underlined in red so I reckon the problem is something to do with that. I also get the message
The multi-part identifier "D.tax_rate" could not be bound
Thanks for your help in advance. Honkonger.
There is no reason to use a subquery in the USING clause, ie: don't put a SELECT in there:
MERGE [DWH].[dbo].[w_materialmarketprices2] AS A
USING
[DWH].[dbo].[d_component] AS B
INNER JOIN [DWH].[dbo].[d_tax] AS D ON D.[tax_code] = B.[comp_tx]
ON
A.[mp_comp_code] = B.[comp_code] .......

SQL JOIN Syntax Error with Multiple Joins

SELECT
*
FROM
[SQL].[dbo].[Debtors] d
JOIN
[SQL].[dbo].[DebtorIndex] di
JOIN
[SQL].[dbo].[DebtorAddresses] da ON d.IDNumber = di.IDNumber
AND d.AutoNumber = da.DebtorID
AND da.DebtorID = '199'
I am getting this error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'da'.
First however there is nothing there. Is there supposed to be something else there?
Second for [SQL].[dbo].[Debtors] d, I know for a fact and have triple checked that d.IDNumber & d.AutoNumber exist but I get the following error for both
"The multi-part identifier "d.IDNumber" could not be bound."
and get the same for d.AutoNumber.
Please help. Thanks in advance
Try it like this:
SELECT *
FROM [SQL].[dbo].[Debtors] d
INNER JOIN [SQL].[dbo].[DebtorIndex] di ON d.IDNumber = di.IDNumber
INNER JOIN [SQL].[dbo].[DebtorAddresses] da ON d.AutoNumber = da.DebtorID
WHERE da.DebtorID = '199'
You don’t have a join condition on di.
... di on d.something = di.anotherthing

SCCM2012 SQL Query has an error

I am creating an SQL query for my SCCM collection but I get that there is an error. It doesn't tell me what the error is and the rule looks ok for me.
Select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client, SMS_R_User.UniqueUserName
FROM SMS_R_System
JOIN SMS_UserMachineRelationship ON SMS_R_System.Name=SMS_UserMachineRelationship.MachineResourceName
JOIN SMS_R_User ON SMS_UserMachineRelationship.UniqueUserName=SMS_R_User.UniqueUserName
Where SMS_R_User.UniqueUserName in (select UniqueUserName from SMS_R_User where UserGroupName = "Domain\\GroupName")
Assuming that you haven't misspelled any column names the only thing that I can see that would throw an error is that maybe "Domain\\GroupName" should be 'Domain\\GroupName'? Double-quotes are normally used as quoted identifiers for objects while single-quotes denote string literals.
With the double quotes you would probably get an error like:
Msg 207, Level 16, State 1, Line ?? Invalid column name
'Domain\GroupName'.
Also, the subquery in the where clause looks unnecessary, this query should be equivalent (if I'm not misreading it):
SELECT
S.ResourceID,
S.ResourceType,
S.Name,
S.SMSUniqueIdentifier,
S.ResourceDomainORWorkgroup,
S.Client,
U.UniqueUserName
FROM SMS_R_System S
JOIN SMS_UserMachineRelationship UMR ON S.Name = UMR.MachineResourceName
JOIN SMS_R_User U ON UMR.UniqueUserName = U.UniqueUserName
WHERE U.UserGroupName = 'Domain\\GroupName'