Changing Database Name In Stored Procedures - sql

I need to change the database name in SQL SERVER 2008 and use it in another project. However it consist hundreds of stored procedures and the name of the database should be changed in the stored procedures as well. Is there any way to do this?

If you right mouse click a Database and choose Tasks->Generate Scripts. Go through the Wizard and it will create a SQL script for you. Make sure that you select all of the necessary options e.g. Create Database, Stored Procedures etc. Once finished, you'll have a big script. Find and replace the database name.

I would suggest that you export all your stored procedures as sql-files and then take a nice texteditor (like Notepad++) and make a file-search&replace-action to change all the names referenced inside the sql-files.
There is no other way around as far as I can say :-(

I'd make next thing: I'd generate all scripts for database by Management Studio(Right Click on DB -> Tasks -> Generate scripts), after that I'd replace name of database (Ctrl + H).

Right click on the database, Tasks -> Generate scripts, select stored procedure, and open the scripts in sql editor.
Search and replace the database or string names.
replace Create procedure with Alter procedure.
Compile and done.

There is no way of doing this automatically. You would have to manually change every stored procedure.
Do you really need to change the name?

Wouldn't it be better to keep everything the same and use a different server or server instance? Are you going to have to maintain database changes through these two datbases with the different names?

Related

Dropping and recreating database, along with tables

I have a database whose structure I'm happy with, but which has a fair amount of dummy data in it. I would like to drop and recreate the database while at the same time wiping out the tables, but retain the table structures and relationships.
When I right-click on the database and choose to script 'drop and create' statements they're for the database itself, but no mention of the tables within-- unless I'm missing something.
Is it possible to generate a script that drops/creates a database and the tables within? I can individually select each table and script out their drop/create statements and order things so it will work, but are there other ways of doing this in one swoop?
I have a localized version of Sql Server 2008 R2, so some of my instructions could be imprecise.
I hope that there are no big differences.
Right click on your database and select Tasks and then Generate Scripts
Leave the first option selected (Build script for all db and objects) and select next
On the second page click Advanced
Select the option to Build Script for DROP & CREATE
Select the option to Use only the Schema (not Schema and Data)
Check the other options you would like to use.
Finally choose your save options and click Next until SSMS creates the script
Instead of that you should do this. That way you can select different tables or stored procedure to script for. See MSDN How to: Generate a Script (SQL Server Management Studio) on how to do it step-by-step.
Right click on DB_Name -> select tasks -> Generate Scripts

Tools to delete and recreate a lot of stored proc in SQL Server 2005

We have a database that had a lot of changed in both tables and Stored proc. Dealing with the tables isn't too bad since we only had a handful of changed, but there is something like 85 or more SP that needed to be changed/create. Is there a quick way to take all the SP from one database into another one, assuming the table definitions are the same, which they are in my case
Thanks,
You could use a product like SQL Compare (http://www.red-gate.com/products/sql-development/sql-compare/.
You can also use SQL Management Studio to script the procedures from one database and run those scripts against the other database. Right click the database -> Tasks -> Generate Scripts.

Is there Microsoft SQL Mangement studio Edit procedure command

I have around 50 databases, all identical structure. Now I have to edit one procedure, so the process is a bit complex:
expand database,
expand programmability
expand stored procedures
right click on procedure
select modify
What I want to know: Is there a command that will open the modify window for a specific procedure so that my process will just be
select database
CTRL+E to execute command
When I say command I mean text in editor window, something like this
edit procedure 'my_procedure'
Maybe I misunderstand your question... but...
If you want to use SQL Server Management Studio:
Could you simply copy the ALTER statement, paste it in a new window, change the database that window is connected to, execute, change database, execute, etc.
If you want to do it faster and more repeatable, you could just save the query to a file and write a script in cmd using sqlcmd to run that script on each of your 50 databases.
http://msdn.microsoft.com/en-us/library/ms162773.aspx
I can't imagine managing 50 databases w/ the same structure and not using scripting...

Create SQL script that create database and tables

I have a SQL database and tables that I would like to replicate in another SQL Server. I would like to create a SQL script that creates the database and tables in a single script.
I can create "Create" script using the SQL Management Studio for each case (Database and Tables), but I would like to know if combining the both "Create" scripts into single script would be enough.
Thanks.
Although Clayton's answer will get you there (eventually), in SQL2005/2008/R2/2012 you have a far easier option:
Right-click on the Database, select Tasks and then Generate Scripts, which will launch the Script Wizard. This allows you to generate a single script that can recreate the full database including table/indexes & constraints/stored procedures/functions/users/etc. There are a multitude of options that you can configure to customise the output, but most of it is self explanatory.
If you are happy with the default options, you can do the whole job in a matter of seconds.
If you want to recreate the data in the database (as a series of INSERTS) I'd also recommend SSMS Tools Pack (Free for SQL 2008 version, Paid for SQL 2012 version).
In SQL Server Management Studio you can right click on the database you want to replicate, and select "Script Database as" to have the tool create the appropriate SQL file to replicate that database on another server. You can repeat this process for each table you want to create, and then merge the files into a single SQL file. Don't forget to add a using statement after you create your Database but prior to any table creation.
In more recent versions of SQL Server you can get this in one file in SSMS.
Right click a database.
Tasks
Generate Scripts
This will launch a wizard where you can script the entire database or just portions. There does not appear to be a T-SQL way of doing this.
An excellent explanation can be found here: Generate script in SQL Server Management Studio
Courtesy Ali Issa Here's what you have to do:
Right click the database (not the table) and select tasks --> generate scripts
Next --> select the requested table/tables (from select specific database objects)
Next --> click advanced --> types of data to script = schema and data
If you want to create a script that just generates the tables (no data) you can skip the advanced part of the instructions!
Not sure why SSMS doesn’t take into account execution order but it just doesn’t. This is not an issue for small databases but what if your database has 200 objects? In that case order of execution does matter because it’s not really easy to go through all of these.
For unordered scripts generated by SSMS you can go following
a) Execute script (some objects will be inserted some wont, there will be some errors)
b) Remove all objects from the script that have been added to database
c) Go back to a) until everything is eventually executed
Alternative option is to use third party tool such as ApexSQL Script or any other tools already mentioned in this thread (SSMS toolpack, Red Gate and others).
All of these will take care of the dependencies for you and save you even more time.
Yes, you can add as many SQL statements into a single script as you wish. Just one thing to note: the order matters. You can't INSERT into a table until you CREATE it; you can't set a foreign key until the primary key is inserted.

How can I view the original SQL that created a stored procedure in SQL Server 2008?

The title pretty much says it all.
How can I view the original SQL that created a stored procedure in SQL Server 2008?
Is this possible? I've been searching online for some leads, but I'm either missing correct vernacular or I'm just looking for something that can be found by some other means.
My basic problem is that I've got a SQL Server 2008 db here with a couple hundred stored procedures and I want to see what they are doing. I need to copy one and modify it slightly and then use it.
Open up management studio and expand the database you are after. Inside of there is a programmability folder, expand that and you will see the stored procedures. Right click on one of them and select modify.
From a query window on the db you can execute sp_helptext YOURPROCEDURENAME It's a shorthand for what Martin described.
To get the definition
select object_definition(object_id('sp_help'))
Or in management studio right click the procedure and choose a scripting option.
As long as it was not encrypted sp_helptext is the stored procured you want to show the text of any stored procedure
Of course if you were storing your sps in your source control as you should be doing, you would go there and look at it and even be able to see previous versions.
For any of the answers given so far, if there was any set up done - to create a #temp table that the proc depends on, for example - that won't exist in the results because SS stores the functional code for the proc definition, not all of the SQL used in the creation. Some things you might have to infer.