I am using the below Query to try to insert a few rows from table2 in Database2 into table1 from Database1. Both these tables have different schemas.
Both the tables have same structure. I did check other posts with a similar query to no avail. Used the below basic approach :
INSERT INTO remoteDATABASE.remoteSCHEMA.remoteTABLE
SELECT * FROM localTABLE
INSERT INTO 'eno*****.com'.asdf_stage.temp
SELECT i.artf_id as id,
title as description,
(case
when i.assigned_group = 'asdfk' then 'dfg'
when i.assigned_group = 'dfgdg' then 'dgdk'
when i.assigned_group = 'dfghdgf' then 'igo'
when i.assigned_group = 'dfgh' then 'eMgem'
when i.assigned_group = 'Edsfg' then 'esgd'
end ) as area,
"N/A" as disposition,
"********" as flavor,
"ipf" as link,
"*************" as vendor,
DATE(date_created) date,
DATE(close_date) as completed
FROM ******com.lkjhg_stage.issues i
where date(date_created) >'2018-04-01' AND status!='Rejected'
I am getting syntax error, need to know what is the correct syntax to mention the DB name , Schema name and the table name at both the places.
Also, the table has around 10-15 columns , do I need to specify all of them ?
Replace the double quotes with single quotes. DATE is not a function, and the reserved word date is not a good column name.
Reference your table as SERVER.DATABASE.OWNER.OBJECT
INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT * FROM localTABLE
INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT i.artf_id as id,
title as description,
(case
when i.assigned_group = 'asdfk' then 'dfg'
when i.assigned_group = 'dfgdg' then 'dgdk'
when i.assigned_group = 'dfghdgf' then 'igo'
when i.assigned_group = 'dfgh' then 'eMgem'
when i.assigned_group = 'Edsfg' then 'esgd'
end ) as area,
'N/A' as disposition,
'********' as flavor,
'ipf' as link,
'*************' as vendor,
date_created,
close_date as completed
FROM SERVER2.DATABASE2.OWNER2.OBJECT2 i
WHERE date_created > '2018-04-01' AND status != Rejected'
Im trying to insert into a linked SQL table in access 2007, below is my query
INSERT INTO tblProducts ( ProductPrefix, ProductCode, ProductDescription, MadeFrom, MadeFromDescription, SamFamilySort1, SamFamilySort2, SamFamilySort3, SamFamilySort4, SamFamilySort5, Grade, Length, Thickness, fWidth, Factor, CubicMtrs, CubicMtrsFull, [Weight(T)], DrawingFilepath, EFACSProductGrouping, BatchSize, PackSize, Density, createdby, createddate, ProductType, customer, DimA, DimB, DimC, DimD, DimE, DimF, DimG, DimH, DimI, DimJ, DimK, DimL, DimM, DimN, DimO, DimP, DimQ, DimR, DimS, DimT, DimU, DimV, DimW, DimX, DimY, DimZ, TolA, TolB, TolC, TolD, TolE, TolF, TolG, TolH, TolI, TolJ, TolK, TolL, TolM, TolN, TolO, TolP, TolQ, TolR, TolS, TolT, TolU, TolV, TolW, TolX, TolY, TolZ, Dimension, Main, Saws, Moulders, PaintLines, XCut, DET, Wrapper, Blocks, HingeRecess, reorderpolicy, machinedaway, UseOtherM3XC, UseOtherM3MS, ShrinkWrap, ShrinkWrapPackSize, SW, samtype1, vtype1, vtype2, profile, productchamp, UOM, SAMPartGrp, PostingClass, ProductID )
SELECT DISTINCT tblProducts.ProductPrefix, tblProducts.ProductCode, tblProducts.ProductDescription, tblProducts.MadeFrom, tblProducts.MadeFromDescription, tblProducts.SamFamilySort1, tblProducts.SamFamilySort2, tblProducts.SamFamilySort3, tblProducts.SamFamilySort4, tblProducts.SamFamilySort5, tblProducts.Grade, tblProducts.Length, tblProducts.Thickness, tblProducts.fWidth, tblProducts.Factor, tblProducts.CubicMtrs, tblProducts.CubicMtrsFull, tblProducts.[Weight(T)], tblProducts.DrawingFilepath, tblProducts.EFACSProductGrouping, tblProducts.BatchSize, tblProducts.PackSize, tblProducts.Density, tblProducts.createdby, Date() AS Expr1, tblProducts.ProductType, tblProducts.customer, tblProducts.DimA, tblProducts.DimB, tblProducts.DimC, tblProducts.DimD, tblProducts.DimE, tblProducts.DimF, tblProducts.DimG, tblProducts.DimH, tblProducts.DimI, tblProducts.DimJ, tblProducts.DimK, tblProducts.DimL, tblProducts.DimM, tblProducts.DimN, tblProducts.DimO, tblProducts.DimP, tblProducts.DimQ, tblProducts.DimR, tblProducts.DimS, tblProducts.DimT, tblProducts.DimU, tblProducts.DimV, tblProducts.DimW, tblProducts.DimX, tblProducts.DimY, tblProducts.DimZ, tblProducts.TolA, tblProducts.TolB, tblProducts.TolC, tblProducts.TolD, tblProducts.TolE, tblProducts.TolF, tblProducts.TolG, tblProducts.TolH, tblProducts.TolI, tblProducts.TolJ, tblProducts.TolK, tblProducts.TolL, tblProducts.TolM, tblProducts.TolN, tblProducts.TolO, tblProducts.TolP, tblProducts.TolQ, tblProducts.TolR, tblProducts.TolS, tblProducts.TolT, tblProducts.TolU, tblProducts.TolV, tblProducts.TolW, tblProducts.TolX, tblProducts.TolY, tblProducts.TolZ, tblProducts.Dimension, tblProducts.Main, tblProducts.Saws, tblProducts.Moulders, tblProducts.PaintLines, tblProducts.XCut, tblProducts.DET, tblProducts.Wrapper, tblProducts.Blocks, tblProducts.HingeRecess, tblProducts.reorderpolicy, tblProducts.machinedaway, tblProducts.useotherm3XC, tblProducts.useotherm3MS, tblProducts.ShrinkWrap, tblProducts.ShrinkWrapPackSize, tblProducts.SW, tblProducts.samtype1, tblProducts.vtype1, tblProducts.vtype2, tblProducts.profile, tblProducts.productchamp, tblProducts.UOM, tblProducts.SAMPartGrp, tblProducts.PostingClass, tblProducts.ProductID
FROM tblProducts
This works fine and uploads all records in the table with new keys if i want to (I dont). I want to only recreate one product ive tried added the below
WHERE (((tblProducts.ProductID)=[tests]));
Where tests is a popupbox for User entry
i get an error below
My primary key in the table is called [ProductID]. It is possible to add a WHERE [ProductID] = 1234 in this query somehow?
Notice that the very last item in the column list of the INSERT INTO clause is ProductID. So, you are trying to insert a new row with an existing Primary Key value, and that won't work. As a simplified example,
INSERT INTO tblProducts (ProductDescription, ProductID)
SELECT tblProducts.ProductDescription, tblProducts.ProductID
FROM tblProducts
WHERE tblProducts.ProductID=1
will fail with a primary key violation. You need to remove ProductID from both the INSERT INTO and SELECT clauses, and only use it in the WHERE clause:
INSERT INTO tblProducts (ProductDescription)
SELECT tblProducts.ProductDescription
FROM tblProducts
WHERE tblProducts.ProductID=1
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
I am having the following issue:
When I execute the following statement, I get an error for it returning more than one row
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow'),
(SELECT musicgenreid
FROM musicgenre
WHERE musicgenreid = 4),
( totallikes = 328374 ) );
I am getting the error on the (select pesonID from person...) statement, and I am trying to use the 'any' keyword to just grab any row, but I cannot seem to get it to work. I have tried just about any permutation I can think of of the current query and 'any', but it does not work. Is there another solution I should be trying or am I just missing the mark for some reason?
It seems you're trying to do something like this:
INSERT INTO artist (personid, musicgenreid, totallikes)
VALUES (
(SELECT personid FROM person
WHERE firstname = 'Joe' AND middlename = '' AND lastname = 'blow'
ORDER BY RAND()
LIMIT 1
),
4,
328374
);
This will get you a random personid that matches the given criteria.
The musicgenreid in your query would be either null or 4. I am forcing it to 4 as it seems that you're manually adding them and you know they already exist.
The total likes field is also fixed but your syntax was incorrect.
try with this sql statement
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow' LIMIT 1 ),
4,
328374);
This is the query I'm trying to accomplish:
update amdashboard
set (ASCID, ASCFirst, ASCLast, ASCOtherName, ASCAdd1, ASCAdd2,
ASCCity, ASCState, ASCZip, ASCZip4, ASCY2007, ASCY2008, ASCY2009,
ASCY2010, ASCY2011, ASCY2012, ASCEthnicity, ASCGender, ASCMaritalStatus)
= (select id, firstname, lastname, listingspousename, add1, add2,
city, state, zip, zip4, y2007, y2008, y2009,
y2010, y2011, y2012, Ethnicity, Gender, MaritialStatus
from ASCNCOAClean
inner join amdashboard
on ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast)
where exists (select id, firstname, lastname, listingspousename,
add1, add2, city, state, zip, zip4, y2007, y2008,
y2009, y2010, y2011, y2012, Ethnicity, Gender,
MaritialStatus
from ASCNCOAClean
inner join amdashboard
on ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast);
I can't get this to work...receiving a syntax error on the first parenthesis. So, I figured I'd try on just one field. I tried this:
update amdashboard
set ascid = (select ascncoaclean.id
from ASCNCOAClean
where ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast)
where exists (select ascncoaclean.id
from ASCNCOAClean
where ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast);
This however returns and error 1242: Subquery returns more than 1 row. That seems silly. I know it's going to return more than one row...I want it to because I need to update multiple rows.
What am I missing?
The query you want would look something like this:
UPDATE amdashboard a, ASCNCOAClean b SET
a.ASCID = b.id,
a.ASCFirst = b.firstname,
a.ASCLast = b.lastname,
a.ASCOtherName = b.listingspousename,
...
a.ASCMaritalStatus = b.MaritialStatus
WHERE a.actorsfirst = b.firstname;
Observe you will have to replace ... with the rest of the column associations I didn't write.
But be careful with that, something tells me this query is going to do something very wrong to your database, because you are not relating the tables using a unique key. If there are two records with the same ASCNCOAClean.firstname you certainly will have loss of data.
Also observe that it is going to update existing records on amdashboard, not add new ones. If your intention is to migrate data from ASCNCOAClean to amdashboard, assuming amdashboard is a brand new, empty table, then the query you want is this:
INSERT INTO amdashboard (
ASCID, ASCFirst, ASCLast, ASCOtherName, ASCAdd1, ASCAdd2, ASCCity, ASCState,
ASCZip, ASCZip4, ASCY2007, ASCY2008, ASCY2009, ASCY2010, ASCY2011, ASCY2012,
ASCEthnicity, ASCGender, ASCMaritalStatus
)
SELECT
id, firstname, lastname, listingspousename, add1, add2, city, state,
zip, zip4, y2007, y2008, y2009, y2010, y2011, y2012, Ethnicity, Gender,
MaritialStatus
FROM ASCNCOAClean;