SQL Server - copy stored procedures from one db to another
- Right click on database
- Tasks
- Generate Scripts
- Select the objects you wish to script
- Script to File
- Run generated scripts against target database
How to transfer all stored procedures from one database to another with SQL Server Management Studio
Just to extend answer from @Anup Agarwal,
Right Click on the database name
--> click Generate Scripts..
--> click Next
--> choose Select specific data objects
--> check Stored Procedures
--> click Next
--> choose Save to new query window
--> click Next
--> click Finish
Now, change the database name to the new database name in the query window and execute.
Copying a stored procedure from one database to another
Instead of creating dynamic
script you could use one script with all the procedures
that you want to create (you can script all the procs you want using 2 click in SSMS
), you then run this script manually in the context of the database where you want to create these procedures or by passing the file with this script to sqlcmd
with -i
and passing the correct database name with -d
.
Here Use the sqlcmd Utility you can see the examples.
SQL: programmatically copy stored procedures from one db to another
You can do it programatically in .NET using the SMO framework.
Free/easy implementation could be done via PowerShell.
I have a script that does just this - it generates the scripts for SQL objects, including Stored Procs, and executes the creation scripts on the target server.
It's a handy workaround when security concerns don't allow linked servers but you need to keep certain resources in sync across multiple servers.
If all you care about are the procs, it should be fairly straightforward to check sys.sql_modules
on your source and target DBs and execute any that don't exist in the target via the definition
field in that view.
Copy stored procedure from one database to another using T-sql
The solution is to escape quotes properly and using ''' instead of only one quote
SQL Server: Copy Stored Procedures
In SQL 2005 and 2008 management: Right click on the database, choose tasks -> generate scripts. You should be able to follow the directions in the wizard from there.
In Sql 2000 you can actually just select the procedures you want to copy, ctrl+c to copy them, then paste them into a new query window for the other DB and run it.
How to copy tables , functions and stored procedures from one database to other in same Instance
You can script a backup/restore to do this, just restore to a different database name:
BACKUP DATABASE [DatabaseToCopy] TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\DatabaseToCopy.bak' WITH NOFORMAT, INIT, NAME = N'DatabaseToCopy-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10, CHECKSUM, CONTINUE_AFTER_ERROR
go
RESTORE DATABASE [DatabaseToCreate] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\DatabaseToCopy.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10
go
Just tweak the names and locations and create a sql server job to run it.
Related Topics
Oracle SQL: Fill in Missing Dates
SQL Efficient Way to Join a Table Where All Values Exist
How to Determine If a String Is Numeric in SQL
What Is the Internal Representation of Datetime in SQL Server
Order by with Inner Query, Giving Ora-00907 Missing Right Parenthesis
How to Return Two Columns with Function
How to Rewrite This SQL into Codeigniter's Active Records
Function to Get Number of Weekdays Between Two Dates Excluding Holidays
In VS or of Oracle, Which Faster
What Is the Problem with Foreign Key Cascade Multiple Paths and Cycles
Join a Count Query on Generate_Series() and Retrieve Null Values as '0'
Why am I Getting a "[Sql0802] Data Conversion of Data Mapping Error" Exception
Column Does Not Exist in the in Clause, But SQL Runs
SQL Set Default Not Working in Ms Access
How to Bulk Insert Only New Rows in Postresql
SQL Table Aliases - Good or Bad
How to Do "Where Exists" in Arel
Sql-Server Performance: What Is Faster, a Stored Procedure or a View