Execute T-SQL Batches in a Loop
In T-SQL, the keyword “GO” is the default batch separator. For example: DELETE FROM ErrorLog WHERE ErrorTime < ’2008-01-01′ GO But what if you have billions of rows in the “ErrorLog” table...
View ArticleHow to use CASE in a CHECK Constraint
If you need to use a CASE statement in a check constraint, you may be inclined to use something like this: use AdventureWorks2008 GO ALTER TABLE Person.EmailAddress ADD CONSTRAINT...
View ArticleSQL Server DateTime Format Function
Creating a DateTime Format Function While it is possible to format dates using T-SQL, it is not always efficient. Sure there are a million custom format functions available and you can even use the...
View ArticleFinding Object References in SQL Server 2005 & 2008
You can use the following query to search through all of the code in a SQL Server database for a given string. This is often useful when identifying dependencies in code. –Make sure to set the...
View ArticlePad a Number with Zeros – SQL Server 2005 and SQL Server 2008
If you need to pad a number with zeros do the following: Convert the number to a string padded with spaces by using the STR function. Replace the spaces with 0’s. DECLARE @NumberToPad INT; SET...
View ArticleParameters in SQL Server XPath Queries
If you have forgotten the syntax for using parameters in XPath queries within SQL Server 2005 and SQL Server 2008 the following should refresh your memory. Hard coded example: SELECT * FROM...
View ArticleHow to: list all primary keys in a database
I recently had the following question posted to this site: Hello, I’d think this is a simple question/problem. I want to get the primary key name for each table in my schema. By doing so, it will...
View ArticleLong Running Task Progress Report
There are many operations that take a long time to execute in SQL Server including the following: ALTER INDEX REORGANIZE AUTO_SHRINK option with ALTER DATABASE BACKUP DATABASE DBCC CHECKDB DBCC...
View ArticleType Conversion Precedence
Here’s an interesting question: What is the resulting data type of the following query? SELECT’38’ UNION SELECT 94 The answer is an “int”. The reason for this is SQL Server’s precedence order. It...
View ArticleHow to: list all primary keys in a SQL Server database
I recently had the following question posted to this site: Hello, I’d think this is a simple question/problem. I want to get the primary key name for each table in my schema. By doing so, it will...
View ArticleHow to: Determine SQL Server Transaction Log Space Usage
I have a client that recently asked me if the transaction log of one of his databases was being truncated on a regular basis. The database was using the “simple” recovery model and as such is...
View ArticleHow to: Enable CLR (Common Language Runtime) in SQL Server
With the release of SQL Server 2005 it became possible for developers to write C# or VB.NET code that can execute within SQL Server. One thing that is often times overlooked when installing a .NET...
View ArticleHow to: Understanding the SQL Server FILLFACTOR setting
What is Fill Factor? When dealing with index design in SQL Server one of the single most important things to configure is the fill factor. The fill factor is a number between 0-100 which indicates how...
View ArticleHow to: Identify the object stored at a given page location–SQL Server
Often times when troubleshooting performance issues with SQL Server, you will identify a file id and a page id that is the source of your troubles. The question is: How do I translate this File Id /...
View ArticleType Conversion Precedence
Here’s an interesting question: What is the resulting data type of the following query? SELECT’38’ UNION SELECT 94 The answer is an “int”. The reason for this is SQL Server’s precedence order. It...
View ArticleHow to: list all primary keys in a database
I recently had the following question posted to this site: Hello, I’d think this is a simple question/problem. I want to get the primary key name for each table in my schema. By doing so, it will...
View ArticleLong Running Task Progress Report
There are many operations that take a long time to execute in SQL Server including the following: ALTER INDEX REORGANIZE AUTO_SHRINK option with ALTER DATABASE BACKUP DATABASE DBCC CHECKDB DBCC...
View Article