I need a solution to select a table from access into a temp table in SQL. I looked at bulk insert but from what I understand the source must be a data file so that will not work. Also, I don't want to use the import/export wizard, this has to be done through code as I just need a temp table to perform certain queries on. The query needs to do something like...
SELECT * FROM [Access DB] INTO #TempTable (in SQL)
Anyone got any ideas?
SELECT * INTO #TempTable
FROM [Server_Name].[Database].[Schema].[Table]
You will need to add Access data source as a Linked Server to Sql Server. GOTO Obejct Explorer--> SQL Server--> Server Objects --> Linked Servers--> Right CLick and follow the instruction of adding a linked server. Once you have added Access database as a Linked server you can query it by using above command
Or you can use the OPENROWSET to query Data
SELECT * INTO #TempTable
FROM OPENROWSET(
'Microsoft.Jet.OLEDB.4.0',
'C:\Program Files\Path_to_Access_Database_File\Database_Name.mdb';
'admin';'',Table_Name
)
Using a linked server your best solution. Listed below is a technet article on setting them up. You may need to install a driver, I have included the link for the Office 2007 drivers. Here is a screenshot of my config for an Access 12.0 connection.
http://www.anony.ws/i/2013/11/21/UPm4G.jpg
http://technet.microsoft.com/en-us/library/ff772782.aspx#SSMSProcedure
http://www.microsoft.com/en-us/download/details.aspx?id=23734
Related
Hello I have to pass a select from a database that is on an ip address to another (identical) database that is on a completely different IP, below the query how to pass to make the switch?
Sql Code:
/*Insert into database with same name into same table addres:: 172.16.50.98*/
Insert into
/* select from database address: 172.16.50.96*/
SELECT IdUtente,Longitudine,Latitudine,Stato,DataCreazione
FROM Quote.dbo.Marcatura
where DataCreazione>'2019-01-08 18:37:28.773'
Linked Server/ OpenQuery is the way to achieve this. have a look on this.
including parameters in OPENQUERY
If the data that's being imported isn't large and this won't be a reoccurring task a linked server would probably be the better option. Creating one through the SSMS GUI is easier if you haven't done this before, but an example of creating one using the SP_ADDLINKEDSERVER stored procedure through T-SQL is below. If your account doesn't have access to the other server the SP_ADDLINKEDSRVLOGIN stored procedure will need to be used to configure the linked server with an account that has the appropriate permissions on the source server, as well as database and any referenced objects. While using the linked server syntax (4 part name) is simpler and easier to read, I'd strongly recommend doing the insert with OPENQUERY instead if only one linked server will be used. This will execute the SQL on the source server, applying any filters there and only return the necessary rows, whereas the linked server syntax will return all the rows before performing the filtering. You can read more about the differences between the two here. You indicated the database name is the same on both servers, and this assumes the same for the table and schema names as well. Make sure to update these accordingly if they differ.
If a large volume of the data will imported or if this will be a regular process creating an SSIS package and setting this to run as a SQL Agent job will be the better approach. If you choose to go this route there are a number of things to consider, but the links below will help you get started. SQL Server Data Tools (SSDT) is where the packages can be developed. While not necessary, executing the packages from the SSIS Catalog, SSISDB, will be much more beneficial than just the using the file system. Either an OLE DB or SQL Server Destination can be used since the table that's being loaded to is on SQL Server, however a SQL Server Destination can only be used on a local database.
Linked Server:
--Create linked server
--SQL product name and SQLNCLI11 provider for SQL Server
EXEC [MASTER].DBO.SP_ADDLINKEDSERVER #server = N'MyLinkedServer', #srvproduct=N'SQL',
#provider=N'SQLNCLI11', #datasrc=N'ServerIPAddress'
--OPENQUERY insert
INSERT INTO Quote.dbo.Marcatura (IdUtente, Longitudine, Latitudine, Stato, DataCreazione)
SELECT
IdUtente,
Longitudine,
Latitudine,
Stato,
DataCreazione
FROM OPENQUERY(MyLinkedServer, '
SELECT
IdUtente,
Longitudine,
Latitudine,
Stato,
DataCreazione
FROM Quote.dbo.Marcatura')
SSIS:
SSIS
SSDT
SSISDB
Execute SQL Task
Data Flow Task
OLE DB Source
OLE DB Destination
SQL Server Destination
SQL Server Agent SSIS Packages
SSIS solution
I think this requires a very simple SSIS package to be achieved:
Create two OLEDB Connection manager; one for each server
Add a data flow task
Inside the Data flow task addan OLEDB Source and OLEDB destination
In the OLEDB source (172.16.50.98 connection manager) select SQL command as Access mode and use the following command:
SELECT IdUtente,Longitudine,Latitudine,Stato,DataCreazione
FROM Quote.dbo.Marcatura
where DataCreazione >'2019-01-08 18:37:28.773'
Map the source columns to the OLEDB destination (172.16.50.96 connection manager)
Helpful links
Extract Data by Using the OLE DB Source
SSIS OLEDB Source to OLE DB Destination example
I am working in Visual Studio and using the SQL manager built into the studio. Now I am connecting to several databases and I would very much like to be able to save and open my SQL queries and still have them access the correct database and table.
So:
Database servers:
db.company.com
databasenumber1
databasenumber2
databasenumber3
db2.company.com
databasenumber1
databasenumber2
databasenumber3
db3.company.com
databasenumber1
databasenumber2
databasenumber3
Now I wish to write an sql query that does something simple, lets say:
select * from users where userid = '12';
However I want to select this from database server db2 and from database databasenumber3.
How do I write that in a use statement? Or is there something other than "use"??
Working among several databases in once script file requires USE followed by GO statement.
USE db1;
GO
SQL statements ...
...
USE db2;
GO
SQL statements ...
...
Another option is to use server.dbname.tablename format but that strictly requires that all of your databases are hosted on same server.
SELECT * FROM server.db1.table1
SELECT * FROM server.db2.table2
...
I want to select data from one table (T1, in DB1) in one server (Data.Old.S1) into data in another table (T2, in DB2) in another server (Data.Latest.S2). How can I do this ?
Please note the way the servers are named. The query should take care of that too. That is,
SQL server should not be confused about fully qualified table names. For example - this could confuse SQL server - Data.Old.S1.DB1.dbo.T1.
I also want "mapping" . Eg Col1 of T1 should go to Col18 of T2 etc.
Use Sql Server Management Studio's Import feature.
right click on database in the object explorer and select import
select your source database
select your target database
choose the option to 'specify custom query' and just select your data from T1, in DB1
choose your destination table in the destination database i.e. T2
execute the import
create a linked server. then use an openquery sql statement.
select * into [newtable] from [linked_server].[databasename].dbo.[tablename]
If it is just one time linked server in appropriate.
But if it neede to move data frequently ,replication is better and easier.
I think you're overcomplicating it by insisting on SQL. In SSMS, right-click the server you want to export from, select "Tasks", "Export", and let the wizard walk you through the steps of selecting your target server and table, which includes mapping all of the columns exactly as you're trying to do with your SQL situation. All the functionality you seem to be looking for is already there.
There's no need for linked servers, SSIS, or anything else to accomplish this. It's already built into SQL Server Management Studio.
Based on the accepted answer.
Make sure the source server (server1) is linked to the destination server (server2):
SELECT *
INTO Server2.DB2.dbo.T2
FROM OPENQUERY (server1
, ' SELECT col_one, col_two, ...
FROM DB1.dbo.T1
WHERE ...
...
);
You could look into this:
Clicky!
Or you could use SSIS, which would probably be much simpler.
I have read only permission on a SQL Server 2005 database, and I'm looking to get a table schema locally to work with. With my current access it won't let me right click on the table and choose 'Create Table' to get the script for this.
Is there a way to generate the create table script from a select statement or by some other mechanism?
Thank you!
You could create a linked server on your local machine. Then, you can use select ... into to copy the table, including data:
select *
into NewTableName
from [LinkedServerName].[DatabaseName].dbo.TableName
This will not copy indexes or constraints. To exclude the data, use select top 0 *.
It's a bit long but the script in Script Table Definitions using TSQL could be addapted to work without needing to create the stored procedure mentioned.
I am assuming you are working with SQL Server 2005
I am using Oracle 9i, Please suggest how can I select data from one remote database and insert the data in the local database?
Also suggest how the data can be copied from a remote to remote database.
You need to create a database link.
Please refer to this link: http://download.oracle.com/docs/cd/B10501_01/server.920/a96521/ds_concepts.htm#12354
excerpts:
example:
CREATE DATABASE LINK sales.us.americas.acme_auto.com CONNECT TO scott IDENTIFIED BY tiger USING 'sales_us';
query:
For example, using a database link to database sales.division3.acme.com, a user or application can reference remote data as follows:
SELECT * FROM scott.emp#sales.division3.acme.com; # emp table in scott's schema
SELECT loc FROM scott.dept#sales.division3.acme.com;
Based on the vagueness of the question. Make a backup of production and restore it in development.
If you are talking Microsoft SQL then you can create a Linked Server. Here is an article about doing this in SQL 2008, but you can do it in earlier versions as well. Then you can select from it using a four part name LinkedServer.database.schema.table
http://msdn.microsoft.com/en-us/library/ff772782.aspx
Define a link from the development server to the prooduction server. You can then use a select based insert to copy data into the development server.
Use the SAMPLE clause on the select to retrieve a percentage of the data. For child tables use a WHERE exists clause to copy child rows for which the parent was sampled.