I am trying to insert some date into another table. At first I´ve tried to use sqlalchemy to create such queries, but as I got some error when executing, I tried to solve it through raw SQL, but the error still the same.
I am not very used to CTE commands, so I don´t know if there are some restrinctions over them.
WITH Conv_Pre_Pagos AS
(SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente) SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN
The sentence is bigger, but I removed some data to bring it cleaner. Still, the same error:
ibm_db_dbi::ProgrammingError: SQLNumResultCols failed: [IBM][CLI Driver][DB2] SQL0199N The use of the reserved word "INSERT" following "INSERT" is not valid.
Expected tokens may include: "(SELECT ,". SQLSTATE=42601 SQLCODE=-199
[SQL: WITH Conv_Pre_Pagos AS (SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO WHERE CONVENIO.COD_ESPC = 52)
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN]
(Background on this error at: https://sqlalche.me/e/14/f405)"
Where does it see an "insert following insert"?
Try this:
INSERT INTO DB2I023A.ANL_TARF_PAGAS_PREPAGO (convenio, convenente)
WITH Conv_Pre_Pagos AS
(
SELECT CONVENIO.COD_IDEN, CONVENIO.D_CLIENTE_NOM
FROM db2rpc.CONVENIO
WHERE CONVENIO.COD_ESPC = 52
)
SELECT CBR_TARF_REC.NR_DOC_SIS_OGM, Conv_Pre_Pagos.D_CLIENTE_NOM
FROM DB2TFA.CBR_TARF_REC
JOIN Conv_Pre_Pagos ON CBR_TARF_REC.NR_DOC_SIS_OGM = Conv_Pre_Pagos.COD_IDEN
I am creating a new table into database. My code is below
CREATE TABLE Master_data AS
select a.*, COALESCE(b.content_view,0) as content_view, COALESCE(b.headline_view,0) as headline_view
FROM [Britannia_MMx_2021].[dbo].[Sales_20210323] a
left join [Britannia_MMx_2021].[dbo].[Doximity_20220328] b on a.NPI = b.NPI
and a.Year_Mon = b.Date;
I am getting this error
Msg 102, Level 15, State 1, Line 46
Incorrect syntax near '('.
Your SQL snippet displays characteristics of TSQL. In TSQL (e.g. in MS SQL Server) you do not use "create table as" followed by a select statement, instead you use an INTO clause e.g.
SELECT a.*
, COALESCE(b.content_view, 0) AS content_view
, COALESCE(b.headline_view, 0) AS headline_view
INTO Master_data
FROM [Britannia_MMx_2021].[dbo].[Sales_20210323] a
LEFT JOIN [Britannia_MMx_2021].[dbo].[Doximity_20220328] b ON a.NPI = b.NPI
AND a.Year_Mon = b.DATE;
In MySQL you do use CREATE TABLE [ IF NOT EXISTS ] new_table [ AS ]
I need help with my SYNTAX error
I try to add trigger into mysql by still have syntax error.
I will be glad for any help
When I try add this trigger i still have error 1064
Full error response is: Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 19 MySQL version is 5.7
CREATE TRIGGER delete_przejazdy_logger AFTER DELETE ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 1, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
OLD.data, OLD.osob, OLD.fotelik, OLD.detale, OLD.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON OLD.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON OLD.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON OLD.id_adres_z = adresy_z.id
WHERE pasazerowie.id = OLD.id_pasazera);
END;
I add triggers on this same db for logs on creating records and works fine.
Here is the code which works good:
CREATE TRIGGER add_przejazdy_logger AFTER INSERT ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do,
data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 2, CURRENT_DATE, SUBSTRING_INDEX(USER(), '#', 1),
pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
NEW.data, NEW.osob, NEW.fotelik, NEW.detale, NEW.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON NEW.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON NEW.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON NEW.id_adres_z = adresy_z.id
WHERE pasazerowie.id = NEW.id_pasazera);
END;
I'm using NodeJS v8.7.0 with the promise-mysql module, alongside mysql v14.14 Distrib 5.5.57.
I um unable to insert into the temporary table I create. On online SQL validators, as well as my project, I receive the error around the INSERT command.
Whilst I can personally see no issues, I'd love to see a fresh perspective of things to spot the issue.
My whole query string:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;DROP TEMPORARY TABLE QueryBuilder;
Query line-by-line:
CREATE TEMPORARY TABLE QueryBuilder LIKE ItemStore;
INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1, 1), (3427, -1, 1, 1), (3428, -1, 1, 1);
UPDATE ItemStore AS I JOIN QueryBuilder AS Q ON I.AssetID = Q.AssetID SET I.Owner = Q.Owner, I.GameMode = Q.GameMode, I.GameID = Q.GameID;
DROP TEMPORARY TABLE QueryBuilder;
Extra info:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'INSERT INTO QueryBuilder (AssetID, Owner, GameMode, GameID) VALUES (3426, -1, 1,\' at line 1'
errno: 1064
An SQLFiddle demonstrating why I made this query
Thanks for reading my issue, I appreciate all feedback!
I'm trying a MySQL Insert Query with mix of static & Dynamic Values. The INSERT command is.
INSERT INTO ebdb.requestaction(RequestID,
ActionID,
TransactionID,
IsActive,
IsComplete)
VALUES (
1,
**Dynamic Value from Below Query,
Dynamic Value from Below Query,**
1,
0);
The Query to fetch the field 2 & 3 come from the below Query.
SELECT transitionaction.TransitionID, transitionaction.ActionID
FROM transitionaction
INNER JOIN transition
ON transitionaction.TransitionID = transition.TransitionID
WHERE transition.TenantID = 1
AND transition.ProcessID = 1
AND transition.CurrentStateID = 1
ORDER BY transitionaction.TransitionID;
I'm doing something wrong in here.
Please guide me as to how this can be achieved in the most optimized way.
You can select static values as part of a query, e.g.:
INSERT INTO ebdb.requestaction(RequestID, ActionID, TransactionID, IsActive, IsComplete)
SELECT 1, transitionaction.ActionID, transitionaction.TransitionID, 1, 0
FROM transitionaction
INNER JOIN transition
ON transitionaction.TransitionID = transition.TransitionID
WHERE transition.TenantID = 1
AND transition.ProcessID = 1
AND transition.CurrentStateID = 1
ORDER BY transitionaction.TransitionID;
For more information, refer to MySQL's Insert...Select Syntax