Importare dati da Excel a SQL Server 2005 Express

lunedì, 5 maggio 2008 09.55 by Marco Bellinaso

Quando l'altro giorno mi è stato chiesto di importare i dati di un foglio Excel in una tabella di SQL Server 2005, mi son detto "no problem, lancio il Data Import/Export wizard, e via". Purtroppo tale tool non è presente nella versione Express, dal momento che mancano anche i SQL Server Integration Services (SSIS) and SQL Server Agent sui quali quel wizard si basa. Mi sembrava uno spreco installare la versione Standard solo per questo, quindi ho cercato qualche soluzione alternativa. E' possibile ad esempio scrivere una macro VBA direttamente in Excel per ciclare sulle righe e colonne, e inserire i dati in SQL Server tramite il vecchio Recordset di ADO...ma anche questo mi sembrava troppo lavoro per un compito così semplice! La soluzione che ho adottato -- e che ha funzionato alla perfezione -- è stata di usare la funzione OPENROWSET, in questo modo:

SELECT * INTO Negozi FROM 
OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=c:\temp\DatiNegozi.xls', [Sheet1$])

Questo codice prende tutti i dati presenti nel file XLS specificato (nel foglio Sheet1), e li copia in una nuova tabella chiamata Negozi. La prima riga del foglio Excel dovrebbe essere di intestazione, ed è quella che il comando utilizzerà per estrarre i nomi dei campi da creare nella nuova tabella. Se alcuni campi dovessero essere creati con un tipo diverso da quello desiderato (ad esempio float ancihè int, o varchar invece di nvarchar) sarà ovviamente possibile modificare lo schema della tabella direttamente da Management Studio Express dopo l'importazione.

Il comando però non funzionerà se le "Ad Hoc Distributed Queries" non sono abilitate. Per attivarle, eseguire i seguenti statement: 

sp_configure 'show advanced options',1
reconfigure with override
go
sp_configure 'Ad Hoc Distributed Queries',1
reconfigure with override
go 

Correntemente valutato 5.0 da 4 utenti

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Segnala:  
Tags:   , , , , ,
Categorie:   Sviluppo software
Azioni:   E-mail | Permalink | Commenti (20) | RSS CommentiRSS comment feed

Post correlati

Commenti

maggio 8. 2008 12.16

Alessandro

Siccome uso solo la versione Express questa dritta è utile ed interessante per me, e credo per molti altri.
Ho 2 domande da porti a vari propositi:
1) sai consigliarmi qualche testo, tutorial ecc. chiaro e completo su SqL Server 2005 (standard - express)?. Ho lacune da colmare ( prima o poi )
2) ci sarà un seguito a TheBeerHouse in c# 3.0 e NET 3.5?
grazie, e baci alla "bestia" da parte di Minnie (la mia)

Alessandro

maggio 8. 2008 16.16

Marco Bellinaso

Ciao Alessandro,

1) dipende da che area di SQL Server vuoi approfondire Smile TSQL? CLR Integration? Amministrazione? DB Design? Tuning? Ci sono libri dedicati per ciascuno di questi argomenti. Io di solito per scegliere un libro vado su Amazon, faccio una ricerca e vedo quali sono testi con il più alto numero di ottime recensioni Smile Non sarà un metodo infallibile, ma finora ha dato buoni risultati Smile

2) Ci dovrebbe essere, ma non si sa ancora la data...

P.S: Mescal ringrazia...e aspetta di vedere anche Minnie su Pet-files ;)

Marco Bellinaso

maggio 27. 2008 12.34

wiplock

ero interessato a importare in una tabella (non ancora esistente) il contenuto di un file excel ...

ma eseguendo

select *
into [XXXXX].[dbo].[Compleanni]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel8.0;Database=C:\Users\................\Desktop\COMPLEANNI.xls;HDR=YES', 'SELECT * FROM [Sheet1$]')

ho questo errore:

Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".


dove [XXXXX].[dbo].[Compleanni] è una tabella che non esiste... e vorrei che mi venisse creata
e C:\Users\................\Desktop\COMPLEANNI.xls è il path assoluto del file


devo fare qualcosa sui linked server ?? o configurare qualcosa per 'Microsoft.Jet.OLEDB.4.0' ??


grazie Tong

wiplock

maggio 27. 2008 12.53

wiplock

nb.
mi sono scordato di sottolineare il fatto che il file excel è sul mio desktop ...
mentre il db su cui devo scrivere è su un altra macchian a cui sono collegato con sqlserver management

wiplock

giugno 9. 2008 11.45

massimo

ciao, ho provato ad usare l'openrowset come dici letto nell'articolo ma mi da un errore del genere, hai idea di cosa può essere?

Il provider OLE DB "Microsoft.Jet.OLEDB.4.0" per il server collegato "(null)" ha restituito il messaggio "Impossibile trovare ISAM installabile.".
Messaggio 7303, livello 16, stato 1, riga 1
Impossibile inizializzare l'oggetto origine dei dati del provider OLE DB "Microsoft.Jet.OLEDB.4.0" per il server collegato "(null)".


ciao
grazie

massimo

dicembre 16. 2008 02.50

Busby SEO Test

Wanna say thank you for all the information on site site. Thanks

Busby SEO Test

gennaio 12. 2009 15.35

Massimiliano

Grazie mille, ha funzionato tutto a perfezione (SQL Server 2005 Express da cartella Excel formato 2003, su pc con Vista e Office 2007.... ci speravo poco e invece... mai dire mai)
Smile

Massimiliano

dicembre 6. 2009 03.46

faxless payday loans

Your blog is so informative … keep up the good work!!!!

faxless payday loans

dicembre 28. 2009 09.29

paydayloans

I just hope to have understood this the way it was meant

paydayloans

dicembre 30. 2009 02.15

Michigan Payday Loans

I like what I see. keep it going

Michigan Payday Loans

gennaio 3. 2010 15.26

quick loans

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

quick loans

gennaio 18. 2010 08.29

printing bellevue washington

Police begin campaign to run down jaywalkers

Safety Experts say school bus passengers should be belted

Drunk gets nine months in violin case

Juvenile court to try shooting defendant

Killer sentenced to die for second time in 10 years

Drunken drivers paid $1000 in '84

Stolen Painting Found By Tree

Judge To Rule On Nude Beach

Police Discover Crack In Australia

Men Recommend More Clubs For Wives

Two Convicts Evade Noose; Jury Hung

Deaf mute gets new hearing in killing


Regards
Lop








printing bellevue washington

gennaio 21. 2010 10.42

Cheap International Calls

One day an Englishman, a Scotsman, and an Irishman walked into a pub together. They each bought a pint of Guinness. Just as they were about to enjoy their creamy beverage, three flies landed in each of their pints, and were stuck in the thick head. The Englishman pushed his beer away in disgust. The Scotsman fished the fly out of his beer, and continued drinking it, as if nothing had happened. The Irishman, too, picked the fly out of his drink, held it out over the beer, and started yelling, "SPIT IT OUT, SPIT IT OUT, SPIT IT OUT!!!!"



Regards and respect
Burnett









Cheap International Calls

febbraio 13. 2010 11.08

teeth whitening reviews

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

teeth whitening reviews

febbraio 16. 2010 14.57

Herbal colon cleanse

There are certainly a lot of details like that to take into consideration.

Herbal colon cleanse

febbraio 22. 2010 11.59

Acne products

Interesting article. Were did you got all the information from? Anyway thank you for this great post!

Acne products

febbraio 27. 2010 19.42

cash advance

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

cash advance

marzo 6. 2010 16.22

no credit check loans

Hi Webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do! Big thanks for the useful info i found on Importare dati da Excel a SQL Server 2005 Express.

no credit check loans

marzo 9. 2010 10.49

replica watches

thanks for the share

replica watches

marzo 10. 2010 11.22

payday cash loans

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post. Big thanks for the useful info i found on Importare dati da Excel a SQL Server 2005 Express.

payday cash loans

Aggiungi commento


(Visualizza la tua icona Gravatar)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Anteprima

marzo 11. 2010 16.12