Sometimes you find these really old files floating around on your
harddrive and you forget that you ever downloaded them. Here is one
such example. I have no idea where I got this or who to credit for its
creation, but I’ve had it for awhile and came across it and thought it
would be something nice to share with you, as I’m sure it is something
of great help to many of you, especially if you are limited in your
experience on creating DTS packages, which is another way, and
preferred way under most circumstances, to get data from Sql to
Excel. This
is a T-SQL script that uses the system stored procedures sp_OA* for
creating and handling OLE objects, ADO, Jet and a linked server to
create and
populate an XLS file from a select statement. By default, if the
XLS file already exists, the result of the query will get appended to
the worksheet. You’ll have to add some code to check for and
delete the file before creating if that is your desired behavior.
Oh, and I used this a long time ago
with some minor code changes and it worked fine, but this is the
original script using the pubs database, so there are changes you’ll
have to make, and they should be fairly obvious to you.
Note: DTS packages are the
preferred way of handling this type of data transfer, especially when
scheduled, so don’t be hasty to implement this without looking at a DTS
solution first. That being said, I’m sure there are those of you
out there who can find usefulness out of this script.
Thanks Raymond & David, a very handy script. I have the same problem, loads of scripts from everywhere with no credit info, points out the usefulness of decent header comments.
Hello. my name is panxo i from chile south america,
i speak spanish
yo pienso que ustedes par de sacohueas estan puro weando, tienen que crear un objeto DTS y configurarlo para que exporte a EXCEL, despues lo llaman con
EXEC @hr = sp_OACreate ‘DTS.Package’, @object OUTPUT
EXEC @hr = sp_OAMethod @object, ‘LoadFromStorageFile’,
NULL, ‘c:\sd\Genera_Mega_SC.dts’, ”
y listo
ya pues escribamne cualquer cosa
a jaimealarcon@hotmail.com
xupenlo!
0×80004005 Microsoft JET Database Engine ‘C:\TEMP\Test_20060423.xls’ is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
“Here is one such example. I have no idea where I got this or who to credit for its creation, but I’ve had it for awhile and came across it and thought it would be something nice to share with you”
I did not know who the author was, as it had been sitting on my harddrive for so long. Thank you for pointing out the author.
Hey we have started contest to win Querycell excel add-on by which you can Generate SQL Script from Excel on a single click of mouse.
t3g906
This code works perfectly fine when data is populated for two or more columns.
It fails if data is populated for only one column.
Thanks Raymond & David, a very handy script. I have the same problem, loads of scripts from everywhere with no credit info, points out the usefulness of decent header comments.
You are right – I did find this to be of great interest. Thanks for sharing it.
Hello. my name is panxo i from chile south america,
i speak spanish
yo pienso que ustedes par de sacohueas estan puro weando, tienen que crear un objeto DTS y configurarlo para que exporte a EXCEL, despues lo llaman con
EXEC @hr = sp_OACreate ‘DTS.Package’, @object OUTPUT
EXEC @hr = sp_OAMethod @object, ‘LoadFromStorageFile’,
NULL, ‘c:\sd\Genera_Mega_SC.dts’, ”
y listo
ya pues escribamne cualquer cosa
a
jaimealarcon@hotmail.com
xupenlo!
What is the reason of this Error ….
0×80004005 Microsoft JET Database Engine ‘C:\TEMP\Test_20060423.xls’ is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
As noted in my blog post:
“Here is one such example. I have no idea where I got this or who to credit for its creation, but I’ve had it for awhile and came across it and thought it would be something nice to share with you”
I did not know who the author was, as it had been sitting on my harddrive for so long. Thank you for pointing out the author.
So who is author? you or David A. Long ?
http://www.sqlservercentral.com/scripts/contributions/763.asp