Solved

How to make a scheduled query

Posted on 2004-03-26
6
175 Views
Last Modified: 2010-07-27
Is there a way to do the following?: I need a simple count query to automatically run once a day and save the resulting text to a textfile (or preferably a html document...).  Please put up an example.

Thanks.
0
Comment
Question by:fabbo77
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:dishanf
ID: 10685430
Hi fabbo77,

Yes. You can do this. Create a Job and sechdule it.

Cheers!
DishanF.
0
 
LVL 8

Expert Comment

by:dishanf
ID: 10685477
fabbo77,

BCP works ok if you want to store the contents of a table to text file. If you want the contents of a stored procedure it gets complicated. In summary, you must use the stored procedure to fill a table, then use BCP to export the table, and finally you need to clear the table. I wish there was a better way!

Wait. There is and it's called OSQL. OSQL is a command line program that allows you to run SQL Statements. Let's look at the simplest form:

osql /U sa /P password /d pubs /S Server9 /Q "Select * from Authors"

Remember you are typing this into the operating system (command prompt) and NOT Query Analyzer. This will connect to the pubs database on Server9 and run the query Select * from Authors and output the results back to stdout (which is the screen unless you redirect it). And the arguement headers (-U, -P, etc.) are case sensitive. Go figure.

You wanted to get the result of a stored procedure. You can do that like this:

osql /U sa /P password /d pubs /S Server9 /Q "sp_help"

Now we need to save this in a file. The simplest way is like this:

osql /U sa /P password /d pubs /S Server9 /Q "sp_help" -o ofile.txt

This will store the results in a file called ofile.txt. You could store them in a Unicode file using -u ofile.txt. You can also embed the path information in the file name using quotes.

There are a couple of other cool things you can do with this. It's really easy to put the SQL statement you want to run in an environment variable using a batch file. You batch file will look something like this:

set mysqlcommand=sp_help
osql /U sa /P password /d pubs /S Server9 /Q "%mysqlcommand%"

This makes it pretty easy to build a batch system. You can also capture the SQL Server result code at the operating system level using the -b flag.

Other popular settings include disabling headers, setting the row width, using trusted connections and input redirection. With input redirection you can put a series of SQL commands into a file and execute them. Books Online has quite a bit more detail on OSQL if you're so inclined.

http://www.sqlteam.com/item.asp?ItemID=744

HTH
DishanF.
0
 
LVL 8

Expert Comment

by:dishanf
ID: 10685482
fabbo77,

place following statement in sql server job

EXEC master..xp_cmdshell 'osql /U sa /P password /d DatabaseName /S ServerName /Q "SELECT COUNT(1) AS Count FROM TableName" -o ofile.txt'


HTH
DishanF.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:fabbo77
ID: 10687943
DishanF,

Thanks for the reply. Your script Works nicely. But the text is horribly corrupted when viewing in a text editor. Any tips, or should I live with it? :)

Best regards,
Janne
0
 
LVL 34

Accepted Solution

by:
arbert earned 250 total points
ID: 10688627
If you're going to make an HTML file, take a look at the SQL Web Task.  In enterprise mangaer, go to the tools menu, Wizards, Management, Web Assistant Wizard.

If will create the web page for you and schedule it to run....

Brett
0
 

Author Comment

by:fabbo77
ID: 10694767
Thanks Brett. This was exactly what I wanted. :)
Thanks also to DishanF for his answers.

Cheers,
Janne
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 31
VB.net and sql server 4 36
sql server tables from access 18 19
Proper Case SQL Command 2 10
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

778 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