Import Excel data into an existing SQL Server 2005 table - sql

I'm trying to import some data from an Excel sheet into an existing SQL Server 2005 table.
The table has the following columns:
Name, Surname, PhoneNumber ,Bill
and they are in the same order in the Excel sheet.
I've never attempted such a thing before, but after searching for a while I was told I could use:
INSERT INTO table
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\temp\test.xls',
'SELECT * FROM [Sheet1$]')
Do I need to reference the column names from the Excel sheet to the SQL Server table columns?
I'm certainly not a SQL guru, but would consider myself a good beginner.
Can anyone help?

See here in you Management Studio
:
Choose you db?
Choose your source :
and from here is all just following the GUI !!

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.

How to use CSV data in an SQL query

I have a some data in an excel sheet in the form of a table. I want to run an sql query on that data. I am okay to use the data raw from the csv.
Is it possible to create a temporary view or table using that data within the query itself. I don't want to create a separate table for this excel data.
There are a bunch of answers for this already... Have you searched?
You want to do something like this;
Select *
into [temp_table$]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\spreadsheet.xls;HDR=YES;IMEX=1',
'SELECT * FROM [SHEET1$]')
Like in this question: Get Excel sheet into temp table using a script
For SQL Server 2008 and above you can use OPENROWSET
In simplest form it will look like this:
SELECT * FROM OPENROWSET(BULK 'data.csv', SINGLE_CLOB) AS DATA;
Just remember to specify full file path.

How to deal with Excel file in SQL query?

i had a problem .... i have and old excels files and i want to save them into SQL database ... MY Q is : if i want to do for example in .xls from (C16:C28) want to take it and put it into table1 in field NAMES .. how i can write it in query ?
note am using Microsoft SQL Server Management .. and try to get all my xls and import it into data base via sql query take from those files data and put it into my database ..
INSERT INTO [table1] (Names) VALUES ('&C16&') to ('&C28&')
is this true ? idk how to write it .. need help
When you use openrecordset you can specify a range [Sheet1$C16:C28] on the table in the query parameter
INSERT INTO [table1] (Names)
SELECT *
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\Foo.xls;HDR=NO',
'select * from [Sheet1$C16:C28]') AS t

Create Table by Copying Structure of Existing Table

I am trying to create a new table by copying an existing table in SQL Server 2008 using Management Studio. The existing table contains no data. I am using the following code but am receiving an error for Incorrect Syntax near AS. I am not sure what is wrong here. I am a SQL newb and any help would be appreciated. Thanks.
CREATE TABLE Drop_Centers_Detail
AS (Select * From Centers_Detail)
like this, however this will not create indexes and constraints
select * into Drop_Centers_Detail
from Centers_Detail
where 1 = 0
In Sql Server Managment Studio, right-click your existing table and select Script Table as > Create to > New Query Editor Window. This will give you a better starting script that you can use as the base for your new schema.
1) I would suggest generating a create script from the table you want to copy, and then run that on your destination database.
2) Write a Insert statement in another SQL Query window to import that data
Insert Into Database1.Table1(Field1, Field2)
Select Field1, Field2 From Database2.Table

To get data from table in script from sql server 2005

I am using sql server 2005
I have a table [say tblHistory] and this table contains 100 rows.
I have created the same table at the server, but the table doesn't have the data, I want data from tblHistory to convert into
INSERT INTO tblHistory ------
so that I could run the script on the server to fill the database.
To generate all the INSERT INTO statements you need based on table data, take a look at this project: http://www.codeproject.com/KB/database/sqlinsertupdategenerator.aspx
you need to create a linked server between the two servers and then you do something like this
INSERT INTO tblHistory
select * from LinkedServerNAme.DatabaseName.SchemaName.tblHistory
To add a linked server read this http://msdn.microsoft.com/en-us/library/ms190479.aspx
BTW you can also use OPENROWSET, OPENDATASOURCE, SSIS or bcp out and bcp in
Not sure I understand the question... you just want to copy the contents of one table into another?
INSERT INTO newTable SELECT * FROM tblHistory
If the new table doesn't already exist, you can use SELECT INTO:
SELECT *
INTO new_table
FROM tblHistory
But that's the caveat - it has to be a new table, no data already in there.
Otherwise, use:
INSERT INTO new_table
SELECT x.* --preferrable to actually define the column list than use *
FROM OLD_TABLE x
You should check out the SSMS Tools Pack - one of its feature is the ability to generate those INSERT scripts you're looking for!
Get the SSMS Tool Pack from this site - it's an add-in for SQL Server Management Studio - highly recommended!