Generate a "Create Table" script in Toad / SQL Navigator - sql-server

I know that in SQL Server Management Studio you have the ability to right click on any table and are able to select Script Table As > CREATE To and are then able to get the entire create statement for any object in your DB.
Does anyone know if there's a similar functionality in Toad / SQL Navigator?

Although #Gary's answer above works in Toad, just for the sake of completeness, I wanted to mention that in SQL Navigator, I couldn't get it working as-is and, so, managed to do it using:
Extract DDL > Code Editor > SQL Scripts
Hope this helps someone else too..

In Toad, select your table in the object navigator then right-click and select "Create Script".
There are various options to select that control what is included in that script too.

Related

How do I export my views from a database?

I have a number of views in my SQL Server database.
How do I export these as CREATE VIEW scripts?
I tried to right click the database - script database as - create to - new query editor window, but it doesn't show my my views (or tables even for that matter).
Any ideas?
In SSMS, if you right click the DB -> Tasks -> Generate Scripts... - that will take you through a wizard.
You can run through the wizard and select your DB, then the views as AdaTheDev mentioned or you can right click on each query and generate a script directly.
I prefer another way, because always I edit the views to remove "not necessary script generated SQL syntax" (depends on the point of view :)).
Right click the view, Generate script, CREATE in, new window.
See screenshot below (sorry for german version, but should work anyway).
I tried to right click the db - script database as - create to - new query editor window, but it doesn't show my my views (or tables even for that matter).
The purpose of the menu you mentioned is only to create the empty database, without any tables, views or anything else.
If you want to script anything beyond the empty database, you have to use the wizard already mentioned in AdaTheDev's answer.
Maybe you receive empty page because source of VIEW was encrypted or removed.
In older SQL editions there was a trick - after creating VIEW (SQL Server compiled it) developer could remove source of VIEW statement to protect it from "deassemblation". In current editions there is possibility to encrypt source of VIEW statement.

How to view Stored Procedures in Squirrel

Would like to know if one can view stored proc is Squirrel. Is there a plugin for this?
What version of SQuirreL are you working on ?..
I have 3.2.1 and all I need to do is click on "Objects" tab, expand your connection, look for your database, expand it, and then look for "Procedure" then click on the procedure you want, and then click on the "Source" tab
You have to install your correspondent database plugin. In case of sqlserver, you can install it with squirrel-sql installation by checking Microsoft SQL-Server Plugin as per below:
.
So once installed, you can:
Right click on the stored procedure in the left menu,
Select sql server from the drop down menu,
Then select script procedure from the sub-menu
Here is a screenshot:
An easy way to get Stored Procedures source code on Ingres database:
select
procedure_name, text_sequence, text_segment
from
iiprocedures
where
procedure_name = 'sp_name_goes_here'
order by
text_sequence
Aloha!
Using SQuirreL 3.5.2, I knew there should be Procedures in our database but they were not showing by default. I right-clicked the Procedures and chose "Refresh Item" to get them to show. Must not be loaded by default so that can be misleading.

Is there a way to open a sql file in SQL Server Management Studio?

I have thousands of stored procedures in my data base. And I can't stand scrolling through the entire list to find the SPROC I'm looking for. Is there a command in sql server mgmt studio to open the file in the editor like 'OPEN dbo.SomeStoredProcedureName'
There is no T-SQL command to do this, as SSMS is just a client management tool. Your best bet is to use the Filter tool built in to SSMS:
I don't believe there is such command but if you just want to see and not update you could use
sp_helptext 'dbo.SomeStoredProcedureName'
This messes up the formatting so you shouldn't use it to update (while you can). I also map a key to it Ctrl-F1 that can be used to just write the name and select an d press Ctrl-F1.
You've got lots of options, depending on exactly what you're trying to do:
You can "open file" and graphically browse to the directory you want, then graphically scroll down to and open the file you want.
You can "use" the database you want, and "exec" the stored procedure you want from a command-line query window.
You can "exec sp_helptext XXX" the stored procedure to see the text in a command-line query window.
You can use "filter" in the GUI to eliminate stuff you don't want to see
You can write a VBScript or Powershell script to do the same stuff the SSMS GUI lets you do.
Etc etc
You could export the whole database to a creation script. Then it would be easy to search in your favorite file viewer.
From Management Studio you can right-click on the database, choose Tasks->Generate Scripts, and then select "Stored procedures". If you're just browsing, this is very handy. Also good to store such scripts in your code repository.
SqlSmash lets you navigate easily to any object (including stored procedures) in SSMS.
Source
Disclaimer: I am the developer for the addin.

Is there some way for me to generate SQL Scripts from an already existing database?

Say I already created my database but forgot to save the sql commands do create it.
How could I reverse engineer the code from an already existing database?
I'm using Microsoft SQL Server Express 2008.
You can do this pretty easily by using SQL Server Management Studio (SSMS) - it's available for free if you don't already have it installed.
Connect to the database
Expand out Databases > YourDataBaseName.
Right-click on the database and select the option "Script database as" then "Create To" then finally "File".
That will create the necessary scripts to recreate your database.
To script out all the tables in your database:
Right-click on the database node
Select "Tasks" then "Generate Scripts".
When the wizard appears, click Next.
Select the database. At this point you can check the "Script all objects in the selected database" which does exactly what it says, or if you leave it unchecked you will get the option later in the process to pick which items are scripted.
Click next. Now you're given some scripting options.
I'd suggest scrolling down the list and checking the option to Script Indexes/Script Triggers. You can also script the data if necessary (though I wouldn't do this if you've got a lot of data in your database).
Modify any options you'd like and click Next.
Select the database types you'd like to script (Users/Tables/Views). Click Next.
Now you've got the opportunity to select more specific items. Hit Next and repeat the process of any of your other database types.
Hit next one more time, then select where you'd like the script written to. You get the chance to review your selections.
Click Finish.
Here's a link for the 2008 version SSMS Express 2008
Your RDBMS comes with some sort of "dump" tool that will give you the structure and content of your database, in the form of SQL statements.
As others have mentioned, if you have SQL Management Studio (you should, it's free as part of SQL Server Express). Fire it up, connect to your instance then expand the Database tree.
Right click on your database and select Tasks->Generate Scripts..
Click next, then Next again (which selects all objects in the database by default), pick an output option (defaults as "Save to File"), click next and voila!
If you also want to script the data as well as the schema, in the "Set Scripting Options" window, click on the Advanced button, scroll down to "Types of data to script" (just above the Table/View Options header) and select "schema and data".
[Edit] Tested - The Generate Scripts option exists and works in the (free) 2008 R2 edition of SSMS. See the link in my comment below for the URI for the R2 version.

Update SQL Server 2005 view with new database name?

I have approximately 100 SQL views that are a variation of this:
select * from RTC.dbo.MyTable
...now I find I need to change the name of the RTC table to something else. Rather than edit one view at a time, is there a way to script out all their drop/create statements to a text file so that I can do a global replacement?
In SSMS right click the database, go to Tasks and select there 'Generate Scripts...'. Select 'Views', select the views you want exported, export.
I'd use PowerShell. If you're not using SQL 2008 Client Tools, install them. Then get the PowerShell client, add the registered snapins (plenty of information out there on how to do that), and then use the directory structure to get to the folder representing your Views.
Then script them using something like:
Get-ChildItems | % {$_.Script()}
Use ScriptOptions to tell it to use an Alter script.
And replace "RTC." with the new database name... and run them using sqlcmd.
PowerShell actually becomes a really nice deployment option too.

Resources