SQL Update statement not working - SQL Server - sql

I am trying to run the below sql statement (SQL Server), however getting the error
"FROM clause in UPDATE and DELETE statements cannot contain subquery sources or joins."
update fp
set fp.totalcapacity = hc.totalcapacity,
fp.sellablecapacity = hc.sellablecapacity
from [fact].[FinalPosition] fp
join fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey
and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18'
I can't seem to understand why I am getting this error. Any idea?

I think the syntax you want is:
update fp
set totalcapacity = hc.totalcapacity,
sellablecapacity = hc.sellablecapacity
from fp join
fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18';
If you want fp to refer to an actual table, include that in the from clause and make the fp the alias for the table.

Related

Can't figure out how to add current date to comment

I'm having problems with my sql code, i'm trying to implement a current date to my comment but i can't figure out how + i'm having syntax errors and i don't know what to do anymore. Can someone help me with the date adding to comment?
UPDATE Osalus_projektis AS op
SET op.töötasu = op.töötasu + 100,
op.comment = CONCAT(NOW()'tõusis palk 100 eurot')
FROM Osalus_projektis
INNER JOIN Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
WHERE ol.nimetus = 'nõustaja';
MS Access uses & for string concatenation. And it doesn't support a FROM clause. This may do what you want:
UPDATE Osalus_projektis AS op INNER JOIN
Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
SET op.töötasu = op.töötasu + 100,
op.comment = NOW() & 'tõusis palk 100 eurot'
WHERE ol.nimetus = 'nõustaja';
If there is a problem with the JOIN -- which happens a lot in MS Access -- then you can use an EXISTS clause as well.

MS Access using forms in a pass-through query

I have a form named FietsAantDagen, a query named QueryFietsAantDagen and a textbox named Txtinput. I am trying to use a pass-through query to SQL Server and use a text form's input as a input in my query.
Query:
SELECT
Fiets_id,
Fiets_Type,
SUM(DATEDIFF(DAY, Huurovereenkomst_Begin_datum, Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
Fiets
INNER JOIN
HuurovereenkomstFiets
ON HuurovereenkomstFiets_Fiets_id = Fiets_id
INNER JOIN
Huurovereenkomst
ON Huurovereenkomst_id = HuurovereenkomstFiets_Huurovereenkomst_id
WHERE
YEAR(Huurovereenkomst_Begin_datum) = [Forms]![FietsAantDagen]![Txtinput]
AND YEAR(Huurovereenkomst_Eind_datum) = [Forms]![FietsAantDagen]![Txtinput]
GROUP BY
Fiets_id,
Fiets_Type
While running this query as a pass-through query I get the following error:
ODBC: Runtime error [Microsoft][SQL Server Native Client 11.0][SQL
Server]Incorrect syntax near the keyword 'WHERE'/ (#156)
Is the problem that I am using an Access text form value in a pass-through query, if so what can I do to solve it?
I read in another Overflow question you needed to add (), which I did and now I get the error:
JOIN-expression is not supported.
I'm going crazy...
If you don’t have direct use of SQL server, then likely best to create two pass though queries.
Query #1 – this is your raw SQL as you have
Eg:
SELECT
Fiets_id,
Fiets_Type,
SUM(DATEDIFF(DAY, Huurovereenkomst_Begin_datum,
Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
Fiets
INNER JOIN
HuurovereenkomstFiets
ON HuurovereenkomstFiets_Fiets_id = Fiets_id
INNER JOIN
Huurovereenkomst
ON Huurovereenkomst_id = HuurovereenkomstFiets_Huurovereenkomst_id
WHERE
YEAR(Huurovereenkomst_Begin_datum) = [StartYear]
AND YEAR(Huurovereenkomst_Eind_datum) = [EndYear]
GROUP BY
Fiets_id,
Fiets_Type
Query #2 – this is an application wide query you make that you can re-use over and over for any raw t-SQL (SQL server pass though). You then in code go like this:
Dim rst As DAO.Recordset
Dim strSQL As String
strSQL = CurrentDb.QueryDefs("MyQ1").SQL
srtSQL = Replace(strSQL, "[YearStart]", [Forms]![FietsAantDagen]![Txtinput])
strSQL = Replace(strSQL, "[YearEnd]", [Forms]![FietsAantDagen]![Txtinput])
With CurrentDb.QueryDefs("qryPassR")
.SQL = strSQL
.ReturnsRecords = True
Set rst = .OpenRecordset
End With
However, if you have the ability to use SQL server, and create a proc, then I would suggest you create store procedure like this:
CREATE PROCEDURE SelectDates
#StartYear int,
#EndYear int
AS
BEGIN
SET NOCOUNT ON;
SELECT
Fiets_id,
Fiets_Type,
SUM(DATEDIFF(DAY, Huurovereenkomst_Begin_datum, Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
Fiets
INNER JOIN
HuurovereenkomstFiets
ON HuurovereenkomstFiets_Fiets_id = Fiets_id
INNER JOIN
Huurovereenkomst
ON Huurovereenkomst_id = HuurovereenkomstFiets_Huurovereenkomst_id
WHERE
YEAR(Huurovereenkomst_Begin_datum) = #StartYear
AND YEAR(Huurovereenkomst_Eind_datum) = #EndYear
GROUP BY
Fiets_id,
Fiets_Type
END
Then in access, you use this:
Dim rst As DAO.Recordset
With CurrentDb.QueryDefs("qryPassR")
.SQL = "exec SelectDates " & [Forms]![FietsAantDagen]![Txtinput] & "," & _
[Forms]![FietsAantDagen]![Txtinput]
.ReturnsRecords = True
Set rst = .OpenRecordset
End With
So by passing parameters you reduce most of issues in regards to SQL injection.
Of course if you can’t create store procs, or don’t have permissions, then you have to adopt the first idea above. You can also of course in the first suggestion insert the raw SQL into the code editor, but I find using up an extra query to “just” hold the raw SQL, and then modifying the SQL into the 2nd pass-through query eliminates the need for messy SQL in the VBA code editor.
When you run a Pass-Through query, its SQL text gets sent to the DB server as-is, nothing is evaluated. So including [Forms]![FietsAantDagen]![Txtinput] in a Pass-Through query will never work.
You're on the right track with
SELECT * FROM fnFietsAantDagenPerJaar([Forms]![FietsAantDagen]![Txtinput])‌​
but you need to supply the parameter yourself, because (see above).
So you need to build the Pass-Through SQL, e.g. with this VBA procedure:
Private Sub txtInput_AfterUpdate()
Dim QD As DAO.QueryDef
Dim lYear As Long
' Make sure we pass a number to the SQL Server function
lYear = Val(Me!txtInput)
If lYear > 0 Then
Set QD = CurrentDb.QueryDefs("QueryFietsAantDagen")
QD.SQL = "SELECT * FROM fnFietsAantDagenPerJaar(" & lYear & ")"
Set QD = Nothing
' After changing the query SQL, requery the form
' This assumes that your form is bound to Pass-Through query QueryFietsAantDagen
Me.Requery
End If
End Sub
Pass-through queries run exclusively on the connected, external database engine, here being SQL Server, and does not see anything in MS Access, only everything in that external environment.
Consider adjusting pass-through query to avoid any MS Access evaluations and add year groupings to aggregate query. Then query this resultset in a local Access query filtering for needed year conditions using form values. Below adds table aliases for clarity (adjust if not correct).
Pass-Through (no WHERE clause, but extended SELECT and GROUP BY)
SELECT
f.Fiets_id,
f.Fiets_Type,
YEAR(h.Huurovereenkomst_Begin_datum) As BeginYear,
YEAR(h.Huurovereenkomst_Eind_datum) As EndYear,
SUM(DATEDIFF('d', h.Huurovereenkomst_Begin_datum,
h.Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
HuurovereenkomstFiets_LinkTable hf
INNER JOIN
Fiets_LinkTable f
ON hf.HuurovereenkomstFiets_Fiets_id = f.Fiets_id
INNER JOIN
Huurovereenkomst_LinkTable h
ON h.Huurovereenkomst_id = hf.HuurovereenkomstFiets_Huurovereenkomst_id
GROUP BY
f.Fiets_id,
f.Fiets_Type,
YEAR(h.Huurovereenkomst_Begin_datum),
YEAR(h.Huurovereenkomst_Eind_datum)
Local Query
SELECT
t.Fiets_id,
t.Fiets_Type,
t.AantalDagen
FROM
myPassThroughQuery t
WHERE t.BeginYear = [Forms]![FietsAantDagen]![Txtinput]
AND t.EndYear = [Forms]![FietsAantDagen]![Txtinput]
Alternatively, consider using linked tables from SQL Server (with same ODBC connection as pass-through) where Access can treat them as local tables. Hence, you can run your query using form date values. Do note you will have to convert syntax to adhere to Access' SQL dialect like DATEDIFF and JOIN parentheses. Performance may differ due to changed engines but test.
Linked Tables Local Query
SELECT
f.Fiets_id,
f.Fiets_Type,
SUM(DATEDIFF('d', h.Huurovereenkomst_Begin_datum,
h.Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
(HuurovereenkomstFiets_LinkTable hf
INNER JOIN
Fiets_LinkTable f
ON hf.HuurovereenkomstFiets_Fiets_id = f.Fiets_id)
INNER JOIN
Huurovereenkomst_LinkTable h
ON h.Huurovereenkomst_id = hf.HuurovereenkomstFiets_Huurovereenkomst_id
WHERE YEAR(h.Huurovereenkomst_Begin_datum) = [Forms]![FietsAantDagen]![Txtinput]
AND YEAR(h.Huurovereenkomst_Eind_datum) = [Forms]![FietsAantDagen]![Txtinput]
GROUP BY
f.Fiets_id,
f.Fiets_Type
To create linked tables automatically, use DoCmd.TransferDatabase. Otherwise, use the External Data / ODBC Database icon on ribbon and walk through setup wizard.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DRIVER={SQL Server};Server=<ServerAddress>;Database=<DBname>;Trusted_Connection=Yes;", _
acTable, "HuurovereenkomstFiets", "HuurovereenkomstFiets_LinkedTable"

Updating a Microsoft Access 2013 table with data from a pass-through query

I am trying, unsuccessfully so far, to update records in a Microsoft Access 2013 table (called tbl_Data) with data from an AS400 table (LIBRARY.TABLE).
As you can see in my Access 2013 pass-through query below, I am trying to join the access table with the AS400 table using the Prefix & Number fields, and from there, update the access table with Name & Address information from the AS400 table.
Here is my latest attempt:
UPDATE
tbl_Data
SET
tbl_Data.FirstName = a.NINMFR,
tbl_Data.MiddleName = a.NINMMD,
tbl_Data.LastName = a.NINAML,
tbl_Data.BuildingNumber = a.NIBLNR,
tbl_Data.StreetName = a.NISTNM,
tbl_Data.AptSuite = a."NIAPT#",
tbl_Data.Address2 = a.NIADR2,
tbl_Data.City = a.NICITY,
tbl_Data.State = a.NISTAT,
tbl_Data.ZipCode = a.NIZIPC
INNER JOIN
LIBRARY.TABLE a
ON
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR;
When I run this query, I get an error that says:
OBDC--call failed.
[IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0199 - Keyword INNER not expected. Valid tokens: USE SKIP WAIT WITH WHERE. (#-199)
I would really appreciate any assistance, as I'm out of ideas.
Thanks!
That is Microsoft specific syntax for an update, it does not work on DB2. Try this:
UPDATE
tbl_Data
SET
(tbl_Data.FirstName,
tbl_Data.MiddleName,
tbl_Data.LastName,
tbl_Data.BuildingNumber,
tbl_Data.StreetName,
tbl_Data.AptSuite,
tbl_Data.Address2,
tbl_Data.City,
tbl_Data.State,
tbl_Data.ZipCode)
=
(SELECT
a.NINMFR,
a.NINMMD,
a.NINAML,
a.NIBLNR,
a.NISTNM,
a."NIAPT#",
a.NIADR2,
a.NICITY,
a.NISTAT,
a.NIZIPC
FROM
library.table a
WHERE
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR)
WHERE
EXISTS (
SELECT *
FROM
library.table a
WHERE
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR);

I get an error 3075 (Missing operator) when trying to run an sql. update query

The VBA code is:
''sql1 = "select InvoiceCNotes.[docsetamount] ,InvoiceCNotes.[docsetamount] + AllocationsTEMP.[paidamount] from AllocationsTEMP inner join InvoiceCNotes on AllocationsTEMP.Docnumber = InvoiceCNotes.Docnumber where InvoiceCNotes.[docnumber] = AllocationsTEMP.[docnumber] AND AllocationsTEMP.[paidamount] <> 0"
sqlline = sql1
DoCmd.RunSQL sql1
The code as displayed in sqlline above, is as follows:
Update InvoiceCNotes
set InvoiceCNotes.[docsetamount] = InvoiceCNotes.[docsetamount] + AllocationsTEMP.[paidamount]
from AllocationsTEMP inner join InvoiceCNotes
on AllocationsTEMP.Docnumber = InvoiceCNotes.Docnumber
where InvoiceCNotes.[docnumber] = AllocationsTEMP.[docnumber]
AND AllocationsTEMP.[paidamount] <> 0
I have looked at other questions here regarding the same error, but still I am missing something.
From previous questions, I added the table names, and bracketed the field names.
I checked the table specs to see that docsetamount and paidamount are both defined as [NUMBER,double,fixed,2], the two docnumbers are both long integers, and paidamount is also NUMBER,double,fixed,2
Now I am possibly staring into the problem and not noticing my error, as I have developed quite a few apps in Access over the past five years (since retirement I should add) so I must have done something wrong.
Do you notice the mistake?
The UPDATE t1 SET t1.f=foo FROM t1 JOIN t2 syntax with the FROM is from Sql Server.
In Access SQL you put the JOIN in the first clause, like this:
UPDATE InvoiceCNotes INNER JOIN AllocationsTEMP
ON InvoiceCNotes.Docnumber = AllocationsTEMP.Docnumber
SET InvoiceCNotes.[docsetamount] = InvoiceCNotes.[docsetamount] + AllocationsTEMP.[paidamount]
WHERE AllocationsTEMP.[paidamount] <> 0
where InvoiceCNotes.[docnumber] = AllocationsTEMP.[docnumber] is superfluous because it's already in the JOIN condition.
I found a totally different syntax - and it works just fine.
Maybe my original source was wrong, as I did exactly hat it said.
My new syntax for the update is:
UPDATE InvoiceCNotes INNER JOIN AllocationsTEMP
ON InvoiceCNotes.Docnumber = AllocationsTEMP.Docnumber
SET InvoiceCNotes.docsetamount = InvoiceCNotes.[docsetamount]+AllocationsTEMP.[paidamount]
WHERE (((InvoiceCNotes.docnumber)=[AllocationsTEMP].[docnumber])
AND ((AllocationsTEMP.paidamount)<>False))
Maybe this question should just be removed - it will cause confusion rather than to be helpful. I can't do that, or I would.
Apologies to whoever might have tried to help - and thanks for the effort.
This is now SOLVED

SQL update syntax with subquery

I have this update statement:
UPDATE quotedetailextensionbase qd
SET qd.new_capvehicleid = (SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype)
I get an error back but I'm not sure why
Incorrect syntax near 'qd'
Seems like it would be easier to achieve this result with an update-join statement:
UPDATE qd
SET new_capvehicleid = cv.new_capvehicleid
FROM quotedetailextensionbase qd
JOIN (SELECT new_capvehicleid
FROM vwCapidLookup) cv ON cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype
SQL Server lets you do joined updates:
UPDATE
qd
SET
new_capvehicleid = cv.new_capvehicleid
FROM
quotedetailextensionbase qd
join vwCapidLookup cv on
cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype
;
Does that make sense?
Please try the below SQL:
UPDATE quotedetailextensionbase
SET new_capvehicleid =
(SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype)
Use MERGE: https://msdn.microsoft.com/en-us/library/bb510625.aspx. That is correct when you would update from another table.