Category Archives: Imported from BeyondRelational

These posts are imported from my old blog page: http://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx

This Blog is relocating!


It’s time for a change. This website, BeyondRelational.com, is rolling out a new Blogs module with improved features, feedback and social opportunities. All 146 of my posts have been moved to the new location: http://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx?tab=default. All future posts will be on the new blogs module.

The blogs will continue to be published just as you have been used to – twice a week, every week (one on Monday and the other one on Thursday). In order to subscribe to E-mail alerts do not forget to visit the “Preferences” section within the my blog space.

As always, do keep sending in your most valuable feedback. It helps me ot understand what you would like to read about, what you like and more importantly, where can I help you? If you like a post, do not forget to check the “Like” checkbox and share it using your favourite social media – Facebook, Twitter or Google Plus.

Without any delay, let me redirect you to the new blog location: http://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx?tab=default

Until we meet next time,

Be courteous. Drive responsibly.

Tweet to @nakulv_sql

#0147 – SQL Server – Changing the number of SQL Server Error log files before they are recycled


Today’s post is a short and simple one. No system is error free. The key is in providing easy and efficient mechanisms to capture these errors and enabling the users of the system to extract meaningful information from these error logs.

Microsoft SQL Server logs certain system events and user-defined events to both – the SQL Server error log and the Microsoft Windows application log. Here is what MSDN has to say about SQL Server error logs:

View the SQL Server error log to ensure that processes have completed successfully (for example, backup and restore operations, batch commands, or other scripts and processes). This can be helpful to detect any current or potential problem areas, including automatic recovery messages (particularly if an instance of SQL Server has been stopped and restarted), kernel messages, or other server-level error messages.

Every time the SQL Server instance is started, a new SQL Server error log is created. The default number of logs that the SQL Server will collect before recycling the logs is 6. A user can customize the Error log limit from SSMS by following the steps outlined below:

  1. In the Object Explorer, expand the SQL Server instance node
  2. Expand Management and navigate to SQL Server Logs
  3. image
  4. Right-click SQL Server Logs and click “configure”
  5. Only 2 options are available:
    • Limit the number of the error log files before they are recycled
      • Check to limit the number of error logs created before they are recycled. A new error log is created each time an instance of SQL Server is started
      • SQL Server retains backups of the previous six logs, unless you check this option, and specify a different maximum number of error log files below
    • Maximum number of error log files
      • Specify the maximum number of error log files created before they are recycled
      • The default is 6, which is the number of previous backup logs SQL Server retains before recycling them
      • The user can specify a maximum log file count of 99
  6. image

References:

Until we meet next time,

Be courteous. Drive responsibly.

Tweet to @nakulv_sql

#0147 – SQL Server – Changing the number of SQL Server Error log files before they are recycled


Today’s post is a short and simple one. No system is error free. The key is in providing easy and efficient mechanisms to capture these errors and enabling the users of the system to extract meaningful information from these error logs.

Microsoft SQL Server logs certain system events and user-defined events to both – the SQL Server error log and the Microsoft Windows application log. Here is what MSDN has to say about SQL Server error logs:

View the SQL Server error log to ensure that processes have completed successfully (for example, backup and restore operations, batch commands, or other scripts and processes). This can be helpful to detect any current or potential problem areas, including automatic recovery messages (particularly if an instance of SQL Server has been stopped and restarted), kernel messages, or other server-level error messages.

Every time the SQL Server instance is started, a new SQL Server error log is created. The default number of logs that the SQL Server will collect before recycling the logs is 6. A user can customize the Error log limit from SSMS by following the steps outlined below:

  1. In the Object Explorer, expand the SQL Server instance node
  2. Expand Management and navigate to SQL Server Logs
  3. image
  4. Right-click SQL Server Logs and click “configure”
  5. Only 2 options are available:
    • Limit the number of the error log files before they are recycled
      • Check to limit the number of error logs created before they are recycled. A new error log is created each time an instance of SQL Server is started
      • SQL Server retains backups of the previous six logs, unless you check this option, and specify a different maximum number of error log files below
    • Maximum number of error log files
      • Specify the maximum number of error log files created before they are recycled
      • The default is 6, which is the number of previous backup logs SQL Server retains before recycling them
      • The user can specify a maximum log file count of 99
  6. image

References:

Until we meet next time,

Be courteous. Drive responsibly.

Tweet to @nakulv_sql

#0146 – SQL Server – Exception handling change – DECLARE can now generate an ERROR – SQL Server 2008 and up


It is very important to keep revisiting one’s code – especially, when the underlying platform changes or when teams start using newer features within legacy code.

Until SQL Server 2005, the typical understanding of the keyword “DECLARE” was that it had only one meaning – it was used to define an object/variable within a T-SQL batch. Any variables/objects defined using DECLARE would have a value of NULL, unless a value is explicitly assigned to them. Starting SQL Server 2008, however, things changed. DECLARE now had 2 meanings:

  • DECLARE is still used to “define” an object/variable within a T-SQL batch AND
  • DECLARE can also be used to assign a value to a variable

At first glance, this is good! It saves 2 lines of code:

USE tempdb
GO
DECLARE @x INT
SET @x = 123

becomes:

USE tempdb
GO
DECLARE @x INT = 123

But, can things really be this simple? The simple answer: NO.

DECLARE can now raise an exception

Up until SQL Server 2008 came around, the sole purpose of DECLARE was to ‘define’ objects. Unless something was horribly wrong, this statement was pretty harmless. However, because the purpose of the DECLARE is not to define & assign, things get a little bit complicated. The assignment part of the DECLARE keyword can now raise an exception. Here’s an example.

SQL Server 2005 and below

Because DECLARE would not generate an exception, it was okay to have the DECLARE statements outside of the TRY…CATCH blocks.

USE tempdb
GO
DECLARE @x INT

BEGIN TRY
    SET @x = 'abc'
    SELECT @x
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER() AS ErrorNumber,
           ERROR_SEVERITY() AS ErrorSeverity,
           ERROR_STATE() AS ErrorState,
           ERROR_PROCEDURE() AS ErrorProcedure,
           ERROR_LINE() AS ErrorLine,
           ERROR_MESSAGE() AS ErrorMessage;
END CATCH
GO

Because we are trying to assign a text value to an Integer field, an exception would be raised within the TRY block and caught by the CATCH block.

Handled exception

SQL Server 2008 and above

Attempting to perform both the definition and the assignment using a DECLARE statement, outside of the TRY…CATCH would result in an unhandled exception.

USE tempdb
GO
DECLARE @x INT = 'abc'

The unhandled exception would look like:

Msg 245, Level 16, State 1, Line 2

Conversion failed when converting the varchar value ‘abc’ to data type int.

Solution:

The solution is very simple – wrap the DECLARE within a TRY…CATCH block.

USE tempdb
GO
BEGIN TRY
    DECLARE @x INT = 'abc'
    SELECT @x
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER() AS ErrorNumber,
           ERROR_SEVERITY() AS ErrorSeverity,
           ERROR_STATE() AS ErrorState,
           ERROR_PROCEDURE() AS ErrorProcedure,
           ERROR_LINE() AS ErrorLine,
           ERROR_MESSAGE() AS ErrorMessage;
END CATCH
GO

The output would be a handled error, as demonstrated in the above screenshot.

The morale of the story:

  • DECLARE statements involving assignment must now be within a TRY…CATCH block
  • Making a change, however seemingly small, requires detailed thought and planning

References:

Until we meet next time,

Be courteous. Drive responsibly.

#0145 – Visual Studio, SQL Server Express Editions, Visual Studio Lightswitch – they are licensed & free!


Often, students and colleagues come up to me and enquire how do they get started learning about some of the newest technologies. They generally do not seem to have issues with getting the study materials (most common study materials are the many blogs on the Internet and MSDN documentation). The one thing they do have issues with is procuring software to practice their skills on.

Software, like any other commodity has a price. It takes a large amount of money to envision, design, develop, test, market a piece of software. Documentation and after sales support and training are also expensive for any ISV. This stands true for the Microsoft software as well (Operating Systems, Visual Studio, SQL Server and other products). While Microsoft does have the Microsoft Student Partner program for students, the program cannot cover every student in every university on the planet. Also, the program is not available for anybody else who would like to get started on a little programming. So, if you want to get started on one of the new Visual Studio or SQL Server editions and can do without the advanced design, development & test capabilities; then the following are perhaps the pieces of software that you are looking for:

The most attractive part about these “Express” editions is that they are free. However, the salient benefit is that these editions are covered by Microsoft support. They are eligible for automatic product upgrades and you can get all the technical support that you would need.

The other benefit is that you now own a licensed product for free. Isn’t that a wonderful? I use them on my test system at home, and have never felt the need for more.

Until we meet next time,

Be courteous. Drive responsibly.

Subscribe to my posts via E-mail: Subscribe here | Read my posts via your favourite RSS reader: Click Here! | Tweet to @nakulv_sql