Incorrect syntax near 'C' [duplicate] - sql

This question already has answers here:
Why can't I use an alias in a DELETE statement?
(2 answers)
Closed 2 years ago.
Below is the section of code that is getting an error. I am not sure why I am getting an error.
Delete from TblProcessCurrency C
where Not Exists (
Select PRCSchedule
from tblProcess P
where P.PrcSchedule = C.PCXSchedule and P.PRCOlsn = C.PCXOlsn and P.PRCRelease = C.PCXRelease
);
and here is the error I am getting
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near 'C'.

You want:
DELETE C
FROM TblProcessCurrency C
WHERE Not Exists...
You can't alias the table that is the target for the DELETE, unless you state you want to DELETE from said alias.

Related

How to fix error in sql "Execution finished with errors. Result: near "(": syntax error"? [duplicate]

This question already has an answer here:
Replacement for Left command in SQLite SQL
(1 answer)
Closed 7 months ago.
How to remove all characters from / to &.
UPDATE bal SET Perevod=substr(Perevod, INSTR(Perevod, '/.+&')-1)
WHERE INSTR(Perevod, '/.+&')>0;
You can use this because I believe there is no LEFT function in SQLite:
UPDATE bal
SET Perevod = substr(Perevod, 1, INSTR(Perevod, '/')-1)
WHERE INSTR(Perevod, '/')>0;
DEMO

MSSQL use result from case statement [duplicate]

This question already has answers here:
getting "invalid column" when trying to use column alias in a query
(3 answers)
Closed 1 year ago.
SELECT .....
'NewReserveSum' =
case
when h.ApprovedCurrencyID = 417 then new.ReserveSumV
else [dbo].[GetCurrencyRate](h.ApprovedCurrencyID, #PresentDate) * new.ReserveSumV
end,
(o.ReserveSummN - NewReserveSum) as 'DifferenceReserveSummN'
FROM ......
I want to use the ** NewReserveSum ** variable in the following columns, but I got an error:
Invalid column name 'NewReserveSum'
How can I do it correctly in MSSQL?
In SQL, = is comparison, not assignment
In a SELECT statement, you can use AS to give a name top an expression column:
SELECT
case
when h.ApprovedCurrencyID = 417 then new.ReserveSumV
else [dbo].[GetCurrencyRate](h.ApprovedCurrencyID, #PresentDate) * new.ReserveSumV
end as NewReserveSum,
...

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] .......

Simple SQL command not properly ended [duplicate]

This question already has answers here:
SQL Oracle LEFT JOIN and SUBQUERY error: ORA-00905: missing keyword
(2 answers)
Closed 9 months ago.
I have the following query:
SELECT *
FROM DELUSR.AGREEMENT AS agreement
WHERE agreement.MASTER_AGGREMENT_ID = 4;
After trying to run it, I get this error:
Error code 933, SQL state 42000:
ORA-00933: SQL command not properly ended
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
I am not sure how to decode this error message. The statement looks fine to me. Any insights into whats wrong with it?
Oracle accepts no AS for table aliases:
SELECT * FROM DELUSR.AGREEMENT agreement WHERE agreement.MASTER_AGGREMENT_ID = 4;

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

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