ms sql stored procedure

hi can you show me how to create a stored procedure that export data with the header into ms excel and schedule to run
daily at 9:00 pm ?

Assume the select query is like select firstname from person

Thanks
LVL 1
ITsolutionWizardAsked:
Who is Participating?
 
Kyle AbrahamsSenior .Net DeveloperCommented:
from:
https://www.simple-talk.com/blogs/sending-query-results-to-excel-through-e-mail/
declare @qry varchar(8000)
declare @column1name varchar(50)
-- Create the column name with the instrucation in a variable
SET @Column1Name = '[sep=,' + CHAR(13) + CHAR(10) + 'customerid]'
 
-- Create the query, concatenating the column name as an alias
select @qry='set nocount on;select customerid ' + @column1name + 
             ' ,companyname, contactname, country from customers'
 
-- Send the e-mail with the query results in attach
exec msdb.dbo.sp_send_dbmail @recipients="Your email",
@query=@qry,
@subject='Client list',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'result.csv',
@query_result_separator=',',@query_result_width =32767,
@query_result_no_padding=1

Open in new window


You would then use SQL Agent to schedule a task to run at 9:00 PM.
0
 
Ryan ChongCommented:
you probably can do it using SQL Server Integration Services (SSIS).

as a start, you can read this article:

Using SSIS to Export Data to Excel
http://knowlton-group.com/using-ssis-to-export-data-to-excel/
0
 
ITsolutionWizardAuthor Commented:
If i do not want to use sql agent to schedule, what can i do?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Kyle AbrahamsSenior .Net DeveloperCommented:
You need something to kick it off.  Sql Agent makes sense.  If not it'll need to be a windows service or a program and a scheduled task.
0
 
Pawan KumarDatabase ExpertCommented:
You can create a Windows Service in that case.
0
 
tliottaCommented:
If i do not want to use sql agent to schedule, what can i do?
Use a different scheduling tool.

What do you have available? If we don't know what you have, we can only make random suggestions.
0
 
Pawan KumarDatabase ExpertCommented:
Create a SP in that perform operations you want to do and call it via SQL Job.

SQL Job you can schedule , you can also get alert on completion and you will also get alert in case it fails.
So tracking in this case will be very easy.
0
 
ITsolutionWizardAuthor Commented:
Again, I do not want to use SQL Job and window service.
Is it a way I can use stored procedure straight forward?
0
 
Pawan KumarDatabase ExpertCommented:
In that case you can run it manually !!
0
 
ITsolutionWizardAuthor Commented:
that does not solve my problem...
0
 
Ryan ChongCommented:
if you want a piece of codes or a program to run automatically on a designated timing in a machine, it means you need to automate by allowing another listener program to trigger it.

How to automate it? I think some of us already given you suggestions here.
0
 
Pawan KumarDatabase ExpertCommented:
0
 
ITsolutionWizardAuthor Commented:
no
0
 
ZberteocCommented:
If you can't or don't want to use SQL agent or windows scheduler the only thing left is for you to build one. Basically the simpllest way would be to create a VB script that will run in an infinite loop, check every time what time it is and if the time is right execute the stored procedure from that script. In that case you don't even need a stored procedure as you can manipulate the results and save them into an excel file using the VB script itself.

Execute from vb script:

https://www.experts-exchange.com/questions/28310743/Run-a-SQL-Stored-Procedure-from-a-vbs-File-and-return-a-parameter.html

Write to excel:
http://www.geekshangout.com/an-example-of-using-a-vbs-script-to-create-populate-and-format-an-excel-document/
0
 
ITsolutionWizardAuthor Commented:
so you are telling me to use classic asp to get this task completed?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Is there any specific reason you can't use sql agent?  It makes the most sense in terms of scheduling.
0
 
ITsolutionWizardAuthor Commented:
Our sql agent is not working and we don't want to touch it
0
 
ZberteocCommented:
@Jim Horh:
Gentlemen - Please refrain from posting links-only or links-mostly answers.  Thanks in advance.

Three things:

1. My post is not at all "links-only or links-mostly". I give explanations what the alternatives are considering the conditions

2. One of the links is to experts-exchange and the other is just for a standard procedure to use vbScript to write to Excel!

3. I will not waste my time to just re-type or copy-paste things for things that are common knowledge. I am aware of the EE rules and I am not breaking them. The question was not about how to write in Excel using vbScript so I merely made a suggestion based on what the answers evolved to...
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Zberteoc - The comment was intended for multiple experts, as I'm counting four comments with links and various levels of completeness.  And please spell my name correctly.

Thanks in advance.
0
 
ZberteocCommented:
Sorry for misspelling your name, which was obviously unintended,  but I am surprised to see that you make a case of it...

Do links to to https://technet.microsoft.com or even other sites that are not EE competition but simply articles describing a procedure not allowed either? I mean there should be at least some tolerance in situations like these. We are giving our answers benevolently here, for free but at time cost to us, so can we at least have the right to not waste our time reinventing the wheel?

I think we are making little bit of a big deal about this, here!
0
 
ZberteocCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.