| Author |
Topic |
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-23 : 13:37:50
|
| 1)i have to read a csv file into sql. just return it as a result set. dont have toinsert into any table.2) basically my intention is to use a select * into <table> kind of statement so that i dont have to create the table upfront. I have around 200 csv dont which can change in every months feed. dont want to create the table upfront. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-23 : 13:43:30
|
| use openrowset function------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-23 : 14:03:21
|
| select * FROM openrowset(BULK'C:\Users\name\Desktop\PartPrice_GENERAL_MOTORS.csv', Single_BLob) AS csv iget something like 0x224F452050415254204E554D424552222C224F452050415254205052494345222C2....what am i doing wrong? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-23 : 14:14:53
|
| dont use openrowset bulk. just use likeSELECT * FROM OPENROWSET('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir={your path}','SELECT * FROM ypurtextfile.csv')------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-23 : 14:16:21
|
| Turned on sp_configure 'show advanced options', 1;GORECONFIGURE;GOsp_configure 'Ad Hoc Distributed Queries', 1;GORECONFIGURE;GOnow i getOLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".Msg 7303, Level 16, State 1, Line 1Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)". |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-23 : 14:23:16
|
| [code]SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Text;Database={path here}','SELECT * FROM yourtextfile.csv')[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-23 : 14:26:15
|
| Msg 7308, Level 16, State 1, Line 1OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode. |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-23 : 14:28:35
|
| i have a 64bit os |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 00:26:33
|
quote: Originally posted by AAAV Msg 7308, Level 16, State 1, Line 1OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode.
Ok..Seems like distributed queries are disabled in your db. check itEXEC sp_configure 'ad hoc distributed queries'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-24 : 07:42:00
|
| Ad Hoc Distributed Queries 0 1 1 1is there...i created a table and loaded the csv .. with thisBULKINSERT GMFROM 'C:\Users\vin\Desktop\PartPrice_GENERAL_MOTORS.csv'WITH(FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')GOnow my problem is 1. i want it to ignore the first row which it the column names and 2. I am getting the row as "00457043" $58.72 "GENERAL MOTORS" 0i want to get rid of the " marks in the 1st and 3rd column. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-24 : 12:23:52
|
| --EXEC master.dbo.sp_configure 'show advanced options', 1--RECONFIGURE--EXEC master.dbo.sp_configure 'xp_cmdshell', 1--RECONFIGURECREATE TABLE ##GM( [OE PART NUMBER] [varchar](max) NULL, [OE PART PRICE] [varchar](max) NULL, [MSRP Manufacturer] [varchar](max) NULL, [Core Flag] [varchar](max) NULL) ON [PRIMARY]master..xp_cmdshell 'bcp ##GM in C:\PartPrice_GENERAL_MOTORS.csv -f C:\format.txt -T'bulk insert ##GM from 'C:\PartPrice_GENERAL_MOTORS.csv' with (formatfile = 'C:\format.txt')Error i get(8 row(s) affected)Msg 4832, Level 16, State 1, Line 2Bulk load: An unexpected end of file was encountered in the data file.Msg 7399, Level 16, State 1, Line 2The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.Msg 7330, Level 16, State 2, Line 2Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".with rows output as SQLState = 08001, NativeError = 2Error = [Microsoft][SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [2]. SQLState = 08001, NativeError = 2Error = [Microsoft][SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.SQLState = S1T00, NativeError = 0Error = [Microsoft][SQL Native Client]Login timeout expiredNULL |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-24 : 12:25:15
|
| my data"OE PART NUMBER","OE PART PRICE","MSRP Manufacturer","Core Flag""00457043",$58.72,"GENERAL MOTORS",0my format file8.041 SQLCHAR 0 0 "\"" 0 "OE PART NUMBER" Latin1_General_CI_AS2 SQLCHAR 0 0 "\"," 1 "OE PART PRICE" Latin1_General_CI_AS3 SQLCHAR 0 0 ",\"" 2 "MSRP Manufacturer" Latin1_General_CI_AS4 SQLCHAR 0 0 "\"," 3 "Core Flag" Latin1_General_CI_AS5 SQLCHAR 0 0 "\r\n" 4 u Latin1_General_CI_AS |
 |
|
|
AAAV
Posting Yak Master
152 Posts |
Posted - 2010-03-26 : 13:43:19
|
| any help? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-29 : 02:29:16
|
| Make sure the files exist in Server's directoryMadhivananFailing to plan is Planning to fail |
 |
|
|
amer_md_jamal
Starting Member
1 Post |
Posted - 2011-10-24 : 08:40:41
|
| Try thisBULKINSERT GMFROM 'C:\Users\vin\Desktop\PartPrice_GENERAL_MOTORS.csv'WITH(FIRSTROW=2,FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')GORegards,Amer M. J. |
 |
|
|
|