Solved

ms sql stored procedure

Posted on 2016-11-03
22
74 Views
Last Modified: 2016-11-15
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
0
Comment
Question by:ITsolutionWizard
  • 6
  • 4
  • 4
  • +4
22 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
 

Author Comment

by:ITsolutionWizard
Comment Utility
If i do not want to use sql agent to schedule, what can i do?
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
You can create a Windows Service in that case.
0
 
LVL 27

Expert Comment

by:tliotta
Comment Utility
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
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
 

Author Comment

by:ITsolutionWizard
Comment Utility
Again, I do not want to use SQL Job and window service.
Is it a way I can use stored procedure straight forward?
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
In that case you can run it manually !!
0
 

Author Comment

by:ITsolutionWizard
Comment Utility
that does not solve my problem...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
0
 

Author Comment

by:ITsolutionWizard
Comment Utility
no
0
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
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
 

Author Comment

by:ITsolutionWizard
Comment Utility
so you are telling me to use classic asp to get this task completed?
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
Is there any specific reason you can't use sql agent?  It makes the most sense in terms of scheduling.
0
 

Author Comment

by:ITsolutionWizard
Comment Utility
Our sql agent is not working and we don't want to touch it
0
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
@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
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
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
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now