Development

  • Getting started with dynamic SQL

    Sometimes you need dynamic SQL. Sometimes you don't know at run time where you're querying or what fields you need to bring back, or what your criteria will be, so there will always be a place for the use of dynamic SQL. You have to be careful how you use it, but if you do use it correctly it can be a powerful tool that you have in your tool belt. So, today I am going to show you a nice little general purpose procedure that you can write to pass in a few object strings and execute dynamic...
  • Creating Indexes with IGNORE_DUP_KEY

    Unique indexes and unique constraints limit the values that are allowed in a table column to unique values. This uniqueness is almost always a result of some type of business rule. Programming these types of business rules are usually not overcomplicated to write. However, SQL Server has an option that you can set when creating unique indexes that allows you to control how duplicate values are handled so that you can potentially get away from writing these rules by hand. The unique index option,...
  • SQL Server 2008 Syntactic Sugar

    As a developer, I always enjoy new TSQL language features that can save me some time and potentially effort in accomplishing the same task that I otherwise could in a different fashion. These features aren't really a big deal to me to have but are sometimes fun. SQL Server 2008 has produced a couple of these features so I thought it might be worthwhile to take a look at them. Compound Operators This is a feature that exists in most common-day programming languages. The idea is simple; why use...
  • Pivot Rolling Periods : Dynamic, But Not!

    In the Article, Pivot Rolling Periods Without Using Dynamic T-SQL ( experts-exchange.com/viewArticle.jsp?aid=654 ), I explored the pivot of moving date based reports without the use of dynamic T-SQL, but still allow for re-use month after month without re-writing of code. Here is another introductory tidbit to SQL Server 2005's PIVOT ( technet.microsoft.com/en-us/library/ms177410.aspx ). We will embrace the limitations of the PIVOT keyword requiring hard coded values / column names, but still...
  • Unstringing a comma delimited column

    Ever had a column which had a pile of information delimited by commas that you want to unpack ? Well, it is not that hard so long as the information is pretty short, and tightly controlled... -- step 1 create a sample table used as a datasource... if object_id('tempdb..#tmp_colors','U') is not null drop table #tmp_colors create table #tmp_colors (Date datetime, Name varchar(60), Choices varchar(100)) insert #tmp_colors select '01 Mar 2009','John S','Red,Blue,Yellow'...
  • Two things I love about Management Studio 2008

    I've been using the SQL Server product for quite some time now, and I almost always use the IDE programs that ship with the database engine. Enterprise Manager and Query Analyzer certainly had their quicks, but were overall nice tools. Management Studio 2005 was a great improvement over these tools because it integrated Enterprise Manager and Query Analyzer into one tool, and it added some neat bells and whistles. Now that Management Studio 2008 is out, there are two really neat new features...
  • Using TRY...CATCH IN SQL Server 2005/2008

    New to SQL Server 2005 (and present in SQL Server 2008) is the ability to use TRY..CATCH constructs inside of your TSQL code to catch error statements. If you've done any type of programming in other programming languages, you know that this is a very handy tool to have. Whats even better in my opinion is the ability to write a generic error handling procedure you can use inside of your CATCH blocks to capture and record errors when they occur. So, lets take a look at how we can take advantage...
  • Concatenating Results through SELECT statements

    In this SQL tip I am going to show you a neat little trick which involves using a simple SELECT statement to concatenate row values into a single variable value. First, I'll create a temp table named #Results for which I'll store FirstName and LastName data. IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results CREATE TABLE #Results ( FirstName varchar(25), LastName varchar(25) ) GO This script adds some names to the temp table I just created. INSERT INTO #Results(FirstName...
  • Stripping the time from a DATETIME value

    Many times there is a need to strip the time off of a DATETIME value so you are left with the date only. I've seen a few different ways to handle this. To clarify, when I say a DATETIME value with no time, I really mean midnight (00:00:00.000) on that date. Converting to VARCHAR One way to remove the time from a DATETIME value is to convert it to a VARCHAR data type. This is handy when you need the date returned as a VARCHAR value and when you need the option to return the date in any number...
    Filed under: ,
  • Finding the major version number of your SQL Server instance

    Occasionally, I need to determine which version of SQL Server I am connected to within a script so that I can write the proper code for that version. I’m never really concerned with the build number or which service pack I have installed. Instead, all I want is the major version number (7, 8, 9, 10, etc). One of the difficult things about something that seems like it should be so simple is that some of the ways to check the version number only exist on certain versions. Other ways give you...
    Filed under: , ,
  • Nested Common Table Expressions

    If you've looked up a Common Table Expressions in Books Online, you might have noticed that Microsoft says that you cannot have nested CTEs (meaning, you can't define a CTE in the definition of another CTE. Although this is technically true, you can still accomplish the same functionality in a different manner. Here is an example of a simple CTE using the AdventureWorks database that will return a list of orders. USE AdventureWorks ;WITH CTE_Orders (OrderID, OrderDate) AS ( SELECT PurchaseOrderID...
Copyright SQL Server Nation 2010
Powered by Community Server (Non-Commercial Edition), by Telligent Systems