Custom hook doesn't work when called on button click - sql-server

I'm having trouble on finding what is wrong with this SQL statement. I'm using pyodbc latest and ODBC Driver 17 for SQL Server, here is the statement:
insert_sql = """MERGE VehicleDistanceReport trg
USING (VALUES (?, ?, ?))
src(
Distance,
Ic_Siparis_No,
Vehicle_Hour
)
ON trg.Ic_Siparis_No = src.Ic_Siparis_No
WHEN MATCHED THEN
UPDATE SET
Distance = src.Distance,
Ic_Siparis_No = src.Ic_Siparis_No,
Vehicle_Hour = src.Vehicle_Hour
WHERE trg.NodeGroup LIKE N'%AĞIR%' OR trg.NodeGroup LIKE N'%MAKİNE%'
WHEN NOT MATCHED THEN
INSERT(
id,
Record_No,
Device_No,
License_Plate,
Inı
Distance,
Ic_Siparis_No,
Vehicle_Hour
)
VALUES(
src.Distance,
src.Ic_Siparis_No,
src.Vehicle_Hour
);"""
It says there is a syntax error near WHERE statement but the query seems to work in SQL server.

Your WHEN MATCHED clause is wrong, as the WHERE should be a condition. Also no pooint updating Ic_Siparis_No as it's the joining condition anyway
WHEN MATCHED AND trg.NodeGroup LIKE N'%AĞIR%' OR trg.NodeGroup LIKE N'%MAKİNE%' THEN
UPDATE SET
Distance = src.Distance,
Vehicle_Hour = src.Vehicle_Hour
Don't be tempted to place this condition in the ON clause, it will cause incorrect results.

Related

Merge..USING..WHEN SQL Server equivalent in PostgreSQL

Right now, we are trying to migrate a stored procedure from SQL Server to PostgreSQL. In this, We saw a querying method MERGE..USING..WHEN. So I couldn't find the equivalent. So is there any option to replicate this functionality? The actual query is given below.
WITH phase_list AS (
SELECT #id_plant AS id_plant
, id_phase
, date_started
FROM OPENJSON(#phase_list)
WITH (
id_phase INT '$.id_phase',
date_started DATE '$.date_started'
)
WHERE date_started IS NOT NULL
)
MERGE grow.plant_phase AS t
USING phase_list AS s
ON s.id_plant = t.id_plant AND s.id_phase = t.id_phase
WHEN MATCHED AND (t.date_started <> s.date_started) THEN
UPDATE
SET t.date_started=s.date_started,
t.date_updated=getutcdate(),
t.updated_by=#id_user
WHEN NOT MATCHED BY TARGET THEN
INSERT (id_plant, id_phase, date_started, created_by, updated_by)
VALUES (s.id_plant, s.id_phase, s.date_started, #id_user, #id_user)
WHEN NOT MATCHED BY SOURCE AND t.id_plant = #id_plant THEN
DELETE;
Can we replicate the same using any join operation with some if/else conditions? or any other approach?

Crystal Reports XI - syntax error in query working ok in SQL Server

I have a query, and in SQL Server Management Studio (2008), it is working perfectly, but when I add a subreport to my report when the same query with dynamic params, I get two errors.
The first time the values that I set in the set default values like, for example 'A' is interpreted like a column name and showing
Invalid column name A
When I send a constant 'A' without parameter, no errors are shown.
This is my code:
select top 1
isnull( dtssegcabv.dscv_fecharecepcion, 0) as FechaRecepcion
from
CabVenta
join
dtssegcabv on CabVenta.cvescv_id = dtssegcabv.scv_id
where
CabVenta.cve_letra = {?cve_letra}
and CabVenta.cve_codpvt = {?cve_codpvt}
and CabVenta.cve_nro = {?cve_nro}
and CabVenta.cvetco_cod = {?cvetco_cod}
If I send no default values (let the dialog empty), the error is
Incorrect Syntax Near Keyword And
I repeat, the query in SQL Server is working fine. The datatypes are all varchar.
Thanks for your help and sorry for my bad English.
It was a quotes problem:
select top 1
isnull( dtssegcabv.dscv_fecharecepcion, 0) as FechaRecepcion
from
CabVenta
join
dtssegcabv on CabVenta.cvescv_id = dtssegcabv.scv_id
where
CabVenta.cve_letra = '{?cve_letra}'
and CabVenta.cve_codpvt = '{?cve_codpvt}'
and CabVenta.cve_nro = '{?cve_nro}'
and CabVenta.cvetco_cod = '{?cvetco_cod}'
Works Perfectly

Update field based on Compound select statement

I have a select statement That I need to turn into an Update.
I need to update a particular unix time field to January 1, 2016.
I have to select the records using a compound select statement.
Update archive_queue set archive_time = 1451606400
FROM
select recordings.(star), archive_queue.(star)
from
recordings, archive_queue
where
recordings.device_alias = '70285' and recordings.keepdays = 120
and recordings.ident = archive_queue.rec_ident
The above gives me a syntax error. The select gives me the records that I need to update. I looked at some examples here, but can't figure out the proper syntax based on my needs. Thanks everyone !
You should remove the select and only have one FROM statement:
Update archive_queue set archive_time = 1451606400
from recordings, archive_queue
where recordings.device_alias = '70285' and recordings.keepdays = 120
and recordings.ident = archive_queue.rec_ident

Update on linked server with nested subquery

I want to update on a linked server the result of a query as well from a linked server.
The first sql snippet gives me the value to be updated:
SELECT mmdb_vessel.IMONo, mmdb_vessel.DeathDate
From OPENQUERY(MMDB, 'SELECT FunctionalLocation, IMONo, VesselStatus, CONVERT(VARCHAR(10), DeathDate, 102) AS DeathDate
FROM VESSEL
WHERE VESSEL.VesselStatusID <> 42 AND VESSEL.DeathDate is not null') as mmdb_vessel
, eb_all_v
WHERE
eb_all_v.IMO_No = mmdb_vessel.IMONo
AND eb_all_v.status = 'in service'
the second is actually what I'm not able to implement, it should show what I want to achieve:
UPDATE EPI2..EPI.PLANT
SET KIND_OF_LIQUIDATION_NO = 1
, LIQUIDATION_DATE = [result from snippet above].DeathDate
Where EPI2..EPI.PLANT.IMONo = [result from snippet above].IMONo
I'm not so sure if my explanation is sufficient, please feel free to ask for additional information!
Thanks, already in advance,
Werner
I would recommend to select the data from the remote server first and store the required data e.g. in a temptable, because LinkedServer and updates can have some sideeffects (e.g. performing a tablescan on the remote table, altough you would not expect it if an updaet is involved, etc) - but this depends on your exact usage/scenario.
Select data you need to update
SELECT * INTO #tmpTable FROM LINKEDSERVER.EPI.dbo.PLANT WHERE ....
Perform the update on local server
UPDATE EPI2..EPI.PLANT SET KIND_OF_LIQUIDATION_NO = 1, LIQUIDATION_DATE = t.DeathDate FROM #tmpTable t INNER JOIN EPI2..EPI.PLANT p on t.IMONo = p.IMONo

How to do Sql Server CE table update from another table

I have this sql:
UPDATE JOBMAKE SET WIP_STATUS='10sched1'
WHERE JBT_TYPE IN (SELECT JBT_TYPE FROM JOBVISIT WHERE JVST_ID = 21)
AND JOB_NUMBER IN (SELECT JOB_NUMBER FROM JOBVISIT WHERE JVST_ID = 21)
It works until I turn it into a parameterised query:
UPDATE JOBMAKE SET WIP_STATUS='10sched1'
WHERE JBT_TYPE IN (SELECT JBT_TYPE FROM JOBVISIT WHERE JVST_ID = #jvst_id)
AND JOB_NUMBER IN (SELECT JOB_NUMBER FROM JOBVISIT WHERE JVST_ID = #jvst_id)
Duplicated parameter names are not allowed. [ Parameter name = #jvst_id ]
I tried this (which i think would work in SQL SERVER 2005 - although I haven't tried it):
UPDATE JOBMAKE
SET WIP_STATUS='10sched1'
FROM JOBMAKE JM,JOBVISIT JV
WHERE JM.JOB_NUMBER = JV.JOB_NUMBER
AND JM.JBT_TYPE = JV.JBT_TYPE
AND JV.JVST_ID = 21
There was an error parsing the query. [ Token line number = 3,Token line offset = 1,Token in error = FROM ]
So, I can write dynamic sql instead of using parameters, or I can pass in 2 parameters with the same value, but does someone know how to do this a better way?
Colin
Your second attempt doesn't work because, based on the Books On-Line entry for UPDATE, SQL CE does't allow a FROM clause in an update statement.
I don't have SQL Compact Edition to test it on, but this might work:
UPDATE JOBMAKE
SET WIP_STATUS = '10sched1'
WHERE EXISTS (SELECT 1
FROM JOBVISIT AS JV
WHERE JV.JBT_TYPE = JOBMAKE.JBT_TYPE
AND JV.JOB_NUMBER = JOBMAKE.JOB_NUMBER
AND JV.JVST_ID = #jvst_id
)
It may be that you can alias JOBMAKE as JM to make the query slightly shorter.
EDIT
I'm not 100% sure of the limitations of SQL CE as they relate to the question raised in the comments (how to update a value in JOBMAKE using a value from JOBVISIT). Attempting to refer to the contents of the EXISTS clause in the outer query is unsupported in any SQL dialect I've come across, but there is another method you can try. This is untested but may work, since it looks like SQL CE supports correlated subqueries:
UPDATE JOBMAKE
SET WIP_STATUS = (SELECT JV.RES_CODE
FROM JOBVISIT AS JV
WHERE JV.JBT_TYPE = JOBMAKE.JBT_TYPE
AND JV.JOB_NUMBER = JOBMAKE.JOB_NUMBER
AND JV.JVST_ID = 20
)
There is a limitation, however. This query will fail if more than one row in JOBVISIT is retuned for each row in JOBMAKE.
If this doesn't work (or you cannot straightforwardly limit the inner query to a single row per outer row), it would be possible to carry out a row-by-row update using a cursor.

Resources