• subscribe
June 26, 2002 12:00 AM

Using T-SQL to Concatenate Data Into a Variable

SQL Server Pro
InstantDoc ID #25262
Downloads
25262.zip

Can I create in T-SQL a stored procedure that loops through rows and concatenates data into a variable, as I do in Visual Basic (VB)?

There are two basic ways to solve problems in T-SQL: row-by-row processing or set-based logic. Row-by-row processing is similar to what you might do in a procedural language such as VB. Usually, you implement row-by-row processing in T-SQL by using a T-SQL—based cursor. You can use a cursor to loop through a data set, concatenating data to an existing variable, but that approach is inefficient. Let's look at a simple example of how to use a cursor, then contrast the cursor to another approach to prove that cursors are an inefficient way to process data. (Experienced T-SQL developers can skip to the part of this answer in which I discuss Listing 3 to see an interesting T-SQL construct that you can use to concatenate data to a variable during a set-based operation.)

Listing 1 shows a simple example of how to use a cursor to calculate the sum of freight for all rows in the Northwind database's Orders table. This example calculates the correct value of 64942.6900 for the sum of the freight column across all rows in the Orders table, but it took 130ms to run on my laptop. Although 130ms might not sound like a lot of time, it's slow for such a small amount of data.

Listing 2 shows a better way to determine the sum of freight for all rows in the Orders table. The SUM() function is the traditional way to use SQL to solve this type of problem. The solution generates the same answer but is faster than the cursor method, and the code is clearly simpler. My laptop showed that the query ran in 0ms; however, because the SQL Server GETDATE() function is only accurate to within plus or minus 3ms, my test time was really between 0 and 3ms.

Listing 3 shows another set-based solution that generates the correct answer and runs in 0ms, but this solution looks like row-by-row processing. Many T-SQL developers don't realize you can use T-SQL this way. Only in rare cases would you want to use this approach instead of the built-in SUM() function because you'd be reinventing the wheel—using built-in functions is easy and simple. However, the solution that Listing 3 shows can be handy when you encounter a problem that a built-in function can't solve—such as the problem that Listing 4 demonstrates.

The code in Listing 4 shows how you can use Listing 3's approach to concatenate and pivot string data into a single variable. Listing 4's code sums data across an integer column, but it also concatenates the string value of stor_id for each row in the Pubs database's sales table into a comma-delimited string.



ARTICLE TOOLS

Comments
  • PETER
    6 years ago
    Jan 26, 2006

    Had no idea that the ability to concatenate strings existed in that fashion. Wish it were possible just to SUM(string, 'delimiter'), but this is pretty cool. Planning to use it in one of my reports. Just need to find a way to not have ', prepended or appended to each set now. Thanks for the tip.

  • Anonymous User
    7 years ago
    Aug 23, 2005

    Spectacular. Thanks so much, this was exactly what I needed to learn.

  • Anonymous User
    7 years ago
    Apr 29, 2005

    Great never thought of the 2nd listing example this is going to save me lots of time. Great Work !!!!!!!!!!!!!!!!!!!

  • Anonymous User
    7 years ago
    Apr 29, 2005

    I meant Listing 3 not 2 - Great

  • Anonymous User
    7 years ago
    Jan 31, 2005

    Would love to see more set based examples. Actually an article that shows diff things that SQL programers write cursors to do and show simple set based examples of the same would be wonderful! Anyone know of any nice examples?

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...