I want to run this query from SQL in Excel:
INSERT INTO customer (name, age, gender)
SELECT name = 'Frank', age = '19', gender = 'man'
The point is to insert into the customer table directly from Excel, without having to go through SQL.
I have tried the following statement in excel:
INSERT INTO customers (name, age, gender)
VALUES('"&&"', '"&&"', '"&&"')
But I am not able to connect it to the database, and I have not been able to figure out how to do it in VBA.
Import and Export Wizard
In SQL Server Management Studio, connect to an instance of the SQL Server 2. Database Engine.
Expand Databases.
Right-click a database.
Point to Tasks.
Choose to Import Data or Export Data:
Related
I have a database name "EmpOld" with a table name "Employee" and a database name "EmpNew" with a table name "Employee".
The table structures are identical on both database tables except for the names in the table.
Here is a definition of the tables:
Database "EmpOld" table name "Employee" has following field names:
int e_id
char(20) e_fname
char(25) e_lname
Database "EmpNew" table "Employee" has following field names:
int id
char(20) fname
char(25) lname
Notice, the only difference in the tables is the "e_" prefix for field names are removed from the EmpNew Employee table.
How do I move data from EmpOld database to EmpNew database?
Is there a code that maps these field respectively.
Thanks community,
Nick
Well, you could just name them manually:
INSERT dbo.EmpNew(fname, lname) SELECT e_fname, e_lname FROM dbo.EmpOld;
If you want to do this without manually typing out the column names, there is magic in SSMS - just drag the Columns folder from each table into the appropriate spot in your query window (and then manually remove identity columns, timestamp, computed columns etc. if relevant).
There is no automatic way of mapping fields, unless with some code.
This can be done in two ways:
Using the SQL Import & Export Wizard
This is the most easy way to do this and here is an article that gives step by step to do this. The key is to change the mapping between the source and destination fields while importing the data.
Writing an SQL
This method requires both the databases to be accessible. Using a simple insert statement as follows this can be achieved
insert into EmpNew.dbo.Employee(id, fname, lname)
select
e_id, e_fname, e_lname
from
EmpOld.dbo.Employee
If they are on same sql server then the above will work good as is. If they are different sql server you may have to add a link server connection and prefix the table commands with that.
Is there a code that maps these field respectively.
No - you'll need to provide the mapping. If you're using an ETL tool like SSIS there may be a way to programatically map columns based on some criteria, but nothing built into SQL.
Maybe you can generate code with help from the tables sys.columns and other system tables so that you can make the copy-process run automatically.
I think you can't use:
insert into (...) (...)
because you have two databases. So just generate insert statements like:
insert into table (...) VALUES (...)
Please correct me if i misunderstood the question.
There are 2 ways you can do without the data loss.
1) you can use Insert statement
`
Insert into EmpNew (ID,fname,lname)
Select e_id, e_fname, e_lastname
from EmpOld
`
2) You can simple use Import-Export Wizard
Go to Start Menu > SQL Server 2008/2008R2/2012 > ImportandExport>
This will take you the wizard box
Select Source :- DataSource(ServerName) and Database where you are
extracting data from
Select Destination : DataSource(ServerName) and Database where you are extracting data to
Map the table
BE AWARE of PK/FK/Identity
you are good to go
I just started to learn some SQL syntaxes and now i stuck already when I try to insert data from a Query into a specific database.
I have 5 DB's
db1,db2,db3,db4,db5
in every db there are the same tables lets say dbo.fruits
when I now select db2 and open a query I can easly enter new fruits into my db2 by using :
Insert into dbo.Fruits (Name, Description)
values ('banana', 'yummy');
But if I don't select db2 before. How do I enter my bananas into dbo.fruits from db2 ?
I thought
Insert into [db2] dbo.Fruits (Name, Description)
values ('banana', 'yummy');
but it's not working :/, of course I googled, but didnt find the right answer.
Insert into [db2].dbo.Fruits (Name, Description)
values ('banana', 'yummy');
or if your user is dbowner you can do
Insert into [db2].Fruits (Name, Description)
values ('banana', 'yummy');
The easiest will be to remember to put the database name first for every SQL statement/query.
Something like: SELECT * FROM [db1].[dbo].[Fruits]
ie. ...[DatabaseName].[SchemaName].[TableName]
I'm adding data into a table in my database from a VB application. But, I need to know, is it possible that some of the data I added can be imported to another table?
example:
table1(ID,FisrtName,LastName) data are added completely
table 2 specific (FirstName, LastName)
insert into table2 (FirstName,LastName) select FirstName,LastName from table1;
Use a Trigger AFTER INSERT on the table 1 and the thing is done automatically after inserting the data on table 1 or if the data has already been inserted then use a select insert statement like.
INSERT INTO dbo.Table2(FirstName,LastName)
SELECT T.FirstName,T.LastName FROM dbo.Table1 AS T
And that should be it.
I have a SQL Server 2005. I have created a linked server to PG SQL server.
It works great.
SELECT *
FROM OpenQuery(POSTGRESQL_SERV,
'SELECT comp_id,comp_name FROM company WHERE comp_type = 5')
I need to read all data from PG SQL and insert it into SQL Server.
How to create a while ? (also unique id`s)
Thx.
Not sure if I understand your question entirely (while? unique id's?), but all you'd be doing would be an insert statement:
insert into MyTable ( comp_id, comp_name )
select comp_id, comp_name
from OpenQuery(POSTGRESQL_SERV,'SELECT comp_id,comp_name FROM company WHERE comp_type = 5')
As to creating unique ID's, you'd want to have an IDENTITY column on your destination table, which would create the ID's for you.
I have an application that is ready to go live, once we take data from a MS Access DB and import it into SQL Server 2005. I have used the Migration - Access tool to get the Access db into SQL Server, but now I need to take the data from that table and put it into the tables that our app is going to use. Is there a T-Sql way to Insert multiple rows, while at the same time 're-mapping' the data?
For example
SELECT ID, FIRST_NAME, LAST_NAME
INTO prod_users (user_id, first_name, last_name)
FROM test_users
I know that select * into works when the column names are the same, but the
prod_users (column names, ..., ...)
part is what I really need to get to work.
Any ideas?
I believe the SELECT INTO syntax is used to create new tables. If you want to map data from the tables you just imported to some other existing tables, try a plain INSERT. For example:
INSERT INTO prod_users (user_id, first_name, last_name)
SELECT ID, FIRST_NAME, LAST_NAME
FROM test_users
The mapping of columns from test_users to prod_users is based on the order that they are listed, i.e. the first column in "prod_users (column_names, ...)" matches the first column in the "SELECT other_col_names, ...", the second matches the second, etc. Therefore, in the code sample above, ID is mapped to user_id, and LAST_NAME is mapped to last_name, and so on.
Just make sure you have the same number of columns in each list and that the column types match (or can be converted to match). Note that you don't have to specify all the columns in either table (as long as the destination table has valid defaults for the unspecified columns).
See the INSERT syntax for details; the part relevant to your question is the "execute_statement" expression.
INSERT and SELECT are the magic keywords:
insert into new_table (list of columns) select columnA, functionB(),
(correlated_subquery_C) from table_or_join where critera_expression_is_true
Maybe you can be more specific about what you mean by re-mapping?
Base on your comment, a more specific query is:
insert into new_table (user_id, firstname, lastname)
select id, first_name, last_name from old_database..old_table