How to generate multiple Alter Scripts in SSMS - sql

I'm using sql server management studio 2008 to try and generate an alter script for each of my stored procedures in order to save the scripts for each revision. I can easily generate an alter script for each individual procedure, but I'm not trying to go through a hundred stored procedures manually.
I know that SSMS has an automated generate scripts function under task,
but the only options are create, drop and create, and drop.
I cant seem to figure out how to enable alter. I've already searched through many SO articles, as well as a little digging in msdn, and I've come up with nothing.
I'm hoping that the fine people of stackoverflow will be up to the challenge.
Thanks in advance

Use CHECK FOR OBJECT EXISTENCE option in Advanced Scripting Options.
Script will contain set of IF NOT EXISTS... CREATE commands and below ALTER for each stored procedure you wanted to script.

It's not a very elegant solution but it would definitely work. Why not generate create script and then just replace all occurrences of CREATE PROCEDURE with ALTER PROCEDURE.

You can generate stored procedures automatically from SQL SERVER Management Studio as following:
1) Right click on your database -> Tasks -> Generate Scripts
2) Select "specific database objects" then choose tables / stored procedures you want to generate script for them then press "Next"
3) In this window you can choose where you want to save your script, then you will find an option "Advanced", click it. Then you will find an option "Script DROP and Create",there are three options: Create, Drop, Drop and Create. Choose one as you want.
4) Then press ok, then "Next" and the script will be generated automatically.
If you want to change it from Create to Alter, just do "replace all" operation using any text editor.
Hope this answer helps others.

Well, Drop/Create is the same as alter. By stating that you would like to use alter then you are certain that the target objects exists. Why not just select the group from the object explorer and right click select DROP/Create. This should do the same.

Related

How do you save a CREATE VIEW statement?

EDIT: This question was based on the incorrect premise that SQL VIEWS were cleared from a database when the user that created them disconnects from the server. Leaving this question in existence in case others have that assumption.
I'm trying to use views in my database, but I'm running up against an inability to save the code as a SQL Server object for repeated use.
I tried saving CREATE VIEW statements as procedures and user defined functions, but as many have answered on stack overflow, CREATE PROCEDURE and CREATE FUNCTION are incompatible with CREATE VIEW due to the only one in batch issue.
Obviously I don't want to retype my CREATE VIEW statements every time, and I'd prefer not to have to load them from text files. I must be missing something here.
You don't really "save" CREATE/ALTER statements. The create or alter statement changes the structure of the database. You can use SSMS to generate the statement again later by right clicking on the view, and choosing Script as->Create. This inspects the structure of the database and generates the statement.
The problem with this approach is your database now consists of both a structure definition(DDL) as well as its contents, the data. If you dropped/created the database to clear its data, you'd also have lost the structure. So you always need a database hanging around for the structure and back it up to ensure you don't ever lose the DDL.
Personally I would use Database Projects as part of Visual Studio and SQL Server Data Tools. This allows you to keep each View, Table, etc. as separate files, and then update the database using schema compare. The main benefit being you can separate the definition of the database from the database itself, and also source control or backup the DDL files.
If you really want to, you could create a view in a proc like this:
CREATE PROCEDURE uspCreateView AS
EXEC('CREATE VIEW... ')
Though, you'll have to escape single quotes in your view code with ''
However, I have to agree with the other comments that this seems like a strange thing to do.
Some other thoughts:
You can use sp_helptext to get the code of an existing view:
sp_helptext '<your view name here>'
Also, INFORMATION_SCHEMA.VIEWS includes a VIEW_DEFINITION column with the same code:
SELECT * FROM INFORMATION_SCHEMA.VIEWS

How to find an object in all T-SQL scripts

I have an ERP Database and it is big. One of the table gets updated by an SP, TRIGGER, FUNCTION or something else. Because, I watched the Profiler to find UPDATE or INSERT statements but I couldn't find ant UPDATE or INSERT. Therefore, the table should be updated by SP, TRIGGER, FUNCTION or something else.
Is there a helper to find in which SP,FUNCTION OR TRIGGERS the table is used? I want to give the table name and it will tell me where the table name is used?
In SSMS do the following
Server->Database->Tables-> tablename ->right click -> view
dependencies
select Object that depends on radio button to view the object's that were using your table
Export all script objects to a file and search the file. You can do this from SQL Server Management Studio. Right Click the database and go to Tasks > Generate Scripts.
In SSMS you can right click a table and then choose 'view dependencies' or use sp_depends.

HSQLDB table not found using script to create table and insert data

I am using the GUI (hsqldb.jar) in HSQLDB 2.2.9 to create a DB. I have all the SQL commands in a separate text file. So to create the DB, I just copy the text and paste into the HSQLDB editor and hit the "Execute SQL" button. I have successfully created my DB several times with different revisions, each time executing the CREATE TABLE commands with one press of the "Execute SQL" button, and the INSERT INTO commands with a subsequent press of the "Execute SQL" button. This works, but it would be more convenient to execute both the CREATE and the INSERT commands at the same time. I've tried to combine these into one "Execute SQL", but I keep getting this error:
user lacks privilege or object not found: SHOP / Error Code: -5501 / State: 42501
Here's what I've tried:
CREATE TABLE Shop (
Id int NOT NULL IDENTITY,
Name varchar(255) NOT NULL,
UNIQUE (Name)
)
INSERT INTO Shop VALUES (
NULL,
'Test Shop'
)
Note that this exact same code works if I execute the SQL in two separate steps. I've tried putting COMMIT between the CREATE and the INSERT commands, as well as CHECKPOINT, but neither of these solved the problem. I also tried adding SET WRITE_DELAY FALSE at the top, but this didn't solve it either.
What do I need to add to this code to make it work in one step? Thanks!
This is not possible.
The GUI client sends all the text in the window to the database engine. The engine compiles all the statements before executing them. Because of this, if a statement relies on the completion of a previous statement, it won't compile.
The better way to populate your database from a script is the SqlFile tool which is part of SqlTool.jar. This tool executes statements one by one.
The separate Guide is here:
http://hsqldb.org/doc/2.0/util-guide/sqltool-chapt.html

SQL drop table and re-create and keep data

On our original design we screwed up a foreign key constraint in our table. Now that the table is full of data we cannot change it without dropping all of the records in the table. The only solution I could think of is to create a backup table and put all of the records in there, then delete all the records, alter the table and start adding them back. Any other (BETTER) ideas? Thanks!
Using MS SQL Server
I'm a bit late, just for reference.
If You are using SQL Server Management Studio, You could generate a DROP and RECREATE script with "Keep schema and data" option.
Right click on the desired DB in object explorer
Tasks > Generate scripts
Select the table you want to script
Then clicking on Advanced button
"Script DROP and CREATE" ->"Script DROP and CREATE"
"Types of data to script" -> "Schema and data"
Hope this helps
Here's some pseudo-code. No need to make a backup table, just make a new table with the right constraint, insert your records into it, and rename.
CREATE TABLE MyTable_2
(...field definitions)
<add the constraint to MyTable_2>
INSERT INTO MyTable_2 (fields)
SELECT fields
FROM MyTable
DROP TABLE MyTable
exec sp_rename 'MyTable2', 'Mytable'
Using SQL Server Management Studio (SSMS), you can use it's table designer to specify the final condition of the table. Before saving the changes, have it generate the change script and save that script. Cancel out of the design window, open the script and review it. SSMS may already have generated a script that does everything you need, fixing the primary-foreign key relationship while preserving all existing data. If not, you will have a script, already started, that performs most of what you need to do and should be able to modify it for your needs.
This is your only solution.
Create the backup table, empty the original one, modify the table and then insert step-by-step until you find a violation.
Update All Schema Database Old by new Schema Database .
Create script (Right click on the desired DB in object explorer Tasks > Generate scripts -> select option select specific database objects and tables ->next -> advanced-> option Type of data to script Data only -> ok ->next ->next.) to data only and backup Database to old database
Drop database old and create new database and make new DB is empty .
Excute script of Old Data only on new database .

SQL Server Index Scripting

I'm using SQL Server 2008.
Is it possible to create a script to loop over all tables in a database generating a set of index drop scripts and create scripts separately?
What I have to do is drop all indexes on a set of databases to run a heavy data load process but then I want to re-enable all the indexes. I don't want to have to go through each table and script an index drop and then index create.
I think what you want to do is here. You don't really need to drop the indexes, just disable them while your load is occurring. This script will allow you to disable the indexes for your load, then re-enable when done, and the beauty is it does it all in one shot for the whole database with a single command.
Yes. Someone's already done it so no need to write a script again... :-)
Drop All Indexes and Stats in one Script
Edit. Oops. To create...
Before you drop them... In SSMS, right-click, generate scripts, etc
You could do it with SMO, take a look at SMO Script Index and FK's on a database