Mirosoft Director of Sales Excellence, Eric Ligman, writes a post about 100s of FREE Microsoft eBooks each year.
These are legal DRM-free copies (PDF/EPUB/MOBI) on various Microsoft technologies like Windows, SQL Server, Azure, SharePoint, PowerShell, Visual Studio, Office, Dynamics etc.
The titles may repeat in multiple links below so I’d suggest that you begin browsing at the latest year URL and go backwards from there.
SQL Agent and SSIS packages have multiple failure codes but DTS_E_OLEDBERROR or 0x80040E55 don’t tell you much about the cause.
One of my jobs failed sporadically with the following error.
Message
Code: 0xC0202009
Source: [29]
Description: SSIS Error Code DTS_E_OLEDBERROR.
An OLE DB error has occurred.
Error code: 0x80040E55.
An OLE DB record is available.
Source: "Microsoft SQL Server Native Client 10.0"
Hresult: 0x80040E55
Description: "Column does not exist.".
End Error
Error: 2016-11-30 11:20:03.11
Code: 0xC0047038
Source: SSIS.Pipeline
Description: SSIS Error Code DTS_E_PRIMEOUTPUTFAILED.
The PrimeOutput method on 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.
End Error
The error message “Column does not exist.” led me to think that the stored procedure or one of the tables have been altered.
A quick look at the created date and modified date of the stored procedure and the tables confirmed that there were no schema changes.
So why the missing column error?
I executed the stored procedure manually and that revealed the issue.
There was TRY CATCH block in the procedure. It didn’t re-raise the error but returned it with a SELECT statement.
For example –
BEGIN TRY
-- some sample sql
SELECT
o.OrderNumber,
o.OrderLineNumber
FROM
dbo.Order as o
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
So when there was any error, the error message was returned as a SELECT output.
The column names of this output were different from the original columns. This caused the SSIS package to fail.
Take away –
Use RAISERROR or THROW in TSQL to return the errors to the calling application. Using a SELECT for this purpose can confuse the calling application to consider that as a data set.
SQL formatting may not increase the functionality of the code but good and consistent formatting by the developers certainly affect the readability of the code and therefore makes it easier to maintain. Keyword and separator alignment, spaces and tabs provide opportunities for a lot of variations in code layout. A case-insensitive server/database give further flexibility in formatting with choosing upper or lower casing of the text.
Just as an experiment to see the effect of casing and object naming conventions on readability, I created this dummy stored procedure and formatted it in four different ways. The white-space layout, i.e. tabs, spaces, newline etc is same in all of them.
Which one do you prefer?
Do you have your own layout options that you would like to share?
Feel free to copy the script and share your formatted version in comments.
Version 1
-- underscore as word separator
-- lowercase keywords
create procedure dbo.sp_get_employee
@employee_id int,
@department_id int
as
begin
select
e.first_name,
e.last_name,
e.designation,
e.salary,
e.manager_first_name,
e.manager_last_name,
d.department_name
from
dbo.employee as e with (nolock)
inner join dbo.department as d with (nolock)
on e.department_id = d.department_id
where
employee_id = @employee_id and
department_id = @department_id;
return 0;
end
Version 2
-- underscore as word separator
-- UPPERCASE keywords
CREATE PROCEDURE dbo.sp_get_employee
@employee_id INT,
@department_id INT
AS
BEGIN
SELECT
e.first_name,
e.last_name,
e.designation,
e.salary,
e.manager_first_name,
e.manager_last_name,
d.department_name
FROM
dbo.employee AS e WITH (NOLOCK)
INNER JOIN dbo.department AS d WITH (NOLOCK)
ON e.department_id = d.department_id
WHERE
employee_id = @employee_id AND
department_id = @department_id;
RETURN 0;
END
Version 3
-- camel/pascal casing for objects names
-- lowercase keywords
create procedure dbo.spGetEmployee
@EmployeeId int,
@DepartmentId int
as
begin
select
e.FirstName,
e.LastName,
e.Designation,
e.Salary,
e.ManagerFirstName,
e.ManagerLastName,
d.DepartmentName
from
dbo.Employee as e with (nolock)
inner join dbo.Department as d with (nolock)
on e.DepartmentId = d.DepartmentId
where
EmployeeId = @EmployeeId and
DepartmentId = @DepartmentId;
return 0;
end
Version 4
-- camel/pascal casing for objects names
-- UPPERCASE keywords
CREATE PROCEDURE dbo.spGetEmployee
@EmployeeId INT,
@DepartmentId INT
AS
BEGIN
SELECT
e.FirstName,
e.LastName,
e.Designation,
e.Salary,
e.ManagerFirstName,
e.ManagerLastName,
d.DepartmentName
FROM
dbo.Employee AS e WITH (NOLOCK)
INNER JOIN dbo.Department AS d WITH (NOLOCK)
ON e.DepartmentId = d.DepartmentId
WHERE
EmployeeId = @EmployeeId AND
DepartmentId = @DepartmentId;
RETURN 0;
END
Microsoft announced the general availability of SQL Server 2016 today. This version has many path-breaking updates for performance, security and business intelligence. On top of that, the Developer Edition of SQL Server 2016 and 2014 are now completely free! There is no restriction on features, capabilities or how long you can use them! Read on to find out how to download free SQL Server 2016 Developer Edition and other goodies.
In these Developer Editions, the databases do not have to be smaller than X GB, the feature Y will not be disabled, the time is not restricted to Z days (i.e. 180 days) and you don’t have to pay any $ Dollars. These Developer Editions have all the features of the Enterprise Edition, but should not be used for Production.
SSDT is a free development tool that is a subset of Visual Studio IDE. It is focused on database and business intelligence related development i.e. TSQL, SSIS, SSRS and SSAS. https://msdn.microsoft.com/en-us/library/mt204009.aspx
Download Free eBook
While at your downloading spree, get the final version of the free MS Press eBook that gives an overview of the latest features in the SQL Server 2016 version.
Introducing Microsoft SQL Server 2016
Mission-Critical Applications, Deeper Insights, Hyperscale Cloud
Although the title says that it is an introductory book, it is certainly not a beginner’s level text. In its nine chapters, it introduces the new features of SQL Server 2016. So it will be more useful if you have already worked with SQL Server for a while and are aware of features (or possibilities for enhancements) in the previous versions.
Microsoft is releasing its Developer Editions of SQL Server 2014 and 2016 for free!
Developer Edition is the same as the Enterprise Edition but it is licensed for development and testing purposes only.
The Developer license for the previous versions (2008 R2, 2012) used to go for about $50 and up. Another option was to download the fully functional edition valid for 180-days and later buy a licence.
More information about Microsoft’s new approach to developer licensing is at our Facebook post –
Some index maintenance scripts or dynamic SQL scripts use SQL object names as values for variables or columns i.e. database name, table name, index name etc.
Another technically correct thing to do is to use the QUOTENAME function to wrap the object name in [ and ] brackets. This handles those cases where there are special characters in the name or the name is a reserved keyword.
Example –
set @dbname = quotename(db_name(db_id()))
set @tablename = quotename(object_name(object_id))
set @indexname = quotename(object_name(object_id))
The Issue
The brackets will add two more characters to the value.
So for really long object names that are 128 characters in length, the value would be 128 + 2 = 130 characters.
The two extra characters will break the variable assignment or row insert statements with truncation error –
String or binary data would be truncated. [SQLSTATE 22001] (Error 8152).
In my shop, we do have some long index names and I have had to debug some scripts for this issue. And this is not the first time!
The Solution
I suggest that you use 130 as the width for the variables or columns that will store an object name. SQL Server object names can be maximum 128 characters long so using 130 characters in scripts will handle the extra two characters.
One of our database restore attempt failed with an error message that mentioned cluster resources. At least the error message indicated that the issue was not related to backward compatibility but rather a physical resource or cluster settings.
Error Details
The Error Message Window –
The Error Message –
TITLE: Microsoft SQL Server Management Studio
------------------------------
Restore failed for Server 'MYDEVSQLSERVER'. (Microsoft.SqlServer.SmoExtended)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.2500.0+((KJ_PCU_Main).110617-0038+)&
EvtSrc=Microsoft.SqlServer.Management.Smo.
ExceptionTemplates.FailedOperationExceptionText
&EvtID=Restore+Server&LinkId=20476
------------------------------
ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: Cannot use file 'J:MSSQL10_50MSSQLDATAMyDatabaseName.mdf' for clustered server. Only formatted files on which the cluster resource of the server has a dependency can be used. Either the disk resource containing the file is not present in the cluster group or the cluster resource of the Sql Server does not have a dependency on it. (Microsoft.SqlServer.Smo)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.2500.0+((KJ_PCU_Main).110617-0038+)&LinkId=20476
------------------------------
BUTTONS:
OK
------------------------------
Both the links in the error message above pointed to a missing information message on Microsoft website –
Details
ID:
Restore Server
Source:
We’re sorry
There is no additional information about this issue in the Error and Event Log Messages or Knowledge Base databases at this time. You can use the links in the Support area to determine whether any additional information might be available elsewhere.
Thank you for searching on this message; your search helps us identify those areas for which we need to provide more information.
Cause and Resolution
We determined the cause rather quickly. The source system of the backup file had a drive letter layout that was different from the destination server. The restore process was trying to create the data files on a drive that didn’t exist on the destination! So the location of the files was changed in the restore dialog to a correct drive letter of the destination server. After that the restore progressed normally.
One of my SQL Server databases was returning an error 102 while creating a full-text stoplist. We were trying to create a stoplist based on the system stoplist and later also tried to create a blank stoplist. The error happened both via SSMS, and equivalent TSQL commands.
The Error, 102
The following TSQL gave the error –
USE [DEMO_Database]
GO
CREATE FULLTEXT STOPLIST [DemoStopList]
AUTHORIZATION [dbo];
GO
CREATE FULLTEXT STOPLIST [DemoStopList]
FROM SYSTEM STOPLIST
AUTHORIZATION [dbo];
GO
The error dialog box –
The text in the error message –
TITLE: Microsoft SQL Server Management Studio
------------------------------
Cannot execute changes.
------------------------------
ADDITIONAL INFORMATION:
Create failed for FullTextStopList 'Demo'. (Microsoft.SqlServer.Smo)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=11.0.5058.0+((SQL11_PCU_Main).140514-1820+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Create+FullTextStopList&LinkId=20476
------------------------------
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
------------------------------
Incorrect syntax near 'STOPLIST'. (Microsoft SQL Server, Error: 102)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=10.50.1600&EvtSrc=MSSQLServer&EvtID=102&LinkId=20476
------------------------------
BUTTONS:
OK
------------------------------
The Cause
I looked at the MSDN page related to the TSQL command to check if I was using the right syntax.
My syntax was correct but there was something else on the page that looked relevant. Right at the top of the documentation page is the following message –
Important
CREATE FULLTEXT STOPLIST, ALTER FULLTEXT STOPLIST, and DROP FULLTEXT STOPLIST are supported only under compatibility level 100. Under compatibility levels 80 and 90, these statements are not supported. However, under all compatibility levels the system stoplist is automatically associated with new full-text indexes.
To verify if the compatibility level of my database could indeed be an issue, I checked the properties of the database by –
SELECT
is_fulltext_enabled,
compatibility_level
FROM
sys.databases
is_fulltext_enabled
compatibility_level
0
90
There you have it! My database was originally on a SQL Server 2005 installation so its compatibility level was 90, and that was the reason the CREATE/ALTER/DROP STOPLIST commands were unavailable. The current server that I was working on was SQL Server 2008 R2, which could be checked by –
SELECT @@VERSION
GO
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1
So the resolution to the error lies in changing the compatibility level. As per the documentation, the highest compatibility level I could go on a SQL Server 2008 R2 installation was 100.
REFERENCE:
View or Change the Compatibility Level of a Database
https://msdn.microsoft.com/en-us/subscriptions/index/bb933794
ALTER DATABASE Compatibility Level (Transact-SQL)
https://msdn.microsoft.com/en-us/subscriptions/index/bb510680
Changing the Compatibility Level
I checked that no other users were connected to the database and then issued this command to change the compatibility level.
USE [master]
GO
ALTER DATABASE [DEMO_database]
SET COMPATIBILITY_LEVEL = 100;
GO
It ran successfully and I could verify in the sys.databases catalog view that the compatibility level has changed to 100.
Now I was able to create a Stop List, Full-text Catalog and a Full-text Index on my table, and was able to run queries using the CONTAINS and CONTAINSTABLE keywords.
Fixed? Not so fast!
Interestingly, even though I could use the Full-text features now, the is_fulltext_enabled property still showed up as 0 (i.e. Disabled).
That was fixed by running the following –
EXEC [DEMO_Database].[dbo].[sp_fulltext_database]
@action = 'enable'
GO
Some things are meant to be learnt the hard way. And that is how I learnt about today’s gotcha.
Overview of [sysdiagrams]
I have been working on an ERD (Entity Relationship Diagram) recently and used the Database Diagram feature in SSMS for this purpose. When you try to create a diagram for the first time in a database, a message box asks you if you would like to create diagramming objects.
On clicking Yes, a system table by the name of [sysdiagrams] is created in the same database that you are creating the diagram in. The diagrams are stored in this table. SSMS shows the diagram in the Database Diagrams node of the Object Explorer tree view.
The Scenario
I was creating my diagram in a test environment. At some point I had to refresh all data from the production environment. The easiest way for me to do a full refresh is to use the Import and Export Wizard. The wizard can be launched either via SSMS context menu or in the Business Intelligence Developer Studio as an SSIS project. As usual in the case of quick data refreshes, I selected all tables using the top-left Source checkbox in the wizard, and chose the options to delete and reinsert all rows with identity values.
When the wizard ran successfully, my database diagram at the destination test system was missing!
Gotcha
Upon some research and trials I found that that if the diagramming capabilities in the source and destination servers are enabled, the wizard includes the [sysdiagrams] table automatically in the list of tables to refresh. As you can see, there are no other system tables in the wizard except the [sysdiagrams] table so it is easy to miss it in a long list.
So in my case, all data in the destination [sysdiagrams] table was deleted. There were no diagrams at the source so nothing was imported for this table. This outcome would have been the same with the drop and recreate option too because the destination table would have been recreated.
Conclusion
One needs to be careful while using the Import and Export Wizard. Uncheck this table in the selection list to preserve the diagrams at destination.
I recently wrote about How to Use Temp Table in SSIS. One of the requirements to successfully reuse a temporary table across multiple tasks in SSIS is to set the RetainSameConnection property of the OLEDB Connection to TRUE. In this post, I will discuss the property and also use a Profiler Trace to find out its behavior.
The RetainSameConnection Property
RetainSameConnection is a property of an OLEDB Connection Manager. The default value of this property is FALSE. This default value makes SSIS execution engine open a new OLEDB connection for each task that uses the connection and closes that connection when the task is complete. I believe the idea behind this is to not block a connection to a server unnecessarily and release it until it is needed again. And it makes sense too because some packages can run for an extended duration and may not need to be connected to an OLEDB server all the time. For example, an OLEDB in not required to be open while parsing text files, sending emails, ETL operations not involving the OLEDB server in question etc. Releasing connections unless really required can be certainly helpful on busy servers because SQL Server needs some memory for each open connection.
On the other hand, some scenarios require a persistent connection e.g. temporary table reuse across multiple tasks. We can set the property value to TRUE and then it will open just one OLEDB connection with a server and keep it alive until the end of the package execution. The property can be set via the Properties window for the OLEDB Connection Manager.
The Temporary Table Scenario
Local temporary tables (with a # in front of their name) in SQL Server are scoped to a session. SQL Server drops them when the session is closed. This means, local temporary tables created in one session are not available in another session. In SSIS, with the RetainSameConnection set to FALSE (the default), a new session is opened for each task. Therefore, temporary tables created by a task are not available to another task.
Demo
I have a demo package with two Execute SQL Tasks and one OLEDB Connection Manager. The Execute SQL Tasks have a simple SELECT statement and they both use the same connection manager.
I have a Profiler Trace to monitor the number of connections created by the SSIS package.
The first execution of the package is with the RetainSameConnection set to the default value of FALSE. The trace captures two pairs of login/logout events, one for each task. The second execution is with the property value set to TRUE. This time, the trace captures only one pair of login/logout events.
Conclusion
In most cases, the default value of RetainSameConnection=FALSE will be suitable. A developer should make a decision to enable it when the package tasks really need a persistent connection. In addition to the temporary table reuse, a TRUE value for this property can also be useful in managing transactions and reducing the number of recurring connection requests to a server.