Copy store procedure to another database?

Posted on 2004-10-17
Last Modified: 2008-02-20
I have some store procedure in a database. I usually have to create new database and have to create the sp above into new database. Can you tell me how to automate all that work:
- create new database
- copy some sp from old db to new db

I have SQL2K and coding all in Visual Basic.


Question by:tungduong

Expert Comment

ID: 12335878
You want a complete copy of the database, with all tables, Stored Procedures and data? Or an empty database with all tables and SPs? Or something else?

To get all tables, Stored procedures and data, you can backup the database in enterprise manager then restore it under the new database name. Let us know if you need some further help with that.

If you want an empty database, then what you want to do is script the database in enterprise manager, then save that script. Change the name of the database in the CREATE DATABASE part of the script before you run it. Again, if you need detailed instructions, post back and we can help.
LVL 34

Accepted Solution

arbert earned 125 total points
ID: 12335979
To expand a bit, if you want to create a script,  Open enterprise manager, right click on your database, Choose All Tasks, Generate SQL Scripts.  You can choose to script the database and choose the stored procs you want to script.

So, if you wanted to code it from VB, you could use the above script and execute it against an ADO connection object.

LVL 11

Expert Comment

ID: 12336649
If you REALLY want to do this in VB (Its only a few Clicks in EM like arbert said) you should look at the SQLDMO object which is installed when you install the SQL-Server-Connectivity.

This object allows you to list all SP's and Tabels in an SQL Server (well actually you can do anything you want in it with it....) I think it can also generate the code you need to script the SP and tabels. But i would not go this way since it is so easy in EM :=)

Author Comment

ID: 12337061
Thanks t all of help.

My problem is I have database with meny table and store procedure in it. Each month I have to create new database and want to restore all store procedure and some table (not all the tables). The store procedure then may be modify by the SQL Server admin (it rearly change) With the table I can excute command
to transfer data to new database. The problem is I dont know how to do with the store procedures.

LVL 34

Expert Comment

ID: 12337924
"Each month I have to create new database"

Sounds like a maintenance nightmare....
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.


Author Comment

ID: 12343467
I've heard that sql_helptext can solve the problem. So can you tell me how to do with it in VB6?
LVL 34

Expert Comment

ID: 12343879
You mean sp_helptext?

sp_helptext just returns the SQL for a routine/proc or the text of a view.  You could use it in VB--simply execute it with the connection object and pass the "object" as a parameter for which you want the text back (you can assign a recordset to get the results).

set rs=conn.execute ("sp_helptext 'yourstoredprocnamehere'")

Author Comment

ID: 12344111
Thanks, arbert. I can you tell me how to run .sql script file from visual basic if I use your first guide to generate a script which content all of my store procedure.
I save script to name: create_sp.sql
I try to run with command:
gConn.Excute("EXEC ") & App.Path & "\create_sp.sql"
But it failed.

LVL 34

Expert Comment

ID: 12344461
You would actually have to read the contents of the file into a variable and exec that....the execute method (you have .Excute, but I assume that's a typo) won't read a file...

Author Comment

ID: 12355797
I did as you said, arbert, but it fial to excute. You now there are much of quote (') mark in the store procedure.

How can I do?

Author Comment

ID: 12366066
Well, thanks alot. I can do it now.

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Full Text Search string 5 33
c# code 19 59
SQL Script to find duplicates 16 19
How many Lat/Long for a single UK Postcode 5 12
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 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

14 Experts available now in Live!

Get 1:1 Help Now