Pages

Tuesday, January 7, 2014

Compiled Thoughts: Understanding MS SQL Server Named Instance Connections

The following blog post by Peter Lanoie helped me to better understand how SQL Server instances use ports:
Compiled Thoughts: Understanding MS SQL Server Named Instance Connections

Take note that the default instance of SQL Server listens on TCP port 1433, while the SQL Browser listens on UDP port 1434.  If you open port 1434 in your firewall for the TCP\IP protocol the SQL Browser will still appear not to be working and you will still have to provide the port number when connecting to a named instance.

Just for interest sake: TCP vs UDP

Monday, January 6, 2014

RAID levels

The following Wikipedia articles explain RAID levels nicely:

Thursday, July 1, 2010

NOT IN explained

Whenever the column returned by the subquery contains a NULL value, NOT IN will always return an empty set. Filtering out the NULL values in the subquery will solve this problem.

select
    *
from
    table1
where
    col1 not in(select col2 from table2 where col2 is not null)

Read SQL Server: JOIN vs IN vs EXISTS - the logical difference for a detailed explanation.

Monday, June 21, 2010

Corrupt index(es) in msdb

I was trying to delete a SQL Job using sp_delete_job. It continued to fail with the message:
Unable to find index entry in index ID 1, of table {object_id of table}, in database 'msdb'. The indicated index is corrupt or there is a problem with the current update plan. Run DBCC CHECKDB or DBCC CHECKTABLE. If the problem persists, contact product support.
I found this blog post and the script in it repaired the msdb. 

(The command that fixed it is DBCC CHECKDB('msdb', REPAIR_REBUILD), but there is quite a bit of preparation that needs to be done before this command will execute successfully.  See the blog post I referred to.)

Thursday, June 17, 2010

LocalSystem, LocalService and NetworkService Accounts

Below are links to resources which help explain the difference between these accounts:

This one about "Understanding the Local Service and Network Service Accounts" was the most useful.

Click here for the MSDN article in this regard.

Tuesday, June 8, 2010

Cannot connect to a remote SQL Server instance using SSMS on a Windows 7 pc

I had the following problem: On my Windows XP pc I could use SQL Server 2008 SSMS to connect to our SQL Server 2005 production server, but when I tried to connect from my (newly setup) Windows 7 pc I got the following error:

Cannot connect to {SQL Server Instance Name}.

Additional information:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (Microsoft SQL Server)

To resolve the problem I did the following on my Windows 7 pc:
* Opened Windows Firewall
* Opened the "Allow a program or feature through Windows Firewall" page
* Clicked "Allow another program..."
* Selected "SQL Server Management Studio" and clicked "Add". Only Domain was checked.
* Clicked "OK".

Tuesday, February 23, 2010

Don't be greedy in T-SQL queries

I am working on improving the performance of some stored procedures.  The queries all make use of index seeks and index scans to get to the data, so, nothing I can improve there.
The procs call other procs which call some more procs.  A join to a large table (+/-2million records) gets repeated in several of these procs.

So, I changed the deepest nested proc to return all the data which will be needed by the other procs.  I removed the repeated join; and the performance problem is solved.

So, two very important tips for performance tuning queries:

  1. Only return the records you are interested in; and
  2. Only touch a record once if possible.

Tuesday, February 16, 2010

CATCH block not executing after an error in the TRY block

Today we had trouble with the CATCH block in a T-SQL script which did not execute after an error in the TRY block. It turned out that there are times when your TRY block can fail and the CATCH block will get bypassed altogher.

Click here or on the title of this article to see the article by Alexander Kuznetsov about exactly this problem.

Our problem was a compile error: a column name we referenced in a join statement no longer existed.

Thursday, February 11, 2010

SSIS Indirect Configurations

With indirect configurations SSIS enables you to store the path to the dtsConfig file in an environment variable.  At runtime the package looks up the location to the config file, load the config file at that location, and does whatever it needs to do with the information.

If your SSIS package gets executed from a SQL Job, it is important to restart the SQL Agent after creating the environment variable - the SQL Agent caches the environment variables and won't pick up new ones unless restarted.

Wednesday, December 3, 2008

Protocol error in TDS stream

Yesterday I struggled with the following issue in SSIS: I specified a stored proc as the SQL command text in an OLE DB Source in a data flow. When I clicked the Preview button the data I expected was returned. The Columns were also picked up successfully in the source editor.

But everytime I ran the package it failed with the following errors:
SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80004005 Description: "Protocol error in TDS stream"...

SSIS Error Code DTS_E_PRIMEOUTPUTFAILED. The PrimeOutput method on component "OLE DB Source" (1135) returned error code 0xC0202009. The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing. There may be error messages posted before this with more information about the failure...

A component has returned from its PrimeOutput call. : 1135 : OLE DB Source
SSIS Error Code DTS_E_THREADFAILED. Thread "SourceThread0" has exited with error code 0xC0047038. There may be error messages posted before this with more information on why the thread has exited...

SSIS Error Code DTS_E_THREADCANCELLED. Thread "WorkThread0" received a shutdown signal and is terminating. The user requested a shutdown, or an error in another thread is causing the pipeline to shutdown. There may be error messages posted before this with more information on why the thread was cancelled...

SSIS Error Code DTS_E_THREADFAILED. Thread "WorkThread0" has exited with error code 0xC0047039. There may be error messages posted before this with more information on why the thread has exited...

After searching the whole day for a solution I finally stumbled accross this post by Adam Machanic. At first sight it did not fit my problem, but out of desperation I decided to give it a bash; and it worked.

I am not going to repeat what he said in his post because he did a brilliant job of it. But in short: tables, views and table-valued UDFs have explicitly defined output columns; and stored procs don't. So SSIS will have guaranteed success in retrieving the correct output columns from tables, views and table-valued UDFs, while a stored proc can return different resultsets based on the input parameters.

As I said before, this does not fit my problem since SSIS did pick up the correct output columns, but I suspect that it missed some crutial metadata which wasn't available in the stored procedure.

Monday, November 10, 2008

LINQ to SQL - Implementing the Repository pattern

I've been playing with LINQ to SQL for the past month. It really impresses me with its ease of use.

But I am getting some strange errors where EntitySets don't want to populate no matter what. I can do whatever I like with the relationships and the loading options, it does not want to work. It appears to be objects which wrap views in the database.

Then I read something about how tightly coupled objects can be bad in that they make unit testing a nightmare by complicating the data access layer.

So, I thought of using LINQ to SQL in a loosely coupled fashion. It will still increase the speed at which I develop my data access layer. And then I found someone who had a similar idea. Here it is: LINQ to SQL - Implementing the Repository pattern

Tuesday, July 15, 2008

Get all the dates in a range

Use this T-SQL script to get all the dates in a specified date range.

Please note that it will only work in SQL Server 2005 or later.

Monday, June 9, 2008

SQL Server 2005 Performance Dashboard Reports

Last week our dba resolved serious SQL Server performance issues very effectively using SQL Server Performance Dashboard Reports.
Our server's four dual core processors were averaging 99.73% for a couple of days. After monitoring the situation with SQL Server 2005 Performance Dashboard Reports and implementing the suggested performance enhancements, our server's CPUs are now working at between 12% and 60%.

More about Microsoft SQL Server 2005 Performance Dashboard Reports:
"The Microsoft SQL Server 2005 Performance Dashboard Reports are used to monitor and resolve performance problems on your SQL Server 2005 database server."
"The information captured in the reports is retrieved from SQL Server's dynamic management views. There is no additional tracing or data capture required, which means the information is always available and this is a very inexpensive means of monitoring your server."

Download it here.

Thursday, June 5, 2008

Update all index statistics after upgrading a db from SQL Server 2000 to 2005

After upgrading a database from SQL Server 2000 to SQL Server 2005, our application's performance degraded significantly. It was eventually resolved by creating two new indexes in the database.

However, I could not understand how database performance could be impacted negatively by an upgrade. All the indexes which were in the SQL 2000 db exist in the upgraded database.

After googling the topic for a while I came across the following article: Interview with Greg Linwood. Below is an important tip from the article.

Most upgrades I have been involved with have been fairly smooth but some degree of tuning has been required in most cases. One tip worth remembering is to update all index statistics after an upgrade (or simply rebuild all indexes) so that SQL Server’s cost optimizer has the most recent information possible by which to make the best query execution decisions. SQL Server 2005’s cost optimizer is significantly more sophisticated and will generally make the same or better decisions in most cases than it’s predecessor. These improved decisions rarely get noticed (as no-one complains when things improve!) but those which degrade performance sometimes cause the upgrade process to come under un-warranted criticism. Hence, I recommend closely monitoring performance during and after upgrades, just in case the query optimizer makes decisions which “appear” better to it but end up not working out.

Tuesday, June 3, 2008

Monday, June 2, 2008

SET FMTONLY

When you want to retrieve the metadata for a table, view, stored procedure, etc without having any records returned, use SET FMTONLY ON. Only the column data will be returned.

Thursday, May 22, 2008

Multiple SSIS instances on the same machine

You can only have one instance of SSIS (SQL Server Integration Services) installed on a machine at a time.

But you can have multiple SSIS database instances on one machine at the same time. See this post on how to do that.

Get the SQL Server Instance name from T-SQL

To get the SQL Server instance name with T-SQL use one of the following two statements:
  • select @@SERVERNAME
  • select SERVERPROPERTY('SERVERNAME')
Important: The SERVERNAME property of the SERVERPROPERTY function uses the computer's current network name. So, if the computer (on which the SQL Server instance is installed) had its network name changed after the SQL Server instance was installed, the SERVERPROPERTY function will pick up the new name - @@SERVERNAME will not! @@SERVERNAME will use the network name of the computer at the time when the instance of SQL Server was installed.

Click here to see more properties of the SERVERPROPERTY function.

Thursday, May 15, 2008

Get the names of all tables used in a SQL Server view

I want to be able to quickly identify the tables which make up a SQL Server view. I know I can just open the script and look at it, but I have the following problems:
  1. All the scripts are one liners for some strange reason.
  2. And some of the views contain large numbers of tables in the joins.
So I came up with this script. It will work in SQL Server 2000 and 2005.

Take note: This script is by no means foolproof, and I haven't tested it very extensively. I am even ignoring the tables' schemas altogether. And if a view and a table would have the same name my script wouldn't know the difference.
But hey, it returns the information I want, so I am happy.

I didn't use the system view INFORMATION_SCHEMA.VIEWS because it truncates a view's T-SQL script in the VIEW_DEFINITION column when the script is too long.

I rather used syscomments because it spreads a view's script over several records when the script is too long for the text column. This enabled me to search the whole script for tables names.
I am not sure if syscomments will split a script in the middle of a word when the script is too long for the text column. If that would be the case I could miss some table names because they can be split.
(syscomments is a system view in the sys schema in SQL Server 2005. In SQL Server 7.0 and 2000 it is a system table in the dbo schema.)

Wednesday, May 14, 2008

Undocumented Stored Procedures

Apparently there are extended stored procedures which are not documented in the Books Online. In this post I want to create a list of those stored procs with links to articles about how to use them. So far I have only one proc, but I will update this post as soon as I come across more.

I don't think this one is an extended proc, but also undocumented:
  • EXEC sp_MSForEachTable 'EXEC sp_spaceused [?]' - It allows one to execute a T-SQL statement against each table in a database.