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 ArticleEXISTS vs. COUNT(*)
Often times I see the following query used to determine if records exist: IF ((SELECT COUNT(*) FROM Business) > 0) BEGIN –Do some stuff END My first guess would be that SQL Server would actually...
View ArticleSQL Server 2008 Infrastructure Planning and Design Guide
The new IPD (Infrastructure Planning and Design) Guide for SQL Server 2008 was released late last month. This guide serves as a high level review of many of the key considerations when implementing a...
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 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 ArticleBuilding Indexes Online Creates Larger Indexes
When I create an index on a table, I usually run a script of mine that among other things gives me the size of the created index. This allows me to see for example if the overhead of compressing the...
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 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 Article