Automatically generate scripts for mouse clicks in SSMS? - sql-server

We can create (update, populate) tables etc by either SQL commands or by simply clicking the options/command with a mouse/keyboard in SQL Server Management Studio (Express) 2008.
Is there a way I can record all the commands that occur when I do it with a mouse?
Are there macro recording features within SSMS?
Are there 3rd party addones to support macro recording in SSMS?

No, there is nothing built into SSMS to record keystrokes / mouse clicks. There are plenty of 3rd party programs available, though.
What you should do is write the commands you want to run using T-SQL or DDL. Those are very easy to save to a file, store in source control, review, run again later, etc.
In newer versions of SSMS, almost all tasks you perform through dialogs has the ability to script out the change you are about to perform (or just generate a script instead of clicking OK):
EDIT for the use case you presented in a comment, where you create a table, add constraints, add some dummy data. Here is what you can do to generate a script that you can use later:
Open Object Explorer
Right-click your database and choose Tasks > Generate Scripts
Click Next, Click Next
On the "Choose Script Options" page, scroll down and set "Script Data" to True
Click Next
Check Tables and click Next
Check the table you want and click next
Choose whether to script to a file, to the clipboard, or a new query editor window.
You'll end up with something like this:
USE [your_database]
GO
/**** set options, object name, script date, etc ****/
CREATE TABLE [dbo].[foo](
bar INT,
blat INT,
CONSTRAINT PK_foo PRIMARY KEY CLUSTERED(bar ASC)
) -- with/on etc.
GO
INSERT [dbo].[foo]([bar],[blat]) VALUES(1,2);
INSERT [dbo].[foo]([bar],[blat]) VALUES(3,4);
GO
ALTER TABLE [dbo].[foo] WITH NOCHECK ADD CONSTRAINT [chk_blat]
CHECK ((blat > bar))
GO
ALTER TABLE [dbo].[foo] CHECK CONSTRAINT [chk_blat]
GO
I know this is more steps than you want, but Management Studio's job is not to save every single command you execute and store it somewhere so you can retrieve it later.

Here are two options that might help.
The SSMS Tools Pack has a query history:
http://www.ssmstoolspack.com/Images/Features/QEH1.png
I think this will work as you expect because your interaction with the GUI will translate into T-SQL commands, which should be recorded by this tool.
You can download this here:
http://www.ssmstoolspack.com/Features
If you start getting familiar with the actual commands you can use SQL Tab Magic, which auto-saves your SSMS query windows for future retrieval:
http://www.red-gate.com/products/sql-development/sql-prompt/entrypage/sqltabmagic

Related

IntelliJ IDEA: How to quickly open a DB table editor?

Scenario: I'm in IntelliJ IDEA DB console and looking at
SELECT * FROM TableXY;
I want to see the definition of the TableXY. One way of doing it is:
ctrl+click on the table name: Looks up the table in the Database window.
F4: Opens the table editor.
select the Text tab
The problem is that I'm on a DB with a lot of tables and the first step takes forever because IDEA loads the full list of tables.
Is there a way to jump to the table editor directly?
I am not sure if this is what you are looking for, but to quickly view the table definition you can use the Quick Documentation pop-up:
Place your cursor within the table name and hit CTRL+Q (or F1 on Mac). This will show you some information about the table, the first rows, and the table definition (output from SHOW CREATE TABLE).
You can also configure the Quick Documentation under Settings > Tools > Database (see Intellij IDEA on-line help).

SQL Server changing data type from decimal(18,7) to decimal(23,15)

One of my fields has datatype decimal(18,7). I would like to change it to decimal(23,15). When I attempt it, get error message about "Saving changes not permitted.... require the following tables to be dropped and re-created.... can't be re-created or enable the option Preventing saving changes that rquire that table to be re-create"
Where do I change options to allow table to be dropped and re-created? I have nothing valuable in this table, I can easily drop the table manually and re-create it manually, but would rather have SQL Server do it for me.
Regards,
Well, as it says, it is in the options. Which are in the menu.
And in the documentation:
http://msdn.microsoft.com/en-us/library/bb895146.aspx
To change this option, on the Tools menu, click Options, expand
Designers, and then click Table and Database Designers. Select or
clear the Prevent saving changes that require the table to be
re-created check box.

Right click script alter table disabled in SQL Server Management Studio

I want to script a table as Alter to a New Query Editor Window. But this option is disabled. How can I do this?
Use the "Design" function in SQL Server Management Studio to generate the ALTER script for you:
Right-click on the table you want to alter and choose Design.
Add new columns, change field types, set your fields to accept NULLS or not, etc.
Once you are done, click the Generate Change Script toolbar button (or right-click on any column or in the white space). This button looks like a floppy disk hovering over a piece of curled paper
Save the result to a text file
Depending on how you have your SSMS options set, this may not be available initially if the changes require tables to be dropped and re-created. To change this option, on the Tools menu, click Options, expand Designers, and then click Table and Database Designers. Select or clear the Prevent saving changes that require the table to be re-created check box.
1) Right-click on the table you want to alter and choose Design
2) Add your new columns, change the field types, set your fields to accept NULLS or not, etc.
Important: Don't save your changes now
3) Do a right-click on any column or in the white space and you’ll see the option Generate Change Script is now available.
In SQL Server Management Studio 2016, toolbar button icon has changed. See the highlighted new icon.
You can access this dialog box after you have made unsaved changes to a table in Table Designer. On the Table Designer menu, click Generate Change Script.
2.Automatically generate change script on every save
If checked, the Save Change Script dialog box will appear any time you save changes to a table.
Yes
Bring up the Save dialog box where you can choose the location for the text file.
No
Cancel the creation of the change script.

Refreshing a table in SQL server management studio

I have opened a table in SQL server management studio express.
How do I update it?
As changes made outside the studio application do not show unless I close and reopen the table. Surely there must be a better way of doing this?
In SSMS (SQL Server Management Studio) press Ctrl + R
In Visual Studio Press Shift + Alt + R
I am not sure if I understand exactly what you want, but if you want to see the changes, the query that loaded the table has to be re-executed. You can do this by clicking on "Execute SQL" (or selecting it from the context menu).
Depending on what you mean with "opening the table" (select top x rows, edit top x rows, etc ) you also might try to hit F5 (works for "Select TOP x Rows") - which simply executes the previous statement.
As a rule, manually editing data in SQL Server is a bad practice.
It's not repeatable. Anything you do is limited to your window and once it's gone it's gone.
It's easy to make mistakes. Click the wrong cell, fat-finger a decimal point, etc.
A much better solution is to create and save insert or update scripts. These are editable, reviewable, and rerunnable.
If you need to recreate your database or replicate something now, you need to manually type in all the same values you typed in before. If you script it out, you just run the script and can have the same data as many times as you can hit F5.
It is much easier if you just create a query referencing on that table. Then create select statement giving all rows from that table.

Change A Database's Table's Name (SQL Server 2008)

I got a table named tblHello and I wanna rename it to Hello
Right click on the table and select rename in management studio
You can also use sp_rename:
sp_rename 'old_table_name', 'new_table_name'
I want to point out that table renaming is not as simple as just changing the name when you have queries written. You also need to change all references to the old name in every stored proc, view, function and dynamic sql code. This is not something to be taken on lighty in something that is already on production.
But #jonH has the answer for how to do it (you run that in the query window making sure you switch to the correct database first). Of course you have to have the right security permissions to rename objects.
If its a small project,
you can directly change it from User Interface.
In Server Explorer, right-click the table you want to rename and Open Table Definition.
Right-click the table in the Table Definition window and choose Properties from the shortcut menu.
In the field for the Name value in the Properties, type a new name for the table.
Save the table.
And its done.

Resources