Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

A simple T-SQL statement to create a list of lookup values

By Suresh G. Kumar, (first published: 2011/03/21)

Introduction

Let's say we have a simple table of lookup values and that we want to get a list of these values into a simple comma delimited character list. We can accomplish this with either a traditional CURSOR or a WHILE loop. However, listed below is an easier way to produce the same list without having to use the infamous CURSOR.

Method

To illustrate this new approach, let's create a small table called MyStatus and load it with some sample entries.

CREATE TABLE [dbo].[MyStatus](
 [Status_Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
 [StatusDesc] [varchar](25) NULL,
)

Now let's load this table with some sample values using INSERT statements.

INSERT INTO MyStatus VALUES ('Active')
INSERT INTO MyStatus VALUES ('OnHold')
INSERT INTO MyStatus VALUES ('Disabled')
INSERT INTO MyStatus VALUES ('Closed')

Now our objective is to create a list of comma delimited valid values, for example: Active,OnHold,Disabled,Closed

To get this list one would normally use a CURSOR (or a WHILE loop) as listed below:

/*--------------- CURSOR approach --------------- */

DECLARE @MyStatusList VARCHAR(1000)
DECLARE @StatusDesc VARCHAR(25)

DECLARE myCursor CURSOR FOR SELECT StatusDesc FROM MyStatus

OPEN myCursor
SET @MyStatusList = ''
FETCH NEXT FROM myCursor INTO @StatusDesc 
WHILE @@FETCH_STATUS = 0
BEGIN
 SET @MyStatusList = ISNULL(@MyStatusList,'') + @StatusDesc + ','
 FETCH NEXT FROM myCursor INTO @StatusDesc 
END 
CLOSE myCursor
DEALLOCATE myCursor

/* drop the trailing delimiter and display */
SET @MyStatusList = SUBSTRING(@MyStatusList, 1, LEN(@MyStatusList)-1) 
SELECT @MyStatusList

/*--------------- End of CURSOR approach --------------- */

Now, here is a simpler technique to get the same result without having to use a CURSOR (or a WHILE loop)

SET @MyStatusList = ''
SELECT @MyStatusList = ISNULL(@MyStatusList,'') + StatusDesc + ',' FROM MyStatus

If only distinct values are needed, then one must replace the above SELECT statement with the one below.

SELECT @MyStatusList = ISNULL(@MyStatusList,'') + StatusDesc + ',' 
 FROM (SELECT DISTINCT StatusDesc FROM MyStatus) x

Finally drop the trailing comma at the end and then display.

SET @MyStatusList = SUBSTRING(@MyStatusList, 1, LEN(@MyStatusList)-1) 
SELECT @MyStatusList

Conclusion

In this article, we provided a simple way to get a comma delimited list from a table of entries without having to use a CURSOR or a WHILE loop to read through the table. We also listed the actual T-SQL code to accomplish this task, along with an example table and some sample entries.

Resources:

Creating a loop of lookup values.sql
Total article views: 33164 | Views in the last 30 days: 9
 
Related Articles
FORUM

Cursor

cursor

FORUM

select multirecs from single recs without cursor

select multirecs from single recs without cursor

FORUM

how do i supress cursor select set results

how do i supress cursor select set results

FORUM

How to Group and batch select/cursor results in stored procedure

How to Groupand batch select/cursor results for e-mail

Tags
cursor    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones