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.
Related
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.
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
In BIDS 2005, I use a lot of fixed-width flat file sources. When initially creating the connection manager, in the graphical 'Columns' pane, I can click to insert each column delimiter where needed. Clicking 'OK' saves perfectly fine.
However, when editing an existing flat file connection, I can see and move existing column delimiters on the 'Columns' pane, but can no longer add new ones. Instead, I must go to the 'Advanced' pane, and add columns manually, while having to keep clicking back and forth to the 'Columns' pane to see where the delimiters need to go.
Is this normal behavior in BIDS? Is there a work-around, or am I stuck with the 'Advanced' pane?
There is no other way to add column to existing fixed width flat file. This is true for SQL Server 2008 R2, also. I am not aware of such improvement in 2012 version.
So you are left with adding column in Advanced pane or using Reset columns button to set all the columns again.
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.
How do I limit a SQL Server Profiler trace to a specific database? I can't see how to filter the trace to not see events for all databases on the instance I connect to.
Under Trace properties > Events Selection tab > select show all columns. Now under column filters, you should see the database name. Enter the database name for the Like section and you should see traces only for that database.
In SQL 2005, you first need to show the Database Name column in your trace. The easiest thing to do is to pick the Tuning template, which has that column added already.
Assuming you have the Tuning template selected, to filter:
Click the "Events Selection" tab
Click the "Column Filters" button
Check Show all Columns (Right Side Down)
Select "DatabaseName", click the plus next to Like in the right-hand pane, and type your database name.
I always save the trace to a table too so I can do LIKE queries on the trace data after the fact.
By experiment I was able to observe this:
When SQL Profiler 2005 or SQL Profiler 2000 is used with database residing in SQLServer 2000 - problem mentioned problem persists, but when SQL Profiler 2005 is used with SQLServer 2005 database, it works perfect!
In Summary, the issue seems to be prevalent in SQLServer 2000 & rectified in SQLServer 2005.
The solution for the issue when dealing with SQLServer 2000 is (as explained by wearejimbo)
Identify the DatabaseID of the database you want to filter by querying the sysdatabases table as below
SELECT *
FROM master..sysdatabases
WHERE name like '%your_db_name%' -- Remove this line to see all databases
ORDER BY dbid
Use the DatabaseID Filter (instead of DatabaseName) in the New Trace window of SQL Profiler 2000
In the Trace Properties go to the Event Selection tab. Then click on the Show All Columns radio button. After that click on the Column Filters button.
Now you can see the Database Name property and click on it. Expand the like box and insert your DB name and click ok and you can run the profiler now.
In the Trace properties, click the Events Selection tab at the top next to General. Then click Column Filters... at the bottom right. You can then select what to filter, such as TextData or DatabaseName.
Expand the Like node and enter your filter with the percentage % signs like %MyDatabaseName% or %TextDataToFilter%. Without the %% signs the filter will not work.
Also, make sure to check the checkbox Exclude rows that do not contain values' If you cannot find the field you are looking to filter such as DatabaseName go to the General tab and change your Template, blank one should contain all the fields.
Create a new template and check DBname. Use that template for your tracefile.