Save output of a SP to a file and create a job to execute it - sql

I have a SP which returns a XML string as output. I want to save the result in a .xml file automatically when the SP is executed. whats the best way to do that?

First, saving output to file:
exec xp_cmdshell 'bcp "select * from suppliers" queryout "c:\suppliers.txt" -S server -T'
Second, Scheduling a SQL Job
http://msdn.microsoft.com/en-us/library/ms190268.aspx
Alternatively (in case of SQL Express)
Use command-line SQL to execute the stored procedure from a windows task, scheduled accordingly
Build a quick .NET application that executes the stored procedure. Then, setup a windows task to run the executable on a schedule.

This might be exactly what you are looking for: http://munishbansal.wordpress.com/2009/02/20/saving-results-of-a-stored-procedure-into-a-xml-file/
In a nutshell, you have four options:
Using CLR Stored Procedure.
Using Command Line Utility (OSQL).
Using xp_CmdShell utility of SQL Server.
Creating OLE objects in SQL Server (sp_OACreate).
I will not paste most of that article here, but it is pretty well written.

Related

SQL Server 2005 install stored procedure

I'm trying to install the stored procedure WhoIsActive, but have no clue where to put it.
I thought it was supposed to go in the Bin folder but I believe it's only for DLLs. How do you install a stored procedure in SQL Server 2005?
I would like to call it using
Exec dbo.sp_WhoIsActive
since it has some parameters that I can use with it
How do you install a SP in SQL 2005?
You usually execute it within the query window.
That would invovle opening the stored procedure file (.sql) or copy / pasting it in, then pressing execute (or F5).
You might want to take a look at this article, and scripting stored procedures.

OUTFILE Microsoft SQL Server equivalent?

Is there something like INTO...OUTFILE (from MySQL) in Microsoft SQL Server?
This code causes the query results to be dumped into a text file.
EXEC master..xp_cmdshell'bcp "SELECT TOP 5 CUSTOMERID FROM Northwind.dbo.Customers" queryout "c:\text.txt" -c -T -x'
References:
Code Samples
BCP Utility Reference
xp_cmdshell Reference
You can also use the sqlcmd or osql (in SQL 2000) to store the output from a command line outside of SSMS very like working with mysql from the command line. You just have to provide the correct parameters, including the query you want to run.
It depends on how fancy you want to get. You can use SSMS to generate a CSV A less user-friendly option is a BCP export. Which I see #hamlin11 just suggested

SQL delphi ado, SQL batch execute

Since SQL Server does not have a simple batch command line executor for the scripts the are auto generated from management studio, I created one.
The problem arises when delphi ado syntax and SQL Server syntax don't agree (BUT ITS THE SAME THING).
Well any how, the go I replaced with ;
Now as I declare a stored procedure alter, I hit a brick wall.
The script I'm running is :
ALTER Procedure [dbo].[procName]
as
Declare #param int
and the error i get is :
the arguments are from the wrong type,
out of range or collide with one
another.
(my free translation)
questions :
why is this happening?
what can i do to change this?
is there another udl based program that parse SQL scripts?
thanks.
edit: require login to the db with udl file.
could it be that delphi has problems with # ?
Since SQL Server does not have a
simple batch command line executer for
the scripts the are auto generated
from management studio, I created one.
Are you aware of SQLCMD ?? Seems like a command-line utility to execute SQL scripts to me... also: the SQLCMD utility has a number of additional enhancements that go beyond what the T-SQL scripts in SSMS can do.
Also check out:
SQLCMD reference
Using SQLCMD utility
Not sure about your SQL example above, but does the stored procedure actually have any parameters, or are you calling a variable inside the body #param? The usual syntax is:
ALTER Procedure [dbo].[procName]
(<#params here>)
AS
<body + variables here>
MSDN - Alter Procedure
removed the component and change the code from
ado.open;
to
ado.execute;//or something like this
solved it.

No way to execute SQL script from SQL Server Query Manager like #{file.sql} oracle sqlplus syntax?

Like the title says, in oracle you can issue the following command in SQL*Plus:
SQL> select something from anothertable; #sql
SQL> #{/home/me/somescript.sql}; #load sql from file and execute it
SQL> do something else in script; #other sql
Without having to file->open the sql script to load it to the UI.
Is there an equivalent in SQL Server Query Manager? I've stumbled upon many situation where i could have used it but i couldn't be able to find a way to accomplish it.
You're not really comparing like for like Tools here.
The equivalent tool to SQL*Plus in SQL Server is the SQLCMD Utility.
In particular you will be interested in the -i switch as this allows you to provide a .sql file as input.
Edit:
In response to your comment, you could look to use the system stored procedure xp_cmdshell to launch a prompt form within a T-SQL batch that allows you to use SQLCMD. Not the most elegant solution in my opinion but it should work.
If using latter versions of SQL Server (2005 & 2008), see if the :r command in SQLCMD works for you:
:r <filename>
Parses additional Transact-SQL statements and sqlcmd commands from the file specified by <filename> into the statement cache.
If the file contains Transact-SQL statements that arenot followed by GO, you must enter GO on the line that follows :r.
From sqlcmd Utility
Use isql utility http://msdn.microsoft.com/en-us/library/aa214007(SQL.80).aspx
issql ... -iinputfile
There is also SQLS*Plus tool that you can use to execute scripts within a script

Stored procedure to script database objects to file?

I'd like to write a stored procedure in SQL 2005 to script all my database objects to a file. The only problem is...I have no idea how to do it. Can anyone help?
Thanks!
you can try something like this,
First of all creating a new store procedure in your SQL server, like this.
CREATE PROCEDURE ListObjectsToFile
AS
BEGIN
select * from sysobjects;
END
GO
And then call it from the Command Line, indicating your complete connection string and the path/name of the output file
C:\>sqlcmd -S
yourServer\yourSQLServerInstance -U yourUser -P yourUserPassword -q
ListObjectsToFile -o C:\list.txt
Hope that helps,
Santi! :)
I know your question says that you want a Stored Procedure to do the job done, but if you want an other solutions, I would be using SMO in VB.NET. When accessing database objects with the .NET objects, they all got a "Script()" function which returns the SQL to use to recreate the object. A table for instance.
Hope this might help
Try DBSourceTools. (http://dbsourcetools.codeplex.com)
Its open source, and specifically designed to script an entire database
- tables, views, procs and data to disk, and then re-create that database through a deployment target.