SQL SERVER – Send Email From SQL Server – Configure Database Mail – SQL in Sixty Seconds #039 – Video

Let me start this blog post with negative note: SQL Server is not mass mailing software. If you are thinking of sending emails using SQL Server instead of your mail server – I suggest you stop doing that NOW! Whenever, I see any application using SQL Server as a mail server – I always vote against it. Well, if this is so bad, then why is it possible to send email through SQL Server. The reason is simple – there are many SQL Server Administrative scenarios where we need SQL Server to send emails, e.g. Maintenance task status, job failure messages, operators alerts etc. I suggest to use Database mail option during this situation.

In this SQL in Sixty Seconds I explain how one can configure database mail quickly and send emails to users. If you can’t watch this video – I suggest you to read this blog post which describes the same using images - Configure Database Mail – Send Email From SQL Database.

Let us see how to take backup in this SQL in Sixty Seconds:

Related Tips in SQL in Sixty Seconds:

What would you like to see in the next SQL in Sixty Seconds video?

Reference: Pinal Dave (http://blog.sqlauthority.com)

About these ads

SQL Server – Using SSMS Commandline Parameters – Guest Post by Vinod Kumar M

Vinod Kumar M is my very good friend renowned SQL Server Expert. Vinod Kumar has worked with SQL Server extensively since joining the industry over a decade ago. Before joining Microsoft, he was a Microsoft MVP for SQL Server for more than 3 years. He now works with MTC as a Technology Architect. He is a well-known speaker at all major Microsoft and third party technical conferences. Here is a very interesting blog post he sent on the subject of SSMS Command Line Parameters. Don’t forget to visit Vinod’s blog for additional SSMS Tips and follow him on twitter


Using SQL Server Management Studio is something we do instantly after connecting to our mailing system everyday if we are a SQL Developer or SQL Administrator. Though this is a very common step, there can be optimizations we can make even in this mundane yet simple task. Let us look at the steps we take normally.

  1. We find the “SQL Server Management Studio” icon from the start menu and will click on it.
    1. We can also type “ssms” in the run prompt to quickly launch. This is my favorite and I use almost daily.
    2. After Management Studio launches we need to give the Authentication mode. Let us assume we use Windows Authentication and we press “Enter” to login.
    3. This initializes the Object Explorer with the Login.
    4. Right click on Database or Server node to open a query window.

As we can see that even though this is a common activity has taken close to 4-5 clicks. How about making this efficient? If you didn’t know we have some hidden command line parameters available with SQL Server Management Studio. To know the parameters, from the run prompt just for this command.

C:\> ssms /?

This brings the list of parameters we can use. The figure below shows the same.

To tabulate the various options:

Command Line Switch Description
S The name of the SQL Server instance to connect to.
d The name of the SQL Server database to connect to.
U The name of the SQL Server login to connect with.
P The password associated with the login.
E Use Windows Authentication to login to SQL Server.
noSplash Suppress splash screen.
Displays this usage information.
file_name File to load upon startup. Good for loading a .SQL file directly.

Having known all the parameters, here is the tip we would like to give you. Fire Management Studio using the authentication in one shot using the following command.

C:\> ssms -E

In this case we have used the Windows Authentication to login. We can replace the same with –U and –P parameters for SQL Authentication. Feel free to use the –d option to connect to a specific database.

Another tip, if you want to change the default behavior of the shortcut, then right click the shortcut and select properties. Add the –E or other appropriate options to your desire.

After adding the option. Click the “Ok” button. Now when we fire the Management Studio icon, this will open the management studio with an active Query window ready to do the work.

We would love to hear how many of you have played around with these parameters in the past? Have you enabled these in your Laptop’s or machines you work daily? Do drop us a comment about the same. I did talk about few SSMS Tips earlier over my blog and thought will be worth mentioning here.

Reference: Pinal Dave (http://blog.sqlauthority.com)

SQL SERVER – Wrap SQL Code in SSMS – SQL in Sixty Seconds #038 – Video

Every developer has a different habit. Some like to format the functions in upper case and some wants it in lower case. I often see developers listing the columns in SELECT clause in a different way. I have my own preference but I do respect the other developer’s preference as well. I do not advise to change anybodies habit but there is one thing which I strongly prefer to do on the client side when I am editing code.

WRAP THE CODE!

Well, it is indeed very difficult to read the code when users have to horizontally scroll the code. Now when I am working on the file originally created by another developer, I do not like to change their formatting. I have a very wide screen on my personal desktop computer but when I see the same on my 12″ laptop, I see the horizontal scrolling. There can’t be a single solution in the case of the wrapping the code, which fits all the screens.

Fortunately, there is a solution which will fit all the screens.

SQL Server Management Studio has a setting which will wrap the code displayed on the screen. Even if you resize the screen the code wraps. It will be very difficult to explain that in words – I suggest you watch following SQL in Sixty Seconds Video on this subject.

You can access the settings from SSMS >> Tools >> Options >> Text Editor >> Transact-SQL. It will show the following screen.

Let us see the same concept in this SQL in Sixty Seconds:

Related Tips in SQL in Sixty Seconds:

What would you like to see in the next SQL in Sixty Seconds video?

Reference: Pinal Dave (http://blog.sqlauthority.com)

SQL SERVER – Take Database Backup using SSMS – SQL in Sixty Seconds #037 – Video

Whenever I am suggesting something which changes how database works or the existing status of the database, my suggestion along with it is to take the database backup before making such changes. If the changes are in configurations, that can be easily revert but if the changes are such that it will impact the data, I always suggest to take backup. The nature of this blog is such that we have readership from readers with different expertise, some are experts and some are novice.

One of the questions I keep on receiving comments are whenever I give suggestion to take a full database backup is – how to take database backup. It is not possible to explain in each and every blog regarding how to take backup of the database as it will be of no interest to the users who are very much familiar with this concept. To help the beginners who have yet to learn regarding how to take database backup, here is the SQL in Sixty Series episode which explains how to take database backup.

There are three types of the backup – 1) Full Backup 2) Differential Backup and 3) Log Backup. We can take the differential backup as well log backup from the same screen where we take full backup from. I have explained the same also in later parts of the video.

If you are using SQL Server Management Studio – here is the path which will bring you to the screen where you can take various kinds of backup.

Right Click on Database >> Tasks >> Back up… 

Let us see how to take backup in this SQL in Sixty Seconds:

Please note that if your database is large, it will take long time to take backup. We will focus on the various steps to initiate the backup in this video.

Related Tips in SQL in Sixty Seconds:

What would you like to see in the next SQL in Sixty Seconds video?

Reference: Pinal Dave (http://blog.sqlauthority.com)

SQL SERVER – Select and Delete Duplicate Records – SQL in Sixty Seconds #036 – Video

Developers often face situations when they find their column have duplicate records and they want to delete it. A good developer will never delete any data without observing it and making sure that what is being deleted is the absolutely fine to delete. Before deleting duplicate data, one should select it and see if the data is really duplicate.

In this video we are demonstrating two scripts – 1) selects duplicate records 2) deletes duplicate records.

We are assuming that the table has a unique incremental id. Additionally, we are assuming that in the case of the duplicate records we would like to keep the latest record. If there is really a business need to keep unique records, one should consider to create a unique index on the column. Unique index will prevent users entering duplicate data into the table from the beginning. This should be the best solution. However, deleting duplicate data is also a very valid request. If user realizes that they need to keep only unique records in the column and if they are willing to create unique constraint, the very first requirement of creating a unique constraint is to delete the duplicate records.

Let us see how to connect the values in Sixty Seconds:

Here is the script which is used in the video.

USE tempdb
GO
CREATE TABLE TestTable (ID INT, NameCol VARCHAR(100))
GO
INSERT INTO TestTable (ID, NameCol)
SELECT 1, 'First'
UNION ALL
SELECT 2, 'Second'
UNION ALL
SELECT 3, 'Second'
UNION ALL
SELECT 4, 'Second'
UNION ALL
SELECT 5, 'Second'
UNION ALL
SELECT 6, 'Third'
GO
-- Selecting Data
SELECT *
FROM TestTable
GO
-- Detecting Duplicate
SELECT NameCol, COUNT(*) TotalCount
FROM TestTable
GROUP BY NameCol
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
GO
-- Deleting Duplicate
DELETE
FROM
TestTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM TestTable
GROUP BY NameCol)
GO
-- Selecting Data
SELECT *
FROM TestTable
GO
DROP TABLE TestTable
GO

Related Tips in SQL in Sixty Seconds:

What would you like to see in the next SQL in Sixty Seconds video?

Reference: Pinal Dave (http://blog.sqlauthority.com)

SQL SERVER – Concat Strings in SQL Server using T-SQL – SQL in Sixty Seconds #035 – Video

http://www.pinaldave.com/bimg/sixty00.jpgConcatenating  string is one of the most common tasks in SQL Server and every developer has to come across it. We have to concat the string when we have to see the display full name of the person by first name and last name. In this video we will see various methods to concatenate the strings. SQL Server 2012 has introduced new function CONCAT which concatenates the strings much efficiently.

When we concat values with ‘+’ in SQL Server we have to make sure that values are in string format. However, when we attempt to concat integer we have to convert the integers to a string or else it will throw an error. However, with the newly introduce the function of CONCAT in SQL Server 2012 we do not have to worry about this kind of issue. It concatenates strings and integers without casting or converting them. You can specify various values as a parameter to CONCAT functions and it concatenates them together.

Let us see how to concat the values in Sixty Seconds:

Here is the script which is used in the video.

-- Method 1: Concatenating two strings
SELECT 'FirstName' + ' ' + 'LastName' AS FullName
-- Method 2: Concatenating two Numbers
SELECT CAST(1 AS VARCHAR(10)) + ' ' + CAST(2 AS VARCHAR(10))
-- Method 3: Concatenating values of table columns
SELECT FirstName + ' ' + LastName AS FullName
FROM AdventureWorks2012.Person.Person
-- Method 4: SQL Server 2012 CONCAT function
SELECT CONCAT('FirstName' , ' ' , 'LastName') AS FullName
-- Method 5: SQL Server 2012 CONCAT function
SELECT CONCAT('FirstName' , ' ' , 1) AS FullName

Related Tips in SQL in Sixty Seconds:

What would you like to see in the next SQL in Sixty Seconds video?

Reference: Pinal Dave (http://blog.sqlauthority.com)

SQL SERVER – Auto Recovery File Settings in SSMS – SQL in Sixty Seconds #034 – Video

Every developer once in a while facing an unfortunate situation where they have not yet saved the work and their SQL Server Management Studio crashes. Well, you can minimize the loss by optimizing auto recovery settings. In this video we can see how to set the auto recovery settings.

Go to SSMS >> Tools >> Options >> Environment >> AutoRecover

There are two different settings:

1) Save AutoRecover Information Every Minutes

This option will save the SQL Query file at certain interval. Set this option to minimum value possible to avoid loss. If you have set this value to 5, in the worst possible case, you can loose last 5 minutes of the work.

2) Keep AutoRecover Information for Days

This option will preserve the AutoRecovery information for specified days. Though, I suggest in case of accident open SQL Server Management Studio right away and recover your file. Do not procrastinate this important task for future dates.

Related Tips in SQL in Sixty Seconds:

What would you like to see in the next SQL in Sixty Seconds video?

Reference: Pinal Dave (http://blog.sqlauthority.com)