How to merge Access DB table into a SQL Server table? - sql

I have a SQL Server table that has a unique field which matches a unique field in an Access DB table.
I need to get the Access DB table into the SQL Server table based on that field.
How do I go about doing this?
I want to say a JOIN, but a JOIN is just a temporary query, isn't it? I want this to be permanent. I want the Access DB table data merged into SQL Server table based on this unique field.
What's the best way to do this?
Thanks.
The reason I need this separate is that a software is reading this SQL Server data, so I need it all in the same table.

B,
I believe what you would be looking for is Union
With the two tables being
HumanResources.Employees and HumanResources.Employees2
SELECT ID, Name FROM HumanResources.Employees
UNION
SELECT ID, Name FROM HumanResources.Employees2

Related

How to run sql query and loop through a column then pass that column data into another query from a second database

I am fairly new to SQL. I am using SQL Server 2014. I want to run a query on a database which returns a column of ID's. I am wondering if it is possible to loop over the column of ID's from the first database and pass them into another database to collect additional info.
Attempted to Google the answer but I'm not able to find a helpful scenario that mimics what I am looking for.
SELECT *
FROM dbo.MYDB1
WHERE CreatedLoc = 123
The above example spits out data but I only care about the ID column
I than want to loop over the ID column and for each run them on another database.
SELECT *
FROM dbo.MYDB2
WHERE ID IN (array of ids here, not hardcoded but dynamic)
Assuming appropriate permissions, you can access a different database than the one you're currently connected to using a fully qualified databasename.schemaname.tablename (or view, etc.)
If your databases are MyDB1 and MyDB2, you can run a query that looks something like this:
SELECT * from MyDB2.dbo.Table2
where ID IN (
SELECT ID from MyDB1.dbo.Table1 where CreatedLoc = 123
)

Retrive unique records from an access table which does not have a fixed structure

I want only the unique records in same or new table. And I want to do this with different tables (having duplicate records) in access database through same code.
The flow should be like:
input table ------VBA MODULE------> table with unique records
I am able to do this group by function but for that i have to use field names in query. But field names will differ from table to table.
Please help!
Just use query
SELECT DISTINCT * FROM MyAnyTable

How to determine if a column exists in SQL server DB in other tables with a different name

I need to fetch specific hardware data from source tables. The hardware information is present in a table Server_Data with columns as follows,
Server_ID, Server Property, Property_Value
65 Model Cisco 123
65 Name Cisco abc
I need to link this table with System table that has columns as follows,
System_ID, System_IP
1 10.20.30.40
I searched all tables in database but Server_ID column is present only in Server _Data table. Also, I searched all tables if there exists a table that links System_ID with Server_ID, but there is no such table.
I need to find if the Server_ID column is present in any other table with some other name (say Server_Key or just Key). Any help would be appreciated.
Only using SQL, there will not be a way to find an identical column in another table, especially if it has a different name. I think you would need to manually compare every column in every other table to that column to find a match.
If I were you I would start by running the following SP:
EXEC sp_fkeys 'Server_Data'
That will tell you if the Server_ID column is referencing any other table in the DB or not.

Query to Access Sharded Tables

I am querying a vendors database that has data that is sharded between multiple tables. The tables are named Events_1, Events_2, Events_3, Events_4 etc.
It appears that a new table is automatically created when the table hits 10,000 records. I am looking to write a query that would be able union the results from all the tables (without having to manually add the tables as they are created) as well as a query to only look at the 2 newest tables.
Any suggestions on the best way to go about this?
The database is Microsoft SQL Server 2008
You can do this with dynamic SQL such that you query sysobjects for table name Events_* and build a SELECT UNION from the results.
Better, though perhaps more complicated, would be a DDL trigger that would dynamically update you UNION VIEW to append the new table whenever a table Events_* was created.

View that accesses two databases

Have no idea how to do this! I have two databases on the same instance of SQL server 2008
In Database1 I have a table that has a column that holds a productID.
In Database2 I have a table that holds product info, i want to grab the SKU from this table based on the productID from Database1.
Its pickling my head! Hope you can help!
Many thanks
Use the fully qualified table name to access the table cross-database.
DatabaseName.Schema.TableName
join them as if they were on the same DB, just add the DB name in front of the query.
Of course, you may have to deal with permissions to access one DB from another
Select D1.productID, D2.productINfo
from Database1.SchemaName.Table D1 join Database2.SchemaName.Table D2 on D1.key=D2.key
maybe this:
TSQL: Create a view that accesses multiple databases
and
http://msdn.microsoft.com/en-us/library/ms187956.aspx