Bulk upload from Excel in SQL Server 2008 - sql

I'm trying to bulk upload from an Excel file using the following query:
BULK INSERT TempRevenueForecast
FROM 'E:\RevenueTracker\Demo\UserTemplate.xls'
WITH (FORMATFILE = 'E:\RevenueTracker\Demo\TRF.FMT');
GO
But, I'm getting this error:
Bulk load data conversion error (truncation) for row 1, column 2
How can I solve it?

If you open the table TempRevenueForecast in edit mode (right-click-table - edit top 200 rows) in SSMS, you can just copy-paste the excel data over.
That's a very handy feature of SSMS.
Otherwise, use OpenRowSet:
INSERT INTO [TempRevenueForecast ] ([Column1], [Column2], [Column3], [Column4])
SELECT A.[Column1], A.[Column2], A.[Column3], A.[Column4]
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=E:\RevenueTracker\Demo\UserTemplate.xls;HDR=YES', 'select * from [Sheet1$]') AS A;
For that to work, the Microsoft Access Database Engine (ACE) components must be installed.
http://www.microsoft.com/en-us/download/details.aspx?id=23734
or
http://www.microsoft.com/en-us/download/details.aspx?id=13255

Related

SQL Server: Bulk Insert to a Linked Server

I have a local database myDB and a server database serverDB that is linked to myDB as [serverDB].serverDB.dbo. I want to upload a 50,000-row table from a .csv file on my computer to the serverDB. I tried this:
bulk insert #temp from 'filename'
insert into [serverDB].serverDB.dbo.tablename select * from #temp
and it takes ages. I found out that the insert into creates connection for each row, so it looks like it's not an option in this case. Then I tried
bulk insert [serverDB].serverDB.dbo.tablename from 'filename'
and I get the error Invalid object name 'tablename' even thought this table exists in the [serverDB].serverDB database. Does anyone know how I can make SQL "see" the table [serverDB].serverDB.dbo.tablename?

Database direct import from Excel?

I've worked on SQL Server 2014 and I need to import data in it from Excel.
'Till now, I've imported data by right-clicking on the current database -> Tasks -> Import Data.
I want to know if there is a SQL syntax to import data directly into a table?
You can try OPENROWSET
Insert into SQLServerTable
Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [SheetName$]')

How can I save the results of an OPENQUERY query to an Excel or CSV file?

How to store data from an openquery in a .CSV OR EXCEL FILE?
select * from openquery(DB_LINK,'SELECT col1,col2,col3,col4 from oracle_table_name')
select * from openquery(DB_LINK,'SELECT col1,col2,col3,col4,col5 from oracle_table_name2')
The result has to be stored in 2 different files.
I have scheduled the open query such that it runs on 1st of every month.
And the 2 files that are generated have to be kept in client's windows specific folder.
This is the first time I'm using SQL SERVER. So please explain step by step.
I am using this query at the moment.
declare #title varchar(55)
set #title = 'Test'
EXEC sp_makewebtask
#outputfile = 'D:\Test11.xls',
#query = 'select CIRCUIT_DESIGN_ID as [Account Number] from openquery(MSP_PROD,'select CIRCUIT_DESIGN_ID from circuit')',
#FixedFont=0,#resultstitle=#title
Create an Export Data... task using SQL Server Management Studio (by right clicking the database), specify that you want to output the data to CSV, and that your datasource is to be a SQL statement.
Use your select * from openquery... statement as the datasource.
Save the package, then create a SQL Server Agent job that will execute it on your desired schedule.

How to use OpenRowSet to insert data into a blank file?

How to use OpenRowSet to insert data into a blank file?
I need to insert into a txt file (say to D:\TDB) some select output (say select * from sys.tables) from the database
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I get
OLE DB provider "MICROSOFT.JET.OLEDB.4.0" for linked server "(null)"
returned message "The Microsoft Jet database engine could not find the
object 'sys.tables.txt'. Make sure the object exists and that you spell its
name and the path name correctly.".
Msg 7350, Level 16, State 2, Line
1 Cannot get the column information from OLE DB provider
"MICROSOFT.JET.OLEDB.4.0" for linked server "(null)".
What is wrong?
PS. please do not propose the bcp solution, cause already tested and does not work everytime, so I would test openrowset now..
#serhio , I tested your sql below:
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I got a few test results
The filename should not include "." in it.
(sys.tables.txt→systables.txt)
HDR(Header Row) can not be used here.(Remove it)
The txt file must exist.(Create it)
The first line in the txt file should be the all column name of your
source data.
sql
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;', 'SELECT * FROM systables.txt')
select * from sys.tables;
systables.txt
name,object_id,principal_id,schema_id,parent_object_id,type,type_desc,create_date,modify_date,is_ms_shipped,is_published,is_schema_published,lob_data_space_id,filestream_data_space_id,max_column_id_used,lock_on_bulk_load,uses_ansi_nulls,is_replicated,has_replication_filter,is_merge_published,is_sync_tran_subscribed,has_unchecked_assembly_data,text_in_row_limit,large_value_types_out_of_row,is_tracked_by_cdc,lock_escalation,lock_escalation_desc

Is there an Oracle SQL tool that builds insert statements from a result set?

Is there an Oracle SQL tool that builds insert statements from a result set? We are currently only allowed to use a tool called SQL Station. I'd like to either suggest a tool, like Rapid SQL or CrazySQuirrell, or build my own re-usable chunk of sql.
Where is this result set coming from? If you mean that you want to execute a SELECT, then insert the resulting data into another table, you can do that in a single SQL statement:
INSERT INTO table2 (columnA, columnB)
SELECT columnA, columnB
FROM table1;
PL/SQL Developer will do this as well. I've used both PL/SQL Developer as well as Oracle's SQL Developer, and in my opinion PL/SQL Developer has a smoother and more consistent interface. Not sure about SQL Developer, but PL/SQL Dev. also lets you export result sets as CSV,XML, and HTML.
It also behaves OK under WINE if you're running Linux.
If you want command line tools, the free cx_OracleTools will do this, and some other nice things as well.
http://cx-oracletools.sourceforge.net/
CompileSource - execute statements in a file, checking for errors
CopyData - copy data from one table or view to another
DbDebugger - allows simple debugging of PL/SQL
DescribeObject - describe objects as SQL statements for recreation
DescribeSchema - describe multiple objects as SQL statements for recreation
DumpCSV - dump the results of a select statement as comma separated values
DumpData - dump the results of a select statement as insert statements
ExportColumn - dump the data from a column into a file
ExportData - dump the data from a database into a portable dump file
ExportObjects - describe object as SQL statements for recreation in files
ExportXML - export data from a table into a simple XML file
GeneratePatch - generate SQL script to go from one set of objects to another
GenerateView - generate a view statement for a table
ImportColumn - import the contents of a file into a column in the database
ImportData - import the data dumped with ExportData
ImportXML - import data from an XML file (such as those created by ExportXML)
RebuildTable - generate SQL script to rebuild the table
RecompileSource - recompile all invalid objects in the database
Yes look at Oracle sql developer.Its free can be downloaded from otn.oracle.com
I found this solution, which is what I'm using now. Thanks for all of the help.
It turns out we can use SQL+ too. For some reason I can't run it in SQL Station.
COPY FROM userid/password#from_DB TO userid/password>#to_DB INSERT toDB_tablename USING SELECT * FROM fromDB_tablename where ....;
commit;
In a pinch, using string contatenation works great for smaller statements you want to build:
Select
'Insert Into MyOtherTableTable Values(''' || MyMainTableColumn1 || ''' and ''' || MyMainTableColumn2 || ''')'
From MyMainTable
Right click on the result set of the query, you will get a pop up. select export data and insert. it will ask you for the location to save the file in which insert statements are generated. give file name and the path to save it.
I know it is too late but It could be helpfull for somebody.
If you go to the table, you can "export" the data. The second step is "Specify Data" where you can add some filters.
This only works for a table data.
Cheers
With Oracle SQL-Developer type and execute as script (F5):
select /*insert*/
* from dual;
output:
Insert into "dual" (DUMMY) values ('X');
you can try also /*csv*/" or /*html*/
source: http://www.thatjeffsmith.com/archive/2012/05/formatting-query-results-to-csv-in-oracle-sql-developer/
SELECT /*csv*/ * FROM scott.emp;
SELECT /*xml*/ * FROM scott.emp;
SELECT /*html*/ * FROM scott.emp;
SELECT /*delimited*/ * FROM scott.emp;
SELECT /*insert*/ * FROM scott.emp;
SELECT /*loader*/ * FROM scott.emp;
SELECT /*fixed*/ * FROM scott.emp;
SELECT /*text*/ * FROM scott.emp;