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
Related
I have this query in SQL SERVER:
select *
from ADRESSEN
where Transferred is NOT null
and NR_ID not in (Select NR_ID from openquery(PROD, 'Select * from TABELLEN.NUMMERN'))
When I execute this query I get this error:
OLE DB provider "OraOLEDB.Oracle" for linked server "PROD" returned message "".
Msg 7341, Level 16, State 2, Line 12
Cannot get the current row value of column "[OraOLEDB.Oracle].NR_ID" from OLE DB provider "OraOLEDB.Oracle" for linked server "PROD".
But when I comment one of the condition in WHERE and I execute it, the query works:
select *
from ADRESSEN
where Transferred is NOT null
It works! And this too:
select *
from ADRESSEN
where
NR_ID not in (Select NR_ID from openquery(PROD, 'Select * from TABELLEN.NUMMERN'))
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?
The script below return a select records:
EXEC [LINK_SERV].[DB_SAMPLE].[dbo].[SP_SAMPLE] '1235123'
The I want to insert the return records to a temp table so I wrote the script below (assumed temp table is already created):
INSERT INTO #TempTable
EXEC [LINK_SERV].[DB_SAMPLE].[dbo].[SP_SAMPLE] '1235123'
But I get this error:
OLE DB provider "SQLNCLI11" for linked server "LINK_SERV" returned message "The partner transaction manager has disabled its support for remote/network transactions.
Msg 7391, Level 16, State 2, Line 25
The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "LINK_SERV" was unable to begin a distributed transaction.
Please advise the specific config to enable this. I tried the same code in some other server and it work. Thank you in advance.
This is where the beauty of OPENQUERY() comes into great use.
i.e. something like this...
INSERT INTO dbo.MyTable2
SELECT Col1, Col2, etc
FROM dbo.MyTable1
LEFT JOIN OPENQUERY(<<LINKEDSERVER>>,
'SELECT BLAHBLAH
FROM dbo.BLAHBLAH WHERE <something> = <something>);
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
I am trying to read data from an Excel using the Following statement which is working absolutely fine
Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select MyColum from [Sheet1$]')
the XLS inpout.xls has two columns ID and NAME. Now I want the above statement to return only ONE value/row where ID = x and assign the returned value to a local variable as shown below -
DECLARE #dbValue varchar(20)
DECLARE #I int
SET #I = 1
Select #dbValue = (Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select MyColum from [Sheet1$] where [ID]=#I'))
this returns an error
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" returned message "No value given for one or more required parameters.".
Msg 7320, Level 16, State 2, Line 3
Cannot execute the query "select MyColum from [Sheet1$] where [ID]=#I" against OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".
Please suggest.
Maybe something like this will work. But your question is confusing, since your query on the Excel file selects MyColumn, but you say that file has columns ID and NAME.
DECLARE #dbValue varchar(20)
DECLARE #I int
SET #I = 1
Select #dbValue = NAME FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select ID from [Sheet1$]') as T
WHERE ID=#I;