Syntax for Import Into from Different DBs - MS Access - sql

I'm trying to do (basically) an automated row copy from one backend to another as the backend is being used but is not fully developed (some tables are done, others not; adding the completed data into the more advanced db at the end of the day).
I'd like to know what the syntax is for an SQL statement which:
INSERT INTO tblMyBetterTable * IN "C:\\path_to_db\db.accdb"
FROM tblMyTable IN "C:\\path_to_in_use_db\in-use-db.accdb"
The syntax is not correct but I've searched for a while and can't find out how.. I've seen how to import from one table to another etc, but not across backends. If it's not clear, I'd like to basically do a table copy of all rows in in-use-db.accdb from tblMyTable to the latest version of the backend db.accdb's table tblMyBetterTable.

I suggest you try DoCmd.TransferDatabase
For a query, you need something on the lines of:
SELECT * INTO NewTable
FROM [;DATABASE=Z:\Docs\Test.accdb].Table1
Or the other way round:
SELECT * INTO [;DATABASE=Z:\Docs\Test.accdb].NewTable
FROM Table1
To insert into an existing table:
INSERT INTO table1
SELECT *
FROM [;DATABASE=Z:\Docs\Test.accdb].Table1

Related

Creating searchable table/array in SQL from excel column

I have some programming experience but am brand new to SQL.
Basically I have about 300 terms that I want to search for in a single search.
What is the best way to store those terms in a way that I can iterate through them in a query? They're currently in an excel column and I'd prefer not to have to manually write each one in
SELECT * INTO EXCEL_IMPORT
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\Excel\Spreadsheet.xls; HDR=YES; IMEX=1',
'SELECT * FROM [Sheet1$]');
Will create a temporary table that you can run your queries against.
SELECT * FROM
SEARCHABLEDATABASE
WHERE column_of interest IN
( SELECT search_terms FROM EXCEL_IMPORT )
Or use the SQL Server import wizard
OR Simply run the select queries directly against the sheet.
EDIT: These two queries will match entries in a column of database to terms from a excel spreadsheet, providing they are exactly the same) you could TRIM them both to prevent differences in whitespace causing issue.

Sql loop through the values on a table

first off, noob alert! :))
I need to construct a query that runs on many tables. The tables vary on name just on the last digits as per client code. The thing is, the values that change aren't sequential so looping as in i=1,2,3,... does not work. A possible solution would be to have those values on a given field on an other table.
Here is the code for the first two clients 015 and 061. The leading zero(s) must are essential.
SELECT LnMov2017015.CConta, RsMov2017015.DR, RsMov2017015.NInt, "015" AS CodCli
FROM LnMov2017015 INNER JOIN RsMov2017015 ON LnMov2017015.NReg = RsMov2017015.NReg
WHERE (((LnMov2017015.CConta)="6" And (LnMov2017015.CConta)="7") AND ((RsMov2017015.DR)=9999))
UNION SELECT LnMov2017061.CConta, RsMov2017061.DR, RsMov2017061.NInt, "061" AS CodCli
FROM LnMov2017061 INNER JOIN RsMov2017061 ON LnMov2017061.NReg = RsMov2017061.NReg
WHERE (((LnMov2017061.CConta)="6" And (LnMov2017061.CConta)="7") AND ((RsMov2017061.DR)=9999))
...
So for the first SELECT the table Name is LnMov2017015, the ending 015 being the value, the client code, that changes from table to table e.g. in the second SELECT the table name is LnMov2017061 (061) being what distinguishes the table.
For each client code there are two tables e.g. LnMov2017015 and RsMov2017015 (LnMov2017061 and RsMov2017061 for the second set client shown).
Is there a way that I can build the SQL, based upon the example SQL above?
Does anyone have an idea for a solution? :)
Apparently it is possible to build a query object to read data in another db without establishing table link. Just tested and to my surprise it works. Example:
SELECT * FROM [SoilsAgg] IN "C:\Users\Owner\June\DOT\Lab\Editing\ConstructionData.accdb";
I was already using this structure in VBA to execute DELETE and UPDATE action statements.
Solution found :)
Thank you all for your input.
Instead of linking 100 tables (password protected), I'll access them with SLQ
FROM Table2 IN '' ';database=C:\db\db2.mdb;PWD=mypwd'
And merge them all with a query, before any other thing!

Copy data from one table to another without duplicates based on more then one column comparision

I am stuck in the following query. This was working properly on mySQL but it gives error on MSSQL-2005. The main purpose of the query is to copy data from one table to another without duplicates based on multiple columns comparison from both tables.
I can do this to compare one column for duplication, but I can't do when I compare more then one column for duplication.
Here is my query.
INSERT INTO eBayStockTaking (OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID)
SELECT OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID
FROM tempEBayStockTaking WHERE (OrderLineItemID,SubscriberID,eBayUserID)
Not In (SELECT OrderLineItemID,SubscriberID,eBayUserID FROM eBayStockTaking)
Note: I have been through many similar questions but all in vain.
Thanks
Rather try NOT EXISTS
Something like
INSERT INTO eBayStockTaking (OrderLineItemID,Qty,SKU,SubscriberID,eBayUserID)
SELECT OrderLineItemID,
Qty,
SKU,
SubscriberID,
eBayUserID
FROM tempEBayStockTaking t
WHERE Not EXISTS (
SELECT *
FROM eBayStockTaking e
WHERE e.OrderLineItemID = t.OrderLineItemID
AND e.SubscriberID = t.SubscriberID
AND e.eBayUserID = t.eBayUserID)
)
I know MySQL allows Row Subqueries, nut SQL Server does not allow this.

How do I write a SQL query to select rows from an ODBC source?

I'm fairly new to SQL, and MSSQL in particular. I'm looking for a way to select certain rows from an ODBC data source I have already set up into a table. Something along the lines of:
SELECT
<somecolumns>
INTO
<target_table>
FROM
[ODBC_data_source].sourcetable
I should also mention that the data source is properly configured, and I can import from it with the Import Wizard. Is what I have above possible, or do I need to look for other solutions?
Are you going to be accessing this data source a lot? If so, you might want to look into using a linked server: http://msdn.microsoft.com/en-us/library/ms188279.aspx
If it's just a one time or very infrequent thing then you can use OPENROWSET assuming the ODBC exists on the server itself: http://msdn.microsoft.com/en-us/library/aa276850(v=sql.80).aspx
SELECT column1, column2
INTO new_table_name [IN externaldatabase]
FROM old_tablename
Ok, but you want to pull certain ROWS into a new table. For that just add a WHERE clause:
SELECT column1, column2
INTO new_table_name [IN externaldatabase]
FROM old_tablename
WHERE Name in('Mark','Luke',etc)

Copy Query Result to another mysql table

I am trying to import a large CSV file into a MySQL database. I have loaded the entire file into one flat table. i can select the data that needs to go into separate tables using select statements, my question is how do i copy the results of those select queries to different tables. i would prefer to do it completely in SQL and not have to worry about using a scripting language.
INSERT
INTO new_table_1
SELECT *
FROM existing_table
WHERE condition_for_table_1;
INSERT
INTO new_table_2
SELECT *
FROM existing_table
WHERE condition_for_table_2;
INSERT INTO anothertable (list, of , column, names, to, give, values, for)
SELECT list, of, column, names, of, compatible, column, types
FROM bigimportedtable
WHERE possibly you want a predicate or maybe not;
The answer from Quassnoi was the one I was looking for. Please observe that if new_table_1 doesn't exist yet the "INSERT INTO" statement has to be replaced with a "CREATE TABLE" statement.