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.
Related
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.
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.
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
I want to call a sproc on server B from server A in TSQL without linking the servers. Is it possible to use something like a connection string to execute this sproc? The return will be a single nvarchar value.
Regards.
To avoid "linked servers", you'd normally use OPENDATASOURCE
After comment:
EXEC OPENDATASOURCE('SQLNCLI', 'Data Source=London\Payroll;Integrated Security=SSPI').remoteDB.remoteSchema.remoteProc #param1, #param2,...
Simple 4 part naming convention. The whole OPENDATASOURCE simply replaces the linked server name...
Note: you may have issues with "adhoc access"
i know of no way of doing it without ...
creating an extended stored proc to do it for you
perhaps using xp_cmdshell to use isql to execute your stored proc .. however, getting the result might be tricky (perhaps write the result to a table on your current server in the same sql file that isql is reading)
-don
Can anyone help me how to restore a database from vb.net,I tried to restore using stored procedure by taking retore template script from sql server2005. but there is error "the database is already in use please use a master database.."
I assume you used the same connection string you usually use to connect to the database you are actually restoring.
From you error message, I'd say you should create your connection to the server with a different InitialCatalog parameter (the error message indicates you should use "master").
The other option is to stick a "USE master" at the beginning of the script. Here is a small description of the USE statement.
From the error it doesn't look like there is an actual VB.net programmatic error.
It seems like the problem lies on the database restoration stored procedure.
Before restoring database, make sure that
• Your connection is not using the database you are restoring - That is one of the possible reasons for that error, "the database is already in use please use a master database.."
• Other connections to the target database should be closed off - Close all connection to it.
Denis Troller has mentioned "USE master" and make sure that your script has that statement as the very "first" statement in your restore batch script.
I just wrote this in my project so I thought I'd share my method.
I'm calling the backup and restore by firing the SQL at the server using an SqlCommand.CommandText and setting the SqlParameters for database and filename as follows:
Simple backup:
BACKUP DATABASE #dbName
TO DISK=#fileName
WITH FORMAT
Then restore it using :
USE master
RESTORE DATABASE #dbName
FROM DISK = #fileName
There are plethora of options on the BACKUP and RESTORE commands but I just wanted a quick sledge-hammer approach and this works nicely.
Thanks to Denis for the 'USE master' tip, which just fixed my 'in use' error!