How to do an insert with blocks of selects? - mysql

What i'm doing is a simple insert using a select statement to retrieve data from other tables, and here is the problem, i want to do that insert but with multiple inserts trying to imitate a insert with block insertion in fact, if i run the code below it works but just for one row:
INSERT INTO factmonitoreo (idEstacion, IdVariable, IdTiempoRegistro, Valor, ValorOriginal, FlagRegistro)
(SELECT dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:29', 0, 0, 0
FROM dimestacion AS dt
INNER JOIN dimtipoestacion AS dte ON dt.IdTipoEstacion = dte.IdTipoEstacion
JOIN dimvariable AS dv
WHERE dt.Estacion LIKE 'El Lago'
AND dte.CodTipoEstacion LIKE 'ECT'
AND dv.ColumnNameToManualUpdateFromCSV LIKE 'Temperatura')
But if i append a second Select the query doesn't works, see below:
INSERT INTO factmonitoreo (idEstacion, IdVariable, IdTiempoRegistro, Valor, ValorOriginal, FlagRegistro)
(SELECT dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:29', 0, 0, 0
FROM dimestacion AS dt
INNER JOIN dimtipoestacion AS dte ON dt.IdTipoEstacion = dte.IdTipoEstacion
JOIN dimvariable AS dv
WHERE dt.Estacion LIKE 'El Lago'
AND dte.CodTipoEstacion LIKE 'ECT'
AND dv.ColumnNameToManualUpdateFromCSV LIKE 'Temperatura'),
(SELECT dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:28', 0, 0, 0
FROM dimestacion AS dt
INNER JOIN dimtipoestacion AS dte ON dt.IdTipoEstacion = dte.IdTipoEstacion
JOIN dimvariable AS dv
WHERE dt.Estacion LIKE 'El Lago'
AND dte.CodTipoEstacion LIKE 'ECT'
AND dv.ColumnNameToManualUpdateFromCSV LIKE 'Temperatura')
This piece of code raises the following error:
[Code: 1064, SQL State: 42000] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
(SELECT dt.idEstacion, dv.IdVariable, '2007-01-01 00:04:28', 0, 0, 0
FROM ...' at line 8 [Script position: 1792 - 1853]
I have read this documentation to do an attempt of insert with multiple selects: 13.2.7.1 INSERT ... SELECT Statement

I don't think it support multiple selects in a single insert statement. just split it into 2 sql, that will be fine...
Look at the About This Manual session,
The ellipsis in SELECT ... context is more like the omission of the statement.
Sometimes ellipsis will indicate the preceding element can be repeated. but usually it will be presented as <element>, [<element>]...

Related

CTE + INSERT INTO + SQLAlchemy

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

Creating new table from existing select statement

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 ]

Problem with syntax when try add trigger in mysql

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;

Can't insert into temporary table in MySQL

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!

MySql Insert Dynamic Query with Nested Select

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