Help: SQL Server

Sharing my knowlege about SQL Server Troubleshooting Skills

  • Blog Stats

    • 2,153,602 hits
  • Select GETDATE()

    December 2025
    M T W T F S S
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  

Archive for the ‘SSMS’ Category

SSMS Tip: How to drop multiple objects?

Posted by blakhani on June 3, 2014


Have you even been into situation where you have to drop many tables in a database? Or drop multiple databases? As a developer, you might right click on each table/database and choose “delete”. If you are equipped with little T-SQL knowledge, you might write a script to do the same. Do you know that management studio can do it?

Let’s create multiple databases and learn the trick. I have used NEW_ID to get completely random name of databases.

declare @number_of_databases int, @loop int
select @number_of_databases = 10
select @loop = 1
declare @random_name varchar(40)
declare @str nvarchar(100)

while (@number_of_databases >= @loop)
begin
select @str = N'Create database [' + convert(varchar(100),NEWID())  +N']'
exec sp_executesql @str
set @loop=@loop+1
end

Once the script if executed, I have below in my management studio. Your databases would definitely be different.

image

Now, my task is – drop those databases which are created today. Without knowing the trick, one would get all databases name by query and then right click on database, choose delete. That would open a new window and we would choose “close existing connection..” and again hit OK. This has to be done 10 times. Now, here is my way of doing it.

Click F7 on keyboard when SSMS is open. This would open “Object Explorer Details”. You can also use “View” in menu bar and choose “Object Explorer Details” item there. This would open a interface which is “details” on the object selected in “Object Explorer” . I would refer this as OED in later part of the blog. (OE of left and OED on right)

image

In OED, we have capability to sort the item based on various columns available. I want to sort databases them using date created. But by default that column is not shown. Let’s Right click on column header in OED and choose the column which we are interested. I have selected “Date Created”

image

Once that’s done, I can choose the position of the column by “dragging” the column name

image

Now, we can SORT as well by clicking on column name. I have sorted them by date created and as we can see all databases which I have created by script have listed next to each other. OED has capability the select multiple objects. This can be done by:

  • Use Shift key to choose all objects next to each other.
  • Use Ctrl key to choose multiple object using mouse

Once desired objects are selected, use right click and choose delete. Simple. huh?

image

And once we choose delete, we get “Delete Objects” dialogue box which is similar to single delete.

image

We can choose the desired option and hit OK.

Same operation would work for tables as well. We just need to choose proper node on “Object Explorer” and get details under “Object Explorer Details” – OED

image

Again, choose multiple tables in OED, right click and delete.

I always felt that SQL Server Management Studio is one of “unexplored” and “underestimated” tool in SQL Server Product. Hope this blog has shown a little “hidden” gem of SSMS. If you know any such trick, please use comment section to share your knowledge.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in SQL Server Management Studio, SSMS | Tagged: , , , , , , , | 1 Comment »

    SSMS Tip: What happened to my keyboard shortcuts? Ctrl+R (Show/Hide Result) and Ctrl+E (Execute) not working!

    Posted by blakhani on May 8, 2014


    Long back I installed SQL Server 2014 to learn new features and of course share the knowledge. I started from CTP1, CTP2 and finally came to RTM. Earlier I was installing them on Virtual Machines and recently I installed SQL 2014 on my laptop which already has SQL Server 2012 installed.

    While using SSMS of SQL Server 2014, I noticed that earlier keyboard shortcuts were not working as they used to work earlier (in SQL 2012 SSMS). Few examples are as below:

    • Ctrl + R should show/hide result pane.
    • Ctrl + E should execute the query.

    If I use above, I was getting below message in the left bottom of SSMS. “(Ctrl+R) was pressed. Waiting for second key of chord…

    image

    Pressing Ctrl+R again I get message as “The key combination (Ctrl+R, Ctrl+R) is not a command

    Same was the case with Ctrl+E command. “(Ctrl+E) was pressed. Waiting for second key of chord…” . If I take my mouse to “Execute” button, it guided me that shortcut to execute command is Ctrl+Shift+E. And it works as well.

    image

    But I always preferred to go back to earlier settings. There are multiple ways to achieve it.

    Short Route

    If you have not done any customization in SSMS and you are OK to reset all the settings then you can use this. In Management Studio menu, go to Tools –> Options –> Environment –> Keyboard –> Keyboard –> Apply the following additional keyboard mapping scheme” and choose “Reset”. You would get a confirmation pop-up, click OK there.

    image

    Long Route

    This route would be preferred if you have done some customization with the settings and want to retain those setting. you can provide shortcuts to any action. Go to Management Studio Tools menu and Tools –> Options –> Environment –> Keyboard –> Keyboard Over there, you can choose the command and assign the “shortcuts for selected command”. The list of command can be searched. For example, I have entered “Window.ShowResultsPane” and assigned Ctrl+R to it as below. Make sure to click on “Assign” button before hitting OK.

    image

    In Same way , Ctrl+E can be assigned to “Query.Execute

    Documentation: http://technet.microsoft.com/en-us/library/ms174205(v=sql.120).aspx (SQL Server Management Studio Keyboard Shortcuts)

    More reading: http://blogs.msdn.com/b/managingsql/archive/2011/07/13/enhanced-keyboard-shortcuts-in-ssms-in-denali.aspx

    Hope this helps! Please write comment and let me know your feedback.

    Cheers,
    Balmukund

    Posted in SQL Server Management Studio, SSMS | Tagged: , , , , , , | 15 Comments »

    SSMS Tip: Merging ERRORLOGs from different machines using Log File Viewer

    Posted by blakhani on March 6, 2014


    For those who don’t know what/where is SQL Server ERRORLOG, please read this

    Log File Viewer is an interesting feature of Management Studio and I have been ignoring it (you too??). This tool as saved a lot of time for me so thought of sharing some cool features of this tool. This can load various logs including system event log, application event log, DB Mail Log and SQL Agent log.

    As a part of my regular work, I need to look at SQL Server ERRORLOG to look for various information about environment like SQL version and Edition, Processors and memory etc. If you want to know where ERRORLOG is located, then read here.

    While working on a case on AlwaysOn Availability Group, customer provided us errorlog from two machines. To complete my analysis, I have to merge the file from both servers so that I can look at series of event happening across the server. Since customer was not having access to physical machine, he used management studio to “export” the errorlog.

    Here are the steps to export the log using “Log File Viewer”

    image

    Once the viewer is opened, it opens current ERRORLOG file from the server. If we want to see more we can select checkbox on the left hand tree as highlighted below.

    image

    Export button can be used to Save the content on local machine. This is useful when we don’t have access to physical server to get file.

    To demonstrate “merge”, I have exported current ERRORLOG from three servers and saved them as ERRORLOG_SQLPAPA, ERRORLOG_SRV1 and ERRORLOG_SRV2. I have kept all files in same machine and launched SSMS there. First, we need  to load the log as shown below.

    image

    One all are loaded, we can see them on left hand tree. Select the checkbox if it has to be loaded on right side grid.

    image

    I have selected all three files. Now, in the grid there is one interesting column, Log Source which shows the file which is loaded. I have dragged the column header and moved to visible location.

    image

    If we sort by date column, we can easily see the series of message in errorlog based of time. Since we have “Log Source” column, it possible to find server name as well. That’s why I have given File Name which is meaningful. In above screenshot I can see that top 3 messages are from different servers and I know the time as well.

    You can be more creative and use “Filter” to load only specific data which you are interested.

    Hope you liked this cool feature. Please provide feedback via comments.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in SQL Server Management Studio, SSMS | Tagged: , , , | 1 Comment »